| | |
Problem with very basic homework... (It's done, but have one bug I can't figure out.)
![]() |
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
Problem with very basic homework... (It's done, but have one bug I can't figure out.)
0
#1 Sep 11th, 2006
Hello all,
I've got an assignment due on thursday that is very easy, but I have one bug that is driving me nuts.
The assigment is to just create a basic GUI with 1 label, a text box, and a few check boxes, so it's not that hard. The problem is that it compiles fine with no errors or anything, but when I run it, nothing is showing up in the window unless I use the mouse to click the lower right corner of the window and resize it. Then the items show up.
It has been about 2 years since I've taken CS I (Got all the other stuff out of the way :mrgreen: ) but now, I'm a little rusty...
Here is my code:
Thank you in advance for any help. This but is driving me nuts. I have even shown it to a couple of the TA's in the CS lab and they are a bit baffled by it.
Thank you,
Paul Allen (Antiparadigm)
I've got an assignment due on thursday that is very easy, but I have one bug that is driving me nuts.
The assigment is to just create a basic GUI with 1 label, a text box, and a few check boxes, so it's not that hard. The problem is that it compiles fine with no errors or anything, but when I run it, nothing is showing up in the window unless I use the mouse to click the lower right corner of the window and resize it. Then the items show up.
It has been about 2 years since I've taken CS I (Got all the other stuff out of the way :mrgreen: ) but now, I'm a little rusty...
Here is my code:
Java Syntax (Toggle Plain Text)
/* * Tiny App * */ import javax.swing.*; import java.awt.*; public class TinyApp { public static void main(String[] args) { // Set-up JFrame JFrame jfWindows = new JFrame("Search Box"); jfWindows.setSize(400, 150); jfWindows.setVisible(true); // Set up Container for controls Container cp = jfWindows.getContentPane(); cp.setLayout(new BorderLayout()); // Pane to hold the search box and label JPanel jpSearch = new JPanel(); jpSearch.setLayout(new FlowLayout(FlowLayout.CENTER)); cp.add(jpSearch, BorderLayout.NORTH); // Search label JLabel lblSearch = new JLabel("Enter key phrase: "); jpSearch.add(lblSearch); // Search text box JTextField jtfSearch = new JTextField(20); jpSearch.add(jtfSearch); // Bottom Panel JPanel jpYear = new JPanel(); jpYear.setLayout(new FlowLayout(FlowLayout.CENTER)); cp.add(jpYear, BorderLayout.SOUTH); // Create Year Checkboxes JCheckBox jcb1998 = new JCheckBox("1998"); jpYear.add(jcb1998); JCheckBox jcb1999 = new JCheckBox("1999"); jpYear.add(jcb1999); JCheckBox jcb2000 = new JCheckBox("2000"); jpYear.add(jcb2000); JCheckBox jcb2001 = new JCheckBox("2001"); jpYear.add(jcb2001); } }
Thank you,
Paul Allen (Antiparadigm)
Re: Problem with very basic homework... (It's done, but have one bug I can't figure out.)
0
#2 Sep 11th, 2006
After you have finished adding child components (ie the end of the program) you need to add the following:
I would also suggest adding:
The default is HIDE_ON_CLOSE, which leaves the process running until you explicitly exit the IDE/Runtime Environment.
Java Syntax (Toggle Plain Text)
jfWindows.pack();
Java Syntax (Toggle Plain Text)
jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);
Last edited by DavidRyan; Sep 11th, 2006 at 9:24 pm.
Please anyone, correct me if I am wrong. It is the best way for me to learn.
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
Re: Problem with very basic homework... (It's done, but have one bug I can't figure out.)
0
#3 Sep 11th, 2006
You could also try adding all your components and THEN showing the JFrame.
Java Syntax (Toggle Plain Text)
JFrame jfWindows = new JFrame("Search Box"); ... //add components here... ... jfWindows.setVisible(true);
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
Re: Problem with very basic homework... (It's done, but have one bug I can't figure o
0
#4 Sep 11th, 2006
That was right on DavidRyan.
The line:
Has to be implemented in an action listener right?
The line:
Java Syntax (Toggle Plain Text)
jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);
Last edited by Antiparadigm; Sep 11th, 2006 at 11:18 pm.
Re: Problem with very basic homework... (It's done, but have one bug I can't figure o
0
#5 Sep 12th, 2006
•
•
•
•
The line:
Has to be implemented in an action listener right?Java Syntax (Toggle Plain Text)
jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
Re: Problem with very basic homework... (It's done, but have one bug I can't figure out.)
0
#6 Sep 12th, 2006
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
Re: Problem with very basic homework... (It's done, but have one bug I can't figure o
0
#7 Sep 12th, 2006
Thank you both your help really staightend me out.
Just one quick note:
was having problems with EXIT_ON_CLOSE. After a little research, I found I had to use:
Again, Thank you so much for your help!
Paul Allen (antiparadigm)
Just one quick note:
Java Syntax (Toggle Plain Text)
jfWindows.setDefaultCloseOperation(EXIT_ON_CLOSE);
Java Syntax (Toggle Plain Text)
jfWindows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Paul Allen (antiparadigm)
![]() |
Similar Threads
- date format problem with access and visual basic (Windows NT / 2000 / XP)
- C++ char array parsing-Help (C++)
- Problem with basic templates on GCC (C++)
Other Threads in the Java Forum
- Previous Thread: KoC Recruiting
- Next Thread: New 2 JAVA
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






