Results 1 to 3 of 3

Thread: please help!

  1. #1
    jc5
    jc5 is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default please help!

    Hi,

    I'm relatively new to Java GUI's, although I have written GUI's in other languages. I tried writing the following code and I found a problem that has puzzled me immensely. In the code below, if I comment out the following line from the main function:

    panel.add(label);

    it doesn't work anymore. If I leave this line in, the code works perfectly. I don't understand why it doesn't work if I remove this label from the JPanel. If anyone can help me, I'd be super grateful!

    I'm using Netbeans 6.5.1
    java 1.5.0_18



    The following code can be copied and pasted into Main.java and compiled
    straightaway.





    Java Code:
    
    import java.awt.event.ActionListener;
    import java.lang.Object.*;
    import java.lang.Thread.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    
    
    public class Main {
    
    
        public static JLabel label = new JLabel("new label");
        public static myFrame frame = new myFrame();
        public static myPanel panel = new myPanel();
        private static JButton go = new JButton("go");
        private static JButton exit = new JButton("exit");
        private static myListener listener = new myListener();
        public static drawPanel drawpanel = new drawPanel();
    
    
    
        public static void main(String[] args)
        {
    
    
            System.out.println("program started");
    
            frame.setSize(500, 500);
    
            go.setActionCommand("actiongo");
            exit.setActionCommand("actionexit");
    
            exit.addActionListener(listener);
            go.addActionListener(listener);
    
            panel.add(go);
            panel.add(exit);
    
            panel.add(label);
    
    
            frame.add(panel);
    
            frame.setVisible((true));
    
        }
    
    
    }
    
    
    
    class myListener implements ActionListener {
    
    
    
        public void actionPerformed(ActionEvent e) {
    
            if ("actiongo".equals(e.getActionCommand())) {
    
                Main.label.setText("go button clicked");
    
                Main.frame.remove(Main.panel);
                Main.frame.add(Main.drawpanel);
    
            }
    
            else if("actionexit".equals(e.getActionCommand())){
    
                Main.label.setText("exit button clicked");
    
            }
    
        }
    
    
    
    
    }
    
    
    
    class myFrame extends JFrame
    {
    
    
    
    
    
    }
    
    
    class myPanel extends JPanel
    {
    
    
    
    
    
    }
    
    class drawPanel extends JPanel
    {
    
        @Override public void paintComponent(Graphics g)
        {
    
            super.paintComponent(g);
            g.setColor(Color.blue);
            g.drawOval(20, 20, 20, 20);
    
        }
    
    
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Your code is really quite a mess. You are doing Java the wrong way. Get Sun's tutorial and read it. It has a comprehensive Swing section too.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    As r035198x said,ur code is a big mess.

    But,one thing I can say.
    If u remove the
    "panel.add(label)" ,it will not work properly because u are trying to do something with the label which is not added to panel.
    Ex : Test.label.setText("go button clicked");
    Ramya:cool:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •