SWUST--Java实验(六) 线程

xiaoxiao2021-02-28  32

public class CaThread implements Runnable { static double sum = 1; static String str = "1!"; static int n = 1; Thread thread;//线程类对象 public CaThread() { thread = new Thread(this);//调用本类对象 thread.start(); } double plus(int n) { sum = 1; for (int i = 1; i <= n; i++) sum *= i; return sum; } public class ReadThread extends Thread { CaThread caThread; public ReadThread(CaThread caThread) { this.caThread = caThread;//调用CaThread构造器 } public void run() { try { while (true) { sleep(1000); ThreadGUI.textArea.setText(caThread.str); ThreadGUI.textField.setText(caThread.sum + ""); ThreadGUI.progressBar.setValue(caThread.n); } } catch (InterruptedException e) { System.err.println("程序读取失败!"); } } } public class ThreadGUI extends JFrame implements ActionListener { JPanel contentPane; static JTextField textField = new JTextField();; static JProgressBar progressBar = new JProgressBar(); static JTextArea textArea = new JTextArea(); JButton bt1 = new JButton("\u5F00\u59CB"); JButton bt2 = new JButton("\u505C\u6B62"); CaThread c = new CaThread(); ReadThread r = new ReadThread(c); /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ThreadGUI frame = new ThreadGUI(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ThreadGUI() { setTitle("\u591A\u7EBF\u7A0B\u5C0F\u7A0B\u5E8F"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 418, 185); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); bt1.setFont(new Font("宋体", Font.PLAIN, 14)); bt1.setBounds(309, 64, 63, 34); contentPane.add(bt1); bt2.setFont(new Font("宋体", Font.PLAIN, 14)); bt2.setBounds(309, 108, 63, 34); contentPane.add(bt2); JLabel lblNewLabel = new JLabel("\u8BA1\u7B97\u8FC7\u7A0B\uFF1A"); lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 14)); lblNewLabel.setBounds(21, 17, 85, 24); contentPane.add(lblNewLabel); JLabel label = new JLabel("\u8FD0\u884C\u7ED3\u679C\uFF1A"); label.setFont(new Font("宋体", Font.PLAIN, 14)); label.setBounds(21, 108, 85, 24); contentPane.add(label); JLabel label_1 = new JLabel("\u8FDB\u5EA6\u6761 \uFF1A"); label_1.setFont(new Font("宋体", Font.PLAIN, 14)); label_1.setBounds(21, 69, 85, 24); contentPane.add(label_1); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(116, 44, 282, -15); contentPane.add(scrollPane); JPanel panel = new JPanel(); panel.setBounds(99, 14, 294, 34); contentPane.add(panel); panel.setLayout(new BorderLayout(0, 0)); JScrollPane scrollPane_1 = new JScrollPane(); panel.add(scrollPane_1, BorderLayout.CENTER); textArea.setFont(new Font("Monospaced", Font.PLAIN, 14)); scrollPane_1.setViewportView(textArea); progressBar.setMaximum(30); progressBar.setForeground(Color.ORANGE); progressBar.setBounds(99, 69, 181, 24); contentPane.add(progressBar); textField.setBounds(99, 107, 181, 27); contentPane.add(textField); textField.setColumns(10); bt1.addActionListener(this); bt2.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == bt1) { bt1.setEnabled(false); bt2.setEnabled(true); r.start(); } if (e.getSource() == bt2) { bt1.setEnabled(true); bt2.setEnabled(false); r.stop(); } } }
转载请注明原文地址: https://www.6miu.com/read-2619825.html

最新回复(0)