我正在尝试制作一个带有GUI的java程序,它可以让用户输入他想要等待的时间,直到机器关闭、重新启动或 sleep 。我设法制作了 GUI 并实现了关闭、重新启动或 sleep 的命令,但我不知道如何制作计时器。
这是我到目前为止所做的:
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ContactEditorUI extends javax.swing.JFrame {
public ContactEditorUI() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
buttonGroup2 = new javax.swing.ButtonGroup();
jInternalFrame1 = new javax.swing.JInternalFrame();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jInternalFrame1.setVisible(true);
jRadioButton1.setText("Shut down");
jRadioButton1.addContainerListener(new java.awt.event.ContainerAdapter() {
public void componentAdded(java.awt.event.ContainerEvent evt) {
jRadioButton1ComponentAdded(evt);
}
});
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
jRadioButton2.setText("Restart");
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton2ActionPerformed(evt);
}
});
jRadioButton3.setText("Sleep");
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton3ActionPerformed(evt);
}
});
jLabel1.setText("Please insert the time :");
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField1.setText("00:00:00");
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50)
.addComponent(jButton1))
.addComponent(jRadioButton1)
.addComponent(jRadioButton3)
.addComponent(jRadioButton2))
.addContainerGap(140, Short.MAX_VALUE))
);
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton1)
.addGroup(jInternalFrame1Layout.createSequentialGroup()
.addComponent(jTextField1)
.addGap(2, 2, 2)))
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton3)
.addContainerGap(32, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jInternalFrame1)
);
pack();
}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
buttonGroup.add(jRadioButton1);
Runtime runtime = Runtime.getRuntime();
try {
//wait();
Process proc = runtime.exec("shutdown -s");
} catch (IOException ex) {
Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {
buttonGroup.add(jRadioButton2);
Runtime runtime = Runtime.getRuntime();
try {
//wait();
Process proc = runtime.exec("shutdown -r");
} catch (IOException ex) {
Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {
buttonGroup.add(jRadioButton3);
Runtime runtime = Runtime.getRuntime();
try {
//wait();
Process proc = runtime.exec("shutdown -h");
} catch (IOException ex) {
Logger.getLogger(ContactEditorUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jRadioButton1ComponentAdded(java.awt.event.ContainerEvent evt) {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ContactEditorUI().setVisible(true);
}
});
}
private javax.swing.ButtonGroup buttonGroup;
private javax.swing.JButton jButton1;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private javax.swing.JTextField jTextField1;
}
我们将非常感谢所有帮助。
最佳答案
您需要使用 Java 的 Timer
类。其中有一个名为 schedule()
的方法,具有不同的签名,允许多种用途。
Timer timer = new Timer();
int start=10000; //millisecond 10 seconds=10000
int delay=1000; // millisecond 1 second
timer.schedule(new TimerTask()
{
public void run() {
// what happens when the timer repeats
}
},start,delay);
这段代码最初被发现here 。这是一个与您问题的一些背景相关的新答案。
这将在调用时启动一个新的计时器。为了能够让用户设置指定的时间,schedule 方法将其作为参数(输入的日期需要转换为开始变量)。
使用找到的 Java 文档 here能够修改此答案以适合您,因为时间表可以通过几种不同的方式使用。
关于java - 用Java制作一个倒计时器程序,最终会关闭/重新启动或 sleep 你的机器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50153272/