Re: Ping Java Peeps
on Wednesday 12 September 2007 03:17 am, someone posing as RedGrittyBrick
took a rock and etched into the cave:
In frame2.java change
public frame2(){
to
public frame2(ActionListener listener){
change
button2.addActionListener(this);
to
button2.addActionListener(listener);
Above coding style based on that used in OP's source :-(
UNTESTED - CAVEAT EMPTOR
There's probably better ways to do it and there's lots of other
improvements I think you could and should make to your code. For
example, you could pass an Action instead of an ActionListener, this
would make the code a bit cleaner. I hope the above helps you get started.
Thank you - I did this and ended up with something bizarre.
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();
public static void main(String args[]){
new frame1();
}
public frame1(){
frametwo.setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JButton button1 = new JButton("Click Me");
button1.addActionListener(this);
JLabel lblOne = new JLabel(" ");
lblOne.setBorder(new LineBorder(Color.green, 3));
content.add(lblOne);
content.add(button1);
JButton button2 = new JButton("Exit");
button2.addActionListener( this );
content.add(button2);
this.setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Exit")) {
System.exit(0);
}
if (e.getActionCommand().equals("Click Me")) {
frametwo.setVisible(true);
}
}
public frame1 getMainForm() {
return mainForm;
}
}
------------------------------------------------------------
frame2.java
/*
Java test for multi-class update.
This is the second frame called from the first.
*/
import javax.swing.*;
import java.awt.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class frame2 extends JFrame implements ActionListener{
JTextField txtOne = new JTextField(20);
static ActionListener listener; // = new ActionListener();
public static void main(String args[]){
new frame2( listener );
}
public frame2( ActionListener listener ){
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Container content = getContentPane();
content.setLayout(new FlowLayout());
content.add(txtOne);
JButton button2 = new JButton("Close");
//button2.addActionListener(new ButtonListener());
//button2.addActionListener(this);
button2.addActionListener(listener);
content.add(button2);
this.setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
//if (e.getActionCommand().equals("Close")) {
String blah = this.txtOne.getText();
System.out.println(blah);
// frame1.lblOne.setText(blah);
setVisible(false);
}
/* public Form1 getFrame1(){
return this.frame1;
}
public void setFrame1(Form1 frame1){
this.frame1 = frame1;
}
*/
}
--
www.perfectreign.com