import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.*;
public class MySwing extends JFrame implements ActionListener {
JButton jb = new JButton();
public MySwing() {
this.setTitle("MySwing");
this.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jb.setText("确定");
this.add(jb);
this.setBounds(200, 300, 250, 300);
this.setVisible(true);
//弹出新窗口
jb.addActionListener(this);
}
//jb 事件绑定
public void actionPerformed(ActionEvent e) {
//创建新的窗口
JFrame frame = new JFrame("新窗口");
frame.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
frame.setBounds(
new Rectangle(
(int) this.getBounds().getX() + 200,
(int) this.getBounds().getY() + 200,
(int) this.getBounds().getWidth(),
(int) this.getBounds().getHeight()
)
);
JLabel jl = new JLabel();
frame.getContentPane().add(jl);
jl.setText("这是新窗口");
JButton button = new JButton("回调");
frame.getContentPane().add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//调用原窗口组件
jb.setText("haha");
}
});
frame.setVisible(true);
}
public static void main(String args[]) {
MySwing s = new MySwing();
}
}