inputdialog.java

来自「Thinking in Java 是学习JAVA的经典教材」· Java 代码 · 共 66 行

JAVA
66
字号
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
package javatheater.client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public abstract class InputDialog extends JDialog {
  protected JButton
    btnOK = new JButton("OK"),
    btnCancel = new JButton("Cancel");

  protected int closeOperation;

  public InputDialog(Frame owner, String title) throws HeadlessException {
    super(owner, title);
    setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  }

  public int showModal() {
    setModal(true);
    setVisible(true);
    return closeOperation;
  }

  protected void populateDialog() {
    getContentPane().add(btnOK);
    getContentPane().add(btnCancel);

    btnOK.addActionListener(
      new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          doOK();
        }
      }
    );

    btnCancel.addActionListener(
      new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          doCancel();
        }
      }
    );

    transferDataToDialog();
  }

  protected void doOK() {
    transferDataFromDialog();
    closeOperation = JOptionPane.OK_OPTION;
    dispose();
  }

  protected void doCancel() {
    closeOperation = JOptionPane.CANCEL_OPTION;
    dispose();
  }

  protected abstract void transferDataFromDialog();

  protected abstract void transferDataToDialog();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?