如何创建一个使用线程从下限到上限计数的程序?

时间:2016-07-01 09:17:14

标签: java multithreading swing user-interface

上周我们刚刚介绍了GUI和现在的Threads。我们得到了一个赋值,我们需要创建一个面板,通过Text Fields从用户收集upperBound,lowerBound,incrementValue和delayBetweenEachCount。该面板需要包含“开始”,“暂停”和“继续”按钮,允许用户完全控制程序,因为它通过incrementValue从具有delayBetweenEachCount的incrementBound计数从lowerBound到upperBound。

我刚刚开始研究它并且非常困惑,如果代码相当愚蠢我会道歉。

我不知道如何阅读错误消息。如果有人能突出我出错的一些方面,我将不胜感激。很感谢任何形式的帮助。提前谢谢。

尝试运行代码时收到以下错误消息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at assignment5.CounterPanel.<init>(CounterPanel.java:139)
    at assignment5.CounterPanel$2.run(CounterPanel.java:119)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)

这是我目前的不完整代码:

        public class CounterPanel extends javax.swing.JFrame {

    // Creates new form
    public CounterPanel() {
        initComponents();
    }
//Thread counter = new Thread(new Counter(lower, upper, valueIncrement, timeDelay));
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        lowerBoundLabel = new javax.swing.JLabel();
        upperBoundLabel = new javax.swing.JLabel();
        lowerBoundTextField = new javax.swing.JTextField();
        upperBoundTextField = new javax.swing.JTextField();
        incrementValueLabel = new javax.swing.JLabel();
        delayValueLabel = new javax.swing.JLabel();
        incrementValueTextField = new javax.swing.JTextField();
        delayValueTextField = new javax.swing.JTextField();
        startButton = new javax.swing.JButton();
        pauseButton = new javax.swing.JButton();
        continueButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Counter");

        lowerBoundLabel.setText("Lower bound:");
        lowerBoundLabel.setName(""); // NOI18N

        upperBoundLabel.setText("Upper bound: ");

        incrementValueLabel.setText("Increment value:");

        delayValueLabel.setText("Delay value: ");

        startButton.setText("Start");
        startButton.setName(""); // NOI18N
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        pauseButton.setText("Pause");

        continueButton.setText("Continue");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(incrementValueLabel)
                    .addComponent(upperBoundLabel)
                    .addComponent(lowerBoundLabel)
                    .addComponent(delayValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(incrementValueTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                    .addComponent(upperBoundTextField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lowerBoundTextField)
                    .addComponent(delayValueTextField))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 135, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(continueButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pauseButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(startButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(15, 15, 15))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lowerBoundTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lowerBoundLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(upperBoundLabel)
                            .addComponent(upperBoundTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(incrementValueLabel)
                            .addComponent(incrementValueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(65, 65, 65)
                        .addComponent(startButton)
                        .addGap(26, 26, 26)
                        .addComponent(pauseButton)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(delayValueLabel)
                            .addComponent(delayValueTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(continueButton)))
                .addContainerGap(87, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
      counter.start();
    }                                           

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CounterPanel().setVisible(true);
            }
        });

    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton continueButton;
    private javax.swing.JLabel delayValueLabel;
    private javax.swing.JTextField delayValueTextField;
    private javax.swing.JLabel incrementValueLabel;
    private javax.swing.JTextField incrementValueTextField;
    private javax.swing.JLabel lowerBoundLabel;
    private javax.swing.JTextField lowerBoundTextField;
    private javax.swing.JButton pauseButton;
    private javax.swing.JButton startButton;
    private javax.swing.JLabel upperBoundLabel;
    private javax.swing.JTextField upperBoundTextField;
    // End of variables declaration                   

int upper = Integer.parseInt(upperBoundTextField.getText());
int lower = Integer.parseInt(lowerBoundTextField.getText());
int valueIncrement = Integer.parseInt(incrementValueTextField.getText());
int timeDelay = Integer.parseInt(delayValueTextField.getText());
Thread counter = new Thread(new Counter(lower, upper, valueIncrement, timeDelay));
}

public class Counter implements Runnable{
    int start;
    int end;
    int delayTime;
    int incrementValue;
    int i;

    public Counter(int lower, int upper, int timeDelay, int valueIncrement){
        start = lower;
        end = upper;   
        delayTime = timeDelay;
        incrementValue = valueIncrement;
        i = lower;
    }


    public void run(){
        try{
            while(i <= end)
                Thread.sleep(delayTime);
                System.out.print(i);
                i += incrementValue;
        }catch(Exception e){}
    }

}

1 个答案:

答案 0 :(得分:1)

创建CounterPanel时,会创建Counter。但是Counter扩展了CounterPanel。因此,构建CounterPanel会创建一个Counter,创建一个Counter,创建一个Counter,创建一个Counter,...

Counter绝对没有理由延长CounterPanel