jframe与wait的黑屏问题

纯种直男 2018-03-03 02:53:05
求教各位大佬,我的程序从test1自己启动,test1的界面是正常出来的,按钮也好使,但是从start里面调用,test1直接是黑屏的


test1里启动


从start里调用test1

我想要能从外部(start)通过按钮调用test1,test1中使用按钮来切换test1的一些布置,使用wait是想让线程停下等待事件的触发

test1

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();
}

}
}

}



start

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();
}
}
}



求教啊
...全文
621 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgqjava 2018-03-05
  • 打赏
  • 举报
回复
你在actionPerformed中调用new Test1, 在Test1的构造方法中执行了first 在first中wait 也就是wait的线程是当前执行actionPerformed方法的线程 而这个线程是Swing中的EDT(事件派发线程) 这个线程负责所有Swing的组件绘制事件派发等工作 不允许执行长耗时的操作 更别说wait了 你wait了之后整个Swing程序就停止重绘并且失去响应了 你点击任何按钮都不会得到响应 因为这个线程被挂起了 黑屏就是这个原因 你可以在actionPerformed方法里 新建一个线程异步调用new Test1
weixin_40066981 2018-03-04
  • 打赏
  • 举报
回复
做javaee的话就不用关注这个了吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧