Results 1 to 16 of 16

Thread: GUI new frame

  1. #1
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default GUI new frame

    hi there im quite new to this and i am trying to find out how to create a new frame with a third button, by pressing on the second button in my original frame my code is shown below any help would be greatly appreciated. thanks

    // GuiDemo4
    // Simple example of 2 JButtons

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

    public class GuiDemo4 extends JFrame {

    private JButton myFirstButton;
    private JButton mySecondButton;

    // Constructor for a new frame

    public GuiDemo4() {

    super("My First Button Program");

    myFirstButton = new JButton("PRESS LEFT");
    myFirstButton.setFont(new Font( "Arial", Font.BOLD, 18));
    myFirstButton.setBackground(Color.red);

    mySecondButton = new JButton("PRESS RIGHT");
    mySecondButton.setFont(new Font( "Arial", Font.BOLD, 18));
    mySecondButton.setBackground(Color.green);

    Container c = getContentPane();
    FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
    c.setLayout(fl);

    c.add (myFirstButton);
    c.add (mySecondButton);

    ButtonHandler handler = new ButtonHandler();
    myFirstButton.addActionListener(handler);
    mySecondButton.addActionListener(handler);

    setSize(400, 300);
    setVisible(true);
    }


    public static void main(String [] args) {

    // Make frame
    GuiDemo4 f = new GuiDemo4();

    f.addWindowListener(
    new WindowAdapter() {
    public void windowClosing(WindowEvent e) {

    // This closes the window and terminates the
    // Java Virtual Machine in the event that the
    // Frame is closed by clicking on X.
    System.out.println("Exit via windowClosing.");
    System.exit(0);
    }
    }
    );
    } // end of main

    // inner class for button event handling
    private class ButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == myFirstButton) {
    System.out.println(
    "Left Button has been pressed.");
    }
    if (e.getSource() == mySecondButton) {
    System.out.println(
    "Right Button has been pressed.");
    }
    }
    } // end of inner class
    } // end of outer class

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I prefer to create JDialogs if I am trying to create secondary windows rather than new JFrames. The JDialogs behave better (imo) and can be easily set to modal or not. You create new JFrames or JDialogs in an action listener just as you currently do in your program -- there's no magic to it. For instance:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GuiDemo4 extends JFrame
    {
    
      private JButton myFirstButton;
      private JButton mySecondButton;
      private JButton button3;
    
      public GuiDemo4()
      {
    
        super("My First Button Program");
    
        myFirstButton = new JButton("PRESS LEFT");
        myFirstButton.setFont(new Font("Arial", Font.BOLD, 18));
        myFirstButton.setBackground(Color.red);
    
        mySecondButton = new JButton("PRESS RIGHT");
        mySecondButton.setFont(new Font("Arial", Font.BOLD, 18));
        mySecondButton.setBackground(Color.green);
        
        button3 = new JButton("New Window");
        button3.setFont(mySecondButton.getFont());
        button3.setBackground(new Color(180, 100, 255));
    
        Container c = getContentPane();
        FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
        c.setLayout(fl);
    
        c.add(myFirstButton);
        c.add(mySecondButton);
        c.add(button3);
    
        ButtonHandler handler = new ButtonHandler();
        myFirstButton.addActionListener(handler);
        mySecondButton.addActionListener(handler);
        button3.addActionListener(handler);
    
        setSize(400, 300);
        setVisible(true);
      }
    
      public static void main(String[] args)
      {
    
        GuiDemo4 f = new GuiDemo4();
    
        f.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.out.println("Exit via windowClosing.");
            System.exit(0);
          }
        });
      }
    
      private class ButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          if (e.getSource() == myFirstButton)
          {
            System.out.println("Left Button has been pressed.");
          }
          if (e.getSource() == mySecondButton)
          {
            System.out.println("Right Button has been pressed.");
          }
          if (e.getSource() == button3)
          {
            JPanel pane = new JPanel(new GridLayout(0, 3, 5, 5));
            for (int i = 0; i < 3; i++)
            {
              for (int j = 0; j < 3; j++)
              {
                final String btnString = "[" + i + ", " + j + "]";
                JButton btn = new JButton(btnString);
                btn.addActionListener(new ActionListener()
                {
                  public void actionPerformed(ActionEvent e)
                  {
                    System.out.println("button " + btnString + " pressed");
                  }
                });
                pane.add(btn);
              }
            }
            
            JDialog dialog = new JDialog(GuiDemo4.this, "My Dialog", true);
            dialog.getContentPane().add(pane);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
          }
        }
      }
    }

  3. #3
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default Gui demo

    thanks very much that has helped me out, but instead of the grid how do i get just a button in the new dialog box? thank you for your help.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    hm, delete the code that makes a grid to the JPanel, and instead add a single JButton. You'll probably not want to have the pane JPanel use GridLayout though.

    Have you gone are are you currently going through the Sun Swing tutorials? I highly recommend them.

  5. #5
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default Gui new frame

    Which bits do i need to edit in particular?? because when i try to add the new button i get a lot of error messages which makes me think i have deleted something that i shouldnt have. i have been going through some tutorials in a book i have but they do not explain it very well.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hmm, I think that for you to get any more from this forum, you are going to need to do more studying. Please reread the Sun Swing tutorial section on JPanels and buttons.

    If you have further questions, then lets see your code, your error messages and comments telling where the error is being thrown.

    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Luck.

  7. #7
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default gui new frame

    I have added the button but when i compile it says that it has run succesfully but it sill runs the old code, any help would be greatly appreciated.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GuiDemo4 extends JFrame
    {
    
      private JButton myFirstButton;
      private JButton mySecondButton;
    
    
      public GuiDemo4()
      {
    
        super("My First Button Program");
    
        myFirstButton = new JButton("PRESS LEFT");
        myFirstButton.setFont(new Font("Arial", Font.BOLD, 18));
        myFirstButton.setBackground(Color.red);
    
        mySecondButton = new JButton("PRESS RIGHT");
        mySecondButton.setFont(new Font("Arial", Font.BOLD, 18));
        mySecondButton.setBackground(Color.green);
    
    
        Container c = getContentPane();
        FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
        c.setLayout(fl);
    
        c.add(myFirstButton);
        c.add(mySecondButton);
    
    
        ButtonHandler handler = new ButtonHandler();
        myFirstButton.addActionListener(handler);
        mySecondButton.addActionListener(handler);
        button3.addActionListener(handler);
    
        setSize(400, 300);
        setVisible(true);
      }
    
      public static void main(String[] args)
      {
    
        GuiDemo4 f = new GuiDemo4();
    
        f.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.out.println("Exit via windowClosing.");
            System.exit(0);
          }
        });
      }
    
      private class ButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          if (e.getSource() == myFirstButton)
          {
            System.out.println("Left Button has been pressed.");
    
          }
          if (e.getSource() == mySecondButton)
          {
            JFrame pane = new JFrame(new JButton());
    
            myThirdButton = new JButton("PRESS RIGHT");
    		myThirdButton.setFont(new Font("Arial", Font.BOLD, 18));
        	myThirdButton.setBackground(Color.green);
    
    		{
    
    
                final String btnString = "[" + i + ", " + j + "]";
                JButton btn = new JButton(btnString);
                btn.addActionListener(new ActionListener()
                {
                  public void actionPerformed(ActionEvent e)
                  {
                    System.out.println("button " + btnString + " pressed");
                  }
                });
                pane.add(btn);
              }
            }
    
            JDialog dialog = new JDialog(GuiDemo4.this, "My Dialog", true);
            dialog.getContentPane().add(button);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
          }
        }
      }
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Some errors that are present:

    • button3 is being added an action listener, but is never declared, never used.
    • myThirdButton object is used but never declared, never placed into a root container (JFrame, JDialog)
    • A JFrame is being constructed with a JButton parameter (this constructor doesn't exist)
    • The JFrame is never rendered, never shown.
    • left-over code from my previous JDialog example is kept in the code without removing it.


    I recommend that you get rid of the current second button's actionPerformed code and recode it from scratch, getting rid of the debris from previous code. You really really need to study the tutorials. The things you are trying to do just won't fly until you gain the knowledge to be found there. Trust me, you won't regret this study.
    Last edited by Fubarable; 03-01-2009 at 06:46 PM.

  9. #9
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default gui new frame

    I have studied the tutorials and i have made another attempt i get one error though that reads:

    E:\Web site\GuiDemo4.java:78: cannot find symbol
    symbol : variable myFourthButton
    location: class GuiDemo4.ButtonHandler
    c.add(myFourthButton); }

    The new frame is created but the button does not appear. any help would again would be greatly appreciated
    ^
    1 error
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GuiDemo4 extends JFrame
    {
    
      private JButton myFirstButton;
      private JButton button3;
      private JButton myFourthbutton;
    
      public GuiDemo4()
      {
    
        super("My First Button Program");
    
        myFirstButton = new JButton("FIRST BUTTON");
        myFirstButton.setFont(new Font("Arial", Font.BOLD, 18));
        myFirstButton.setBackground(Color.red);
    
    
        button3 = new JButton("NEW FRAME");
        button3.setFont(myFirstButton.getFont());
        button3.setBackground(new Color(180, 100, 255));
    
        Container c = getContentPane();
        FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
        c.setLayout(fl);
    
        c.add(myFirstButton);
        c.add(button3);
    
        ButtonHandler handler = new ButtonHandler();
        myFirstButton.addActionListener(handler);
        button3.addActionListener(handler);
    
        setSize(400, 300);
        setVisible(true);
      }
    
      public static void main(String[] args)
      {
    
        GuiDemo4 f = new GuiDemo4();
    
        f.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.out.println("Exit via windowClosing.");
            System.exit(0);
          }
        });
      }
    
      private class ButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          if (e.getSource() == myFirstButton)
          {
            System.out.println("Left Button has been pressed.");
          }
    
    
          if (e.getSource() == button3)
          {
            JFrame frame = new JFrame("New Frame");
    
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   frame.setLocationRelativeTo(null);
    	   frame.setVisible(true);
    
    			Container c = getContentPane();
    		    FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
    		    c.setLayout(fl);
    
    		    c.add(myFourthButton);	}
    
    
    		    ButtonHandler handler = new ButtonHandler();
    		    myFirstButton.addActionListener(handler);
    		    button3.addActionListener(handler);
    
    		    setSize(400, 300);
    		    setVisible(true);
    
    			}
    
    
    		 }
    
    	}

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Java is case sensitive, so cApItAlIzAtIoN matters. Check out the button variable closely and you'll see what I'm referring to. Good luck.

    edit:

    you'll also need to initialize this 4th button variable before you use it just as you would any JButton / any object. Also make sure that you're adding an actionlistener to the right button (look at the place where you create your new JFrame).

    Also, I again recommend that you not create a JFrame here but instead a JDialog unless there's a pressing reason to create a JFrame. If so, please post that reason so I can stop mentioning this.

    edit: also, you'll need to check to make sure that you're adding a new button to the correct container. I'm guessing that you want to add it to the contentPane of the new Window that you are creating, not the old window that launches the new window....

    edit: and, you'll not want to set the new window visible until after you've added components such as JButtons. This is well spelled out in the tutorials
    Last edited by Fubarable; 03-01-2009 at 07:57 PM. Reason: More bugs discovered

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please see the multiple comments above.

  12. #12
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    I do apologise in advance if this sounds a bit cheeky below is another attempt of mine that is slightly unsuccesfull as i am printing the new button in the same frame and i dont know how to correct, as this is just a hobbie of mine i would be greatful if you could post the solution as it has been niggling at me all day. thanks for your support.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GuiDemo4 extends JFrame
    {
    
      private JButton myFirstButton;
      private JButton button3;
      private JButton myFourthbutton;
    
      public GuiDemo4()
      {
    
        super("My First Button Program");
    
        myFirstButton = new JButton("FIRST BUTTON");
        myFirstButton.setFont(new Font("Arial", Font.BOLD, 18));
        myFirstButton.setBackground(Color.red);
    
    
        button3 = new JButton("NEW FRAME");
        button3.setFont(myFirstButton.getFont());
        button3.setBackground(new Color(180, 100, 255));
    
        Container c = getContentPane();
        FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
        c.setLayout(fl);
    
        c.add(myFirstButton);
        c.add(button3);
    
        ButtonHandler handler = new ButtonHandler();
        myFirstButton.addActionListener(handler);
        button3.addActionListener(handler);
    
    
    
        setSize(400, 300);
        setVisible(true);
      }
    
      public static void main(String[] args)
      {
    
        GuiDemo4 f = new GuiDemo4();
    
        f.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.out.println("Exit via windowClosing.");
            System.exit(0);
          }
        });
      }
    
      private class ButtonHandler implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          if (e.getSource() == myFirstButton)
          {
            System.out.println("Left Button has been pressed.");
          }
    
    
          if (e.getSource() == button3)
          {
            JFrame frame = new JFrame("New Frame");
    
           frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
    	   frame.setLocationRelativeTo(null);
    	   frame.setVisible(true);
    
    		myFourthbutton = new JButton ("Fourth");
    
    		myFourthbutton.setFont(myFirstButton.getFont());
        	myFourthbutton.setBackground(new Color(180, 100, 255));
    
    			Container c = getContentPane();
    		    FlowLayout f1 = new FlowLayout(FlowLayout.LEFT);
    		    c.setLayout(f1);
    
    
    
    		    c.add(myFourthbutton);	}
    
    
    		    ButtonHandler handler = new ButtonHandler();
    		    myFirstButton.addActionListener(handler);
    		    button3.addActionListener(handler);
    		    myFourthbutton.addActionListener(handler);
    
    		    setSize(400, 300);
    		    setVisible(true);
    
    		}
    
    
    	}
    
    }

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It compiles, and that's good, and it shows the fourth button on the new JFrame, also good. What's not working. BTW, this is a hobby of mine too. It is fun, but takes a lot of work.

  14. #14
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    thats strange because when i run it the fourth button appears on the original frame below the "first button" and "new frame".. i know it does take a lot of work lol :-D another query is how do i make the second frame a bigger size than it is at the minute. thanks again, you have really been a big help today

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    read the tutorial on using layout managers in the Swing tutorials. There you'll see that most layout managers use the preferred size when trying to set the size of a component.

    So I sometimes set the preferred size of key components in my display, sometimes the main JPanel or contentPane. Be sure to call "pack()" on the JFrame or JDialog before calling setVisible(true) as this will then tell the layout managers of the app to layout the components properly.

  16. #16
    david adam is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default

    HEY IAM NEW TO JAVA WOULD KNOW HOW TO CREATE THIS PROGRAM AS A START TO THE END, PLEASE HELP ME.

    Import java.util.Scanner;

    public class triangles
    {
    public static void main(String [] args


    1. Suppose we define a class Isosceles, which can represent an isosceles triangle whose base is aligned with the horizontal directions of the display, using four parameters: x, y, b, h, where (x,y) are the coordinates of the center of the base, b is the length of the base, and h is the height. Suppose we define the relationship "C contains A" to indicate that all of the area or perimeter of Isosceles A is in or on the area or perimeter of Isosceles C.

Similar Threads

  1. frame
    By arunkumarinfo in forum NetBeans
    Replies: 0
    Last Post: 02-07-2009, 10:26 AM
  2. Getting internal frame out
    By rad in forum New To Java
    Replies: 8
    Last Post: 08-11-2008, 02:33 AM
  3. Frame to other Frame
    By Aswq in forum New To Java
    Replies: 2
    Last Post: 07-19-2008, 04:27 PM
  4. close a frame..
    By tajinvillage in forum New To Java
    Replies: 5
    Last Post: 04-27-2008, 10:22 PM
  5. Frame Query
    By Daniel in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 06:27 PM

Posting Permissions

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