Results 1 to 14 of 14
  1. #1
    glomic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default need help with jframes

    hello all,

    i been working on setting up the basic bones for my program and i started with the buttons, what i am trying to do is go from one frame to another and then another, the first two work, but not the second, and help is approached, code is shown below

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

    public class name extends JFrame{
    JLabel label=new JLabel();
    JLabel menu=new JLabel();
    JTextField text=new JTextField(20);
    JPanel panel=new JPanel(new GridLayout(2,2));
    JButton button1 =new JButton("Help");
    JButton button2 =new JButton("Next");
    JButton button3 =new JButton("window 2");


    public name(){
    label.setText("Enter Name:");
    panel.add(label);
    panel.add(text);
    panel.add(button1);
    panel.add(button2);



    add(panel,BorderLayout.CENTER);
    button1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent mh){
    JFrame frame2 = new JFrame("Message Dig");
    JOptionPane.showMessageDialog(frame2, "you will need to type your name into the text box\n"
    +"you can not use numbers and you can only have up\n"
    +"to 20 letters for your name");
    }
    }
    )
    ;
    //start of window 2
    button2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent zzz){
    JLabel menu=new JLabel();
    menu.setText("main menu");
    JFrame frame3 = new JFrame("main menu");
    JButton button3 =new JButton("window 2");
    JLabel label3=new JLabel("test");
    String value=text.getText();
    JPanel panela=new JPanel();
    panela.add(button3);
    JFrame frame1 = new JFrame();
    frame1.setVisible(true);
    frame1.add(panela);
    frame1.setSize(250,100);
    //start of window 3

    }
    }
    )
    ;
    button3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent a){
    JFrame frame4 = new JFrame("sums");
    JButton button7 =new JButton("add");
    String value=text.getText();
    JPanel panal=new JPanel();
    panal.add(button7);
    JFrame frame1 = new JFrame();
    frame1.setVisible(true);
    frame1.add(panal);
    frame1.setSize(250,100);
    }
    }
    )
    ;

    }
    public static void main(String arg[]){
    name frame=new name();
    frame.setSize(300,100);
    frame.setVisible(true);

    }

    }


    I am new to java so any help will be appreached.
    and as another note, i am using bluej

    Michael glover

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    When posting code, make sure you use the CODE tags. There are plenty of examples of how to do this in others' posts.

    But what do you mean by "doesn't work"? Does it throw an Exception? Does it not display at all? Does it display something unexpected? Be specific.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Reposting code with code tags for readability.

    OP - please use the code tag button to surround your code next time, it will make your code more readable/understandable to whoever is helping you.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class name extends JFrame {
    	JLabel label = new JLabel();
    	JLabel menu = new JLabel();
    	JTextField text = new JTextField(20);
    	JPanel panel = new JPanel(new GridLayout(2, 2));
    	JButton button1 = new JButton("Help");
    	JButton button2 = new JButton("Next");
    	JButton button3 = new JButton("window 2");
    
    	public name() {
    		label.setText("Enter Name:");
    		panel.add(label);
    		panel.add(text);
    		panel.add(button1);
    		panel.add(button2);
    
    		add(panel, BorderLayout.CENTER);
    		button1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent mh) {
    				JFrame frame2 = new JFrame("Message Dig");
    				JOptionPane
    						.showMessageDialog(
    								frame2,
    								"you will need to type your name into the text box\n"
    										+ "you can not use numbers and you can only have up\n"
    										+ "to 20 letters for your name");
    			}
    		});
    		// start of window 2
    		button2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent zzz) {
    				JLabel menu = new JLabel();
    				menu.setText("main menu");
    				JFrame frame3 = new JFrame("main menu");
    				JButton button3 = new JButton("window 2");
    				JLabel label3 = new JLabel("test");
    				String value = text.getText();
    				JPanel panela = new JPanel();
    				panela.add(button3);
    				JFrame frame1 = new JFrame();
    				frame1.setVisible(true);
    				frame1.add(panela);
    				frame1.setSize(250, 100);
    				// start of window 3
    
    			}
    		});
    		button3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent a) {
    				JFrame frame4 = new JFrame("sums");
    				JButton button7 = new JButton("add");
    				String value = text.getText();
    				JPanel panal = new JPanel();
    				panal.add(button7);
    				JFrame frame1 = new JFrame();
    				frame1.setVisible(true);
    				frame1.add(panal);
    				frame1.setSize(250, 100);
    			}
    		});
    
    	}
    
    	public static void main(String arg[]) {
    		name frame = new name();
    		frame.setSize(300, 100);
    		frame.setVisible(true);
    
    	}
    
    }
    Best,
    --user0--

  4. #4
    glomic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    thank you, i new to the forums, and when i mean by it does not work, i mean the the program runs and when i press the next button it takes me to the second window but when i press the button on the second window i do not go to the third one.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    When do you call setVisible() on that third JFrame?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    glomic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    i not quite sure what you mean, can you explane it please?

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    To make a JFrame visible, you have to call its setVisible() method. When do you do this for the JFrame that is not becoming visible?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    glomic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    i want to call it when i press the button on the second frame, like how i call the second frame with a button on the first one.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by glomic View Post
    i want to call it when i press the button on the second frame, like how i call the second frame with a button on the first one.
    ...so why don't you?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    glomic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    because i have no idea how you set that up, i found most of the code on the internet, I copyed the code for the second window and changed the buttons and added new ones, but that does not seam to work, and all our teachers say is go ask on a forum

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Quote Originally Posted by glomic View Post
    because i have no idea how you set that up, i found most of the code on the internet, I copyed the code for the second window and changed the buttons and added new ones, but that does not seam to work, and all our teachers say is go ask on a forum
    I see. Generally, simply copying and pasting code without understanding it is a pretty horrible way to go about solving a problem.

    But take a look at the lines you use to set up and show your first JFrame. Take a look at the API to figure out what each one is doing. Read through the Swing tutorials.

    Then look at the code you're using (or not using) to show the third JFrame. Is anything missing?

    The API: Java Platform SE 6
    The Swing Tutorial: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    glomic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    is it the
    JFrame frame2 = new JFrame

    part of the first frame that lets the second one come up, i not too sure the links you gave me sent me to the libarys, so I not sure which one you want me to look at.

    sorry to sound like i being a real pain, but the truth is our teacher just sets the work and that's it, he some some basic stuff but it looked at this and just said, go ask on a forum

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

    Default

    The truth is you must read the tutorials linked to above and understand what you're trying to do. This has nothing to do with your teacher as you are ultimately the one responsible for your eductation. So read the tutorials, and then come back if anything is not understandable. Luck.

  14. #14
    glomic is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    ya i got it working, just had to move the }}); from under window 2 to under window 3 and that now means the frame shows

Similar Threads

  1. Can't have two JFrames in different classes?
    By grizdog in forum AWT / Swing
    Replies: 9
    Last Post: 12-14-2010, 03:25 AM
  2. help with jframes
    By pao09 in forum AWT / Swing
    Replies: 0
    Last Post: 04-15-2009, 02:10 PM
  3. jframes
    By ddj in forum AWT / Swing
    Replies: 0
    Last Post: 03-24-2009, 03:15 PM
  4. two JFrames
    By kirtesh4u in forum New To Java
    Replies: 0
    Last Post: 11-17-2008, 08:26 PM
  5. Handling Two JFrames
    By hiranya in forum AWT / Swing
    Replies: 2
    Last Post: 11-05-2007, 07:23 AM

Posting Permissions

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