用Java尝试开发一个窗体,在窗体中有两个按钮,一个是“开始”按钮,一个是结束按钮。当用户单击“
始”按钮时,在控制台打印出一段话。当用户单击“结束”按钮时,控制台结束打印,,,,,,求解答,谢谢!...
始”按钮时,在控制台打印出一段话。当用户单击“结束”按钮时,控制台结束打印,,,,,,求解答,谢谢!
展开
3个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class PrintFrame extends JFrame {
private JPanel contentPane;
Timer timer;
int index=1;
private JTextArea jta;
//JDK 1.8
public static void main(String[] args) {
PrintFrame frame = new PrintFrame();
frame.setVisible(true);
}
public PrintFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(350, 200);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
JButton jbStart = new JButton("开始打印");
panel.add(jbStart);
timer = new Timer(500, new ActionListener() {//每500毫秒打印1次
public void actionPerformed(ActionEvent e) {
jta.append("打印"+"第"+index+"行\r\n");
index++;
}
});
jbStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.start();//开始
}
});
JButton jbEnd = new JButton("停止打印");
panel.add(jbEnd);
jbEnd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();//停止
}
});
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
jta = new JTextArea();
scrollPane.setViewportView(jta);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询