TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   The Lounge (http://www.talkphp.com/lounge/)
-   -   Anyone here knows Java? (http://www.talkphp.com/lounge/4169-anyone-here-knows-java.html)

Tanax 04-21-2009 01:14 PM

Anyone here knows Java?
 
Heya!
I don't know where to post this, so the Lounge it is!

Anyway, I have a problem with my Java script(not JavaScript - Java script), and I'm wondering if someone could help me out what the problem(s) is(/are)?

With syntax highlighting: http://pastebin.com/m4666a6ed
With no syntax highlighting:
Code:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class GUI extends JFrame implements ActionListener
{
       
        private static final String FRAME_TITLE = "Bank";
       
        private static final int FRAME_WIDTH = 600;
        private static final int FRAME_HEIGHT = 400;
       
        private static final int FRAME_X_ORIGIN = 300;
        private static final int FRAME_Y_ORIGIN = 350;
       
        private JMenu fileMenu;
        private JMenuBar menuBar;
       
        private Container cont;
       
        private JTextField addCust_socNr;
        private JTextField addCust_name;
        private JButton addCust_add;
       
        public static void main(String[] args)
        {
               
                GUI frame = new GUI();
                frame.setVisible(true);
               
        }
       
        public GUI()
        {
               
                this.initiate();
                this.build();
               
        }
       
        public void initiate()
        {
               
                this.cont = this.getContentPane();
                this.menuBar = new JMenuBar();
                setJMenuBar(this.menuBar);
               
        }
       
        public void build()
        {
               
                this.buildFileMenu();
               
                this.setTitle(FRAME_TITLE);
                this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
                this.setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
                this.setResizable(false);
               
                this.setDefaultCloseOperation(EXIT_ON_CLOSE);
               
        }
       
        public void actionPerformed(ActionEvent event)
        {
               
                String option;
               
                option = event.getActionCommand();
                if(option.equals("Quit"))
                {
                       
                        System.exit(0);
                       
                }
               
                else if(option.equals("New customer"))
                {
                       
                        this.addCustomer();
                       
                }
               
                else if(option.equals("Import customer"))
                {
                       
                        JOptionPane.showMessageDialog(null, "This is not supported yet.");
                       
                }
               
        }
       
        public void buildFileMenu()
        {
               
                this.fileMenu = new JMenu("File");
               
               
                JMenuItem newCustomer = new JMenuItem("New customer");
                newCustomer.addActionListener(this);
               
                JMenuItem openCustomer = new JMenuItem("Import customer");
                openCustomer.addActionListener(this);
               
                JMenuItem closeProgram = new JMenuItem("Quit");
                closeProgram.addActionListener(this);
               
                this.fileMenu.add(newCustomer);
                this.fileMenu.add(openCustomer);
                this.fileMenu.addSeparator();
                this.fileMenu.add(closeProgram);
               
                this.menuBar.add(this.fileMenu);
               
        }
       
        public void addCustomer()
        {
               
                this.cont.setLayout(new BorderLayout());
               
               
                JPanel mainContainer = new JPanel();
                mainContainer.setLayout(new GridLayout(2, 1));
               
                JPanel leftContainer = new JPanel();
                leftContainer.setLayout(new GridLayout(2, 1));
               
               
                this.addCust_socNr = new JTextField();
                this.addCust_socNr.setColumns(22);
               
                this.addCust_name = new JTextField();
                this.addCust_name.setColumns(22);
               
                this.addCust_add = new JButton("Add");
                this.addCust_add.addActionListener(this);
               
                mainContainer.add(this.addCust_socNr);
                mainContainer.add(this.addCust_name);
                leftContainer.add(this.addCust_add);
               
                this.cont.add(leftContainer, BorderLayout.WEST);
                this.cont.add(mainContainer, BorderLayout.CENTER);
               
        }
       
}

When I click "New customer" it should show a complete layout.. but it doesn't show anything. I tried just doing this.setTitle to change the title when I'm in addCustomer method - it works, so it gets directed to the method properly.. it just doesn't add the layout..

Anyone? :-/

sketchMedia 04-21-2009 02:23 PM

Im no expert, but try this:
java Code:
this.setVisible(true);
at the end of the addCustomer method, worked for me.

I'm guessing you needed to tell it to repaint the frame.

Tanax 04-21-2009 02:38 PM

Ohya, that did it! Thanks a bunch!

sketchMedia 04-21-2009 02:51 PM

No problem mate. Its quite a while since i did any Java (and even then it wasn't with a GUI) i could do with reacquainting myself with it again.

To avoid code repetition, you could put that line at the end of the actionPerformed method.

Tanax 04-21-2009 05:29 PM

Ye, I'm actually going a course right now in Java on the university.. :-)

Yep, that's what I did already ;-)


All times are GMT. The time now is 11:28 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0