为啥这个java程序不能正常工作?
Posted
技术标签:
【中文标题】为啥这个java程序不能正常工作?【英文标题】:Why this java program not working properly?为什么这个java程序不能正常工作? 【发布时间】:2020-09-19 00:28:36 【问题描述】:我是 java 新手,正如你在 java swing 中看到的这个程序由一个 jframe
和一个 jpanel
组成。当您单击一个按钮时,它的颜色会变为红色并自动按下按钮 1,其颜色会变为蓝色。
但问题是我希望这些操作之间有延迟,当我使用 Thread.sleep(1000)
时,它会导致按下按钮和更改其颜色之间的延迟,我不希望这样。
当我按下按钮 2(例如)它的颜色立即改变并且在按下按钮 1 1 秒后,我该怎么做?
jframe:
public class NewJFrame extends javax.swing.JFrame
private String player = "Player 1";
private JButton[] btn = new JButton[4];
public NewJFrame()
initComponents();
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
newJPanel1 = new NewJPanel();
newJPanel2 = new NewJPanel();
btn1 = new javax.swing.JButton();
btn2 = new javax.swing.JButton();
btn3 = new javax.swing.JButton();
javax.swing.GroupLayout newJPanel1Layout = new javax.swing.GroupLayout(newJPanel1);
newJPanel1.setLayout(newJPanel1Layout);
newJPanel1Layout.setHorizontalGroup(
newJPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
newJPanel1Layout.setVerticalGroup(
newJPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
newJPanel2.setBackground(new java.awt.Color(255, 255, 255));
newJPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
btn1.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(java.awt.event.ActionEvent evt)
btnAction(evt);
);
btn2.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(java.awt.event.ActionEvent evt)
btnAction(evt);
);
btn3.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(java.awt.event.ActionEvent evt)
btnAction(evt);
);
javax.swing.GroupLayout newJPanel2Layout = new javax.swing.GroupLayout(newJPanel2);
newJPanel2.setLayout(newJPanel2Layout);
newJPanel2Layout.setHorizontalGroup(
newJPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(newJPanel2Layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(btn1)
.addGap(74, 74, 74)
.addComponent(btn2)
.addGap(66, 66, 66)
.addComponent(btn3)
.addContainerGap(104, Short.MAX_VALUE))
);
newJPanel2Layout.setVerticalGroup(
newJPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(newJPanel2Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(newJPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(222, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(newJPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(newJPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
// </editor-fold>
private void btnAction(java.awt.event.ActionEvent evt)
JButton btn = (JButton) evt.getSource();
if (player.equals("Player 1"))
player = "Player 2";
btn.setBackground(Color.RED);
else
player = "Player 1";
btn.setBackground(Color.BLUE);
try
Thread.sleep(1000);
catch (InterruptedException ex)
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
player2Action();
private void player2Action()
if (player.equals("Player 2"))
btn1.doClick();
public static void main(String args[])
try
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
if ("Nimbus".equals(info.getName()))
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
catch (ClassNotFoundException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (InstantiationException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (IllegalAccessException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
catch (javax.swing.UnsupportedLookAndFeelException ex)
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.awt.EventQueue.invokeLater(new Runnable()
public void run()
new NewJFrame().setVisible(true);
);
// Variables declaration - do not modify
private javax.swing.JButton btn1;
private javax.swing.JButton btn2;
private javax.swing.JButton btn3;
private NewJPanel newJPanel1;
private NewJPanel newJPanel2;
// End of variables declaration
面板:
public class NewJPanel extends javax.swing.JPanel
public NewJPanel()
initComponents();
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents()
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration
【问题讨论】:
使用 SwingTimer
。 更长: 不要阻塞 EDT(事件调度线程)。发生这种情况时,GUI 将“冻结”。有关详细信息和修复,请参阅 Concurrency in Swing。
如何使用摇摆计时器?我看到这个例子但不明白***.com/questions/1006611/java-swing-timer
【参考方案1】:
在这种情况下,请使用 Timer
代替 Thread
。查看Andrew Thompson的cmets了解更多详情。
要修复您的代码,只需删除 Thread.sleep
的 try-catch 块和它旁边的 player2Action()
方法。然后添加以下代码使其工作:
ActionListener player2ActionListener = new ActionListener()
public void actionPerformed(ActionEvent evt)
player2Action();
;
Timer timer = new Timer(1000, player2ActionListener);
timer.setRepeats(false);
timer.start();
别忘了导入javax.swing.Timer
。
【讨论】:
以上是关于为啥这个java程序不能正常工作?的主要内容,如果未能解决你的问题,请参考以下文章
我的 quickSort() 程序不能正常工作,为啥? [关闭]
为啥我不能在 docker 中构建这个 nuxtjs 应用程序,而本地构建工作?
为啥我的 pygame 应用程序循环不能正常工作? [复制]