62,614
社区成员
发帖
与我相关
我的任务
分享
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test1 extends JFrame implements ActionListener {
JPanel jPanel;
JButton jButton;
public static void main(String[] args) {
new Test1();
}
public Test1() {
this.setTitle("测试1");
this.setVisible(true);
this.setSize(500, 500);
this.setLocation(500, 300);
//this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);// 关闭窗口
this.setDefaultCloseOperation(EXIT_ON_CLOSE);// 关闭窗口且关闭程序
this.setResizable(false);
this.setLayout(null);// 空布局
// for(int i=0;i<3;i++){
// first();
// this.remove(jPanel);
// }
//
// for(int i=0;i<3;i++){
// second();
// this.remove(jPanel);
// }
//
first();
this.remove(jPanel);
second();
this.remove(jPanel);
first();
this.remove(jPanel);
second();
// this.remove(jPanel);
}
public void first() {
System.out.println("first");
jPanel = new JPanel();
jPanel.setLayout(null);
this.add(jPanel);
jPanel.setBounds(0, 0, 500, 500);
jPanel.setBackground(Color.RED);
jButton = new JButton("next");
jPanel.add(jButton);
jButton.setBounds(150, 200, 200, 50);
jButton.addActionListener(this);
synchronized(this){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
synchronized public void second() {
System.out.println("second");
jPanel = new JPanel();
jPanel.setLayout(null);
this.add(jPanel);
jPanel.setBounds(0, 0, 500, 500);
jPanel.setBackground(Color.yellow);
jButton = new JButton("next");
jPanel.add(jButton);
jButton.setBounds(150, 200, 200, 50);
jButton.addActionListener(this);
synchronized(this){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jButton){
System.out.println("下一个");
synchronized (this) {
this.notify();
}
}
}
}
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Start extends JFrame implements ActionListener {
JButton jButton;
public static void main(String[] args) {
new Start();
}
public Start() {
this.setTitle("1");
this.setVisible(true);
this.setSize(500, 500);
this.setLocation(500, 300);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);// 关闭窗口
this.setResizable(false);
this.setLayout(new BorderLayout());
this.setLayout(null);// 空布局
jButton = new JButton("111");
this.add(jButton);
jButton.setBounds(100, 200, 100, 50);
jButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jButton) {
dispose();
new Test1();
}
}
}