App下載
首頁javabasicJava Swing - 如何extends AbstractAction

Java Swing - 如何extends AbstractAction

我們想知道如何extends AbstractAction。
import java.awt.Dialog;
import java.awt.Window;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Main {
  public static void main(String[] args) {
    MainPanelGen mainPanelGen = new MainPanelGen();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(mainPanelGen.getMainPanel());
    frame.pack();
    frame.setVisible(true);
  }
}

class MainPanelGen {
  JPanel mainPanel = new JPanel();
  JTextField field = new JTextField(10);
  JButton btn = new JButton(new BtnActn());
  JDialog dialog;
  DialogPanel dialogPanel = new DialogPanel();

  public MainPanelGen() {
    mainPanel.add(field);
    mainPanel.add(btn);
    field.setEditable(false);
    field.setFocusable(false);
  }

  public JPanel getMainPanel() {
    return mainPanel;
  }

  private class BtnActn extends AbstractAction {
    BtnActn() {
      super("Button");
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
      if (dialog == null) {
        Window win = SwingUtilities.getWindowAncestor(mainPanel);
        if (win != null) {
          dialog = new JDialog(win, "My Dialog",
              Dialog.ModalityType.APPLICATION_MODAL);
          dialog.getContentPane().add(dialogPanel);
          dialog.pack();
          dialog.setLocationRelativeTo(null);
        }
      }
      dialog.setVisible(true); // here the modal dialog takes over
      System.out.println(dialogPanel.getFieldText());
      field.setText(dialogPanel.getFieldText());
    }
  }
}

class DialogPanel extends JPanel {
  private JTextField field = new JTextField(10);
  private JButton exitBtn = new JButton(new ExitBtnAxn("Exit"));

  public DialogPanel() {
    add(field);
    add(exitBtn);
  }

  public String getFieldText() {
    return field.getText();
  }

   class ExitBtnAxn extends AbstractAction {

    public ExitBtnAxn(String name) {
      super(name);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
      Window win = SwingUtilities.getWindowAncestor(DialogPanel.this);
      if (win != null) {
        win.dispose();
      }

    }
  }
}