示例#1
0
  /**
   * And now for a little assembly. Put together the buttons, progress bar and status text field.
   */
  Example1(String name) {
    setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), name));

    progressBar.setMaximum(NUMLOOPS);

    startButton = new JButton("Start");
    startButton.addActionListener(startListener);
    startButton.setEnabled(true);

    interruptButton = new JButton("Cancel");
    interruptButton.addActionListener(interruptListener);
    interruptButton.setEnabled(false);

    JComponent buttonBox = new JPanel();
    buttonBox.add(startButton);
    buttonBox.add(interruptButton);

    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    add(buttonBox);
    add(progressBar);
    add(statusField);
    statusField.setAlignmentX(CENTER_ALIGNMENT);

    buttonBox.setBorder(spaceBelow);
    Border pbBorder = progressBar.getBorder();
    progressBar.setBorder(BorderFactory.createCompoundBorder(spaceBelow, pbBorder));
  }
示例#2
0
        public void actionPerformed(ActionEvent event) {
          startButton.setEnabled(false);
          interruptButton.setEnabled(true);
          statusField.setText("Working...");

          /* Constructing the SwingWorker() causes a new Thread
           * to be created that will call construct(), and then
           * finished().  Note that finished() is called even if
           * the worker is interrupted because we catch the
           * InterruptedException in doWork().
           */
          worker =
              new SwingWorker() {
                public Object construct() {
                  return doWork();
                }

                public void finished() {
                  startButton.setEnabled(true);
                  interruptButton.setEnabled(false);
                  statusField.setText(get().toString());
                }
              };
        }