Re: Ping Java Peeps
on Friday 14 September 2007 02:47 am, someone posing as RedGrittyBrick took
a rock and etched into the cave:
PerfectReign wrote:
http://donutmonster.com/stuff/2007/20070913_java_frames.jpg
The first frame is loaded twice.
Of course, then the button on the second frame doesn't seem to work.
Ideas??
I'll post the code inline, since the classes are not that long.
frame1.java
import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class frame1 extends JFrame implements ActionListener{
frame2 frametwo = new frame2(this);
private static frame1 mainForm = new frame1();
In the above line you create your first instance of class frame1. You
don't need mainForm.
Remove the above line
Yep! I eventually figured that one out.
(Not bad for a manager, IMO!)
<sniP>
if (e.getActionCommand().equals("Click Me")) {
frametwo.setVisible(true);
}
Missing:
if (e.getActionCommand().equals("Close")) {
lblOne.setText(((JTextField)getSource).getText());
}
}
Okay, will add and see how it goes.
Your IDE should tell you the following method is never used
I'm not using an IDE. Just the text editor, Kate. It has java syntax
highlighting.
Remove the following method ...
Okay, will do.
<snip>
Other notes.
When posting code, just remove any commented out code.
Okay, will do. My apologies.
Please follow usual Java conventions when writing code for posting. It
makes it easier for people like me to speed-read code if your classes
have names starting with a capital letter. "frame1" looks like an
instance name. I'd write "Frame1" instead.
Will do. I realize now my class names don't follow Java conventions.
Generally when comparing a String variable to a String constant it is
usually safer (from NPE) to write it the other way around:
if ("Close".equals(e.getActionCommand()))
It probably doesn't matter, but if I intend to use the results of
e.getActionCommand() more than once, I always do
String command = e.getActionCommand() and use the command variable.
It is useful to create contants to be used as ActionCommands. There is
less chance of having "Close" in one place and "close" in another.
1)
public static final String CLOSE = "Close";
...
new JButton(CLOSE);
...
if (CLOSE.equals(command)) { ...
2) Better
Use an enum
3) Best?
Use Actions
I know its a concocted example but I find variable names like lblone and
blah make comprehension more difficult. I'd invent something based on
some typical teaching example like recording names and addresses, or
students and courses, or shops and products, ...
Hope that helps
Yes, it does. Thank you. (Actually lblOne is designed to show it is a
label. I always use the prefix in my variable in my real coding.)
--
www.perfectreign.com