java:JFrame中点击一个按钮无法显示带线程的JDialog 10
继承JDialog,点击按钮却无法显示,把JDialog里的线程去掉就可以显示了,用Timer也可以显示,求问为什么。。代码如下:importjava.awt.Color...
继承JDialog,点击按钮却无法显示,把JDialog里的线程去掉就可以显示了,用Timer也可以显示,求问为什么。。代码如下:
import java.awt.Color;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;import utility.Fade;public class MessageDialog extends JDialog { /** * */ private static final long serialVersionUID = -5033377497704206134L; private JPanel panel; private JLabel label; private String message; private Color bgColor = Color.WHITE;//背景色 private int showTime = 2000;//停留时间 private JDialog instance = this; private int time = 0; public MessageDialog(){ this(null); } public MessageDialog(String message){ this(null, message); } public MessageDialog(JFrame frameParent,String message){ super(frameParent); this.message = message; setPanel(); init(); Fade.fadeIn(this); setTime(); } private void init(){ this.add(panel); this.setSize(220, 80); this.setLocationRelativeTo(getOwner()); this.setResizable(false); this.setUndecorated(true); this.setVisible(true); } private void setPanel(){ panel = new JPanel(); this.panel.setLayout(null); this.panel.setBackground(bgColor); label = new JLabel(message); label.setFont(new Font("microsoft yahei",Font.BOLD,14)); label.setBounds(50, 25, 120, 30); this.panel.add(label); } private void setTime(){//为什么线程不行??!! Thread thread = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(showTime); Fade.fadeOut(instance); instance.dispose(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.run(); // Timer timer = new Timer(1000, new ActionListener() {//// @Override// public void actionPerformed(ActionEvent arg0) {// // TODO Auto-generated method stub// time+=1000;// if(time==showTime){// Fade.fadeOut(instance);// instance.dispose();// }// }// });// timer.start(); }} 展开
import java.awt.Color;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.Timer;import utility.Fade;public class MessageDialog extends JDialog { /** * */ private static final long serialVersionUID = -5033377497704206134L; private JPanel panel; private JLabel label; private String message; private Color bgColor = Color.WHITE;//背景色 private int showTime = 2000;//停留时间 private JDialog instance = this; private int time = 0; public MessageDialog(){ this(null); } public MessageDialog(String message){ this(null, message); } public MessageDialog(JFrame frameParent,String message){ super(frameParent); this.message = message; setPanel(); init(); Fade.fadeIn(this); setTime(); } private void init(){ this.add(panel); this.setSize(220, 80); this.setLocationRelativeTo(getOwner()); this.setResizable(false); this.setUndecorated(true); this.setVisible(true); } private void setPanel(){ panel = new JPanel(); this.panel.setLayout(null); this.panel.setBackground(bgColor); label = new JLabel(message); label.setFont(new Font("microsoft yahei",Font.BOLD,14)); label.setBounds(50, 25, 120, 30); this.panel.add(label); } private void setTime(){//为什么线程不行??!! Thread thread = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(showTime); Fade.fadeOut(instance); instance.dispose(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.run(); // Timer timer = new Timer(1000, new ActionListener() {//// @Override// public void actionPerformed(ActionEvent arg0) {// // TODO Auto-generated method stub// time+=1000;// if(time==showTime){// Fade.fadeOut(instance);// instance.dispose();// }// }// });// timer.start(); }} 展开
展开全部
会不会是同步异步,或者线程锁之类的原因呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不是的Jdialog显示时,会自动线程暂停的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-19
展开全部
不执行 Fade.fadeOut(instance);,很正常,闪一下Dialog才消失。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询