代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame implements Runnable{
JPanel panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel(new GridLayout(3,2));
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel(new GridLayout(2,3));
JLabel label1 = new JLabel( "生产产品的时间间隔: ");
JLabel label2 = new JLabel( "最多存放产品数量: ");
JLabel label3 = new JLabel( "消费产品的时间间隔: ");
JTextField textField1 = new JTextField(10);
JTextField textField2 = new JTextField(10);
JTextField textField3 = new JTextField(10);
JLabel progressLabel = new JLabel( "生产消费进度表: ");
JProgressBar progressBar = new JProgressBar(0,Integer.parseInt(textField2.getText()));
JLabel produceLabel = new JLabel( "生产: ");
JLabel customerLabel = new JLabel( "消费: ");
JButton ProducerButton_start = new JButton( "启动 ");
JButton ProducerButton_suspend = new JButton( "暂停 ");
JButton CustomerButton_start = new JButton( "启动 ");
JButton CustomerButton_suspend = new JButton( "暂停 ");
Thread producer_thread = new Thread(this);
Thread customer_thread = new Thread(this);
Timer producer_timer;
Timer customer_timer;
int x_size,y_size;
public MainFrame(){
setSize(300, 350);
x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size - 300) / 2, (y_size - 350) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(label1);
panel1.add(textField1);
panel1.add(label2);
panel1.add(textField2);
panel1.add(label3);
panel1.add(textField3);
panel2.add(progressLabel);
panel2.add(progressBar);
panel3.add(produceLabel);
panel3.add(ProducerButton_start);
panel3.add(ProducerButton_suspend);
panel3.add(customerLabel);
panel3.add(CustomerButton_start);