给你一个例子:/*
* @(#) TestJFrame.java
* Created on 2004-10-19
* By James Fancy
*/
package jamesfancy;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;/**
* TestJFrame
* @author James
*/
public class TestJFrame extends JFrame implements ActionListener {
Controler controler;
JTextField field;
JButton button; public TestJFrame() {
controler = new Controler(this);
field = new JTextField(15);
button = new JButton("Open Controler");
button.addActionListener(this);
getContentPane().add(field);
getContentPane().add(button, BorderLayout.SOUTH);
pack();
} public static void main(String[] args) {
TestJFrame frame = new TestJFrame();
frame.setLocation(300, 200);
frame.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
controler.setLocation(400, 300);
controler.setVisible(true);
}
}
public void setText(String text) {
field.setText(text);
}
}class Controler extends JFrame implements ActionListener { TestJFrame caller;
JButton button; public Controler(TestJFrame caller) {
this.caller = caller;
button = new JButton("Set Current Time");
button.addActionListener(this);
getContentPane().add(button);
pack();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
caller.setText(format.format(new Date()));
}
}}
* @(#) TestJFrame.java
* Created on 2004-10-19
* By James Fancy
*/
package jamesfancy;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;/**
* TestJFrame
* @author James
*/
public class TestJFrame extends JFrame implements ActionListener {
Controler controler;
JTextField field;
JButton button; public TestJFrame() {
controler = new Controler(this);
field = new JTextField(15);
button = new JButton("Open Controler");
button.addActionListener(this);
getContentPane().add(field);
getContentPane().add(button, BorderLayout.SOUTH);
pack();
} public static void main(String[] args) {
TestJFrame frame = new TestJFrame();
frame.setLocation(300, 200);
frame.setVisible(true);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
controler.setLocation(400, 300);
controler.setVisible(true);
}
}
public void setText(String text) {
field.setText(text);
}
}class Controler extends JFrame implements ActionListener { TestJFrame caller;
JButton button; public Controler(TestJFrame caller) {
this.caller = caller;
button = new JButton("Set Current Time");
button.addActionListener(this);
getContentPane().add(button);
pack();
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
caller.setText(format.format(new Date()));
}
}}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货