Results 1 to 14 of 14
Thread: need help with jframes
- 01-07-2011, 05:17 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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
- 01-07-2011, 05:38 PM #2
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!
- 01-07-2011, 05:41 PM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
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.
Best,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); } }--user0--
- 01-07-2011, 05:58 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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.
- 01-07-2011, 06:11 PM #5
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!
- 01-07-2011, 06:17 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
i not quite sure what you mean, can you explane it please?
- 01-07-2011, 06:23 PM #7
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!
- 01-07-2011, 06:57 PM #8
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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.
- 01-07-2011, 07:03 PM #9
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-07-2011, 07:20 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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
- 01-07-2011, 07:25 PM #11
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!
- 01-07-2011, 08:06 PM #12
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
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
-
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.
- 01-08-2011, 11:16 AM #14
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Can't have two JFrames in different classes?
By grizdog in forum AWT / SwingReplies: 9Last Post: 12-14-2010, 03:25 AM -
help with jframes
By pao09 in forum AWT / SwingReplies: 0Last Post: 04-15-2009, 02:10 PM -
jframes
By ddj in forum AWT / SwingReplies: 0Last Post: 03-24-2009, 03:15 PM -
two JFrames
By kirtesh4u in forum New To JavaReplies: 0Last Post: 11-17-2008, 08:26 PM -
Handling Two JFrames
By hiranya in forum AWT / SwingReplies: 2Last Post: 11-05-2007, 07:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks