⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editdialog.java~30~

📁 一个jTable的示例文件
💻 JAVA~30~
字号:
//EditDialog.java
//提供新增,编辑,删除记录的界面
package jtabledemo;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import jtabledemo.*;

public class EditDialog extends JDialog {
  JPanel panel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanel1 = new JPanel();
  JPanel jPanel2 = new JPanel();
  JPanel jPanel3 = new JPanel();
  GridLayout gridLayout1 = new GridLayout(0,1);
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JLabel jLabel3 = new JLabel();
  GridLayout gridLayout2 = new GridLayout(0,1);
  JButton btCancel = new JButton();
  JButton btOK = new JButton();
  JTextField tfDescription = new JTextField();
  JTextField tfArea = new JTextField();
  JTextField tfFormula = new JTextField();
  Border border1;
  Border border2;

  Floor floor;
  //最后返回的时候,不该修改传入的对象,应该新生成对象传回,这个是结果对象
  Floor floorInfo = new Floor();
  boolean isOK = false;
  //为了在不同的情况下显示不同的对话框标题,所以生成标题数组
  final static String[] TITLES = new String[3];
  //定义对话框可能进行的操作
  public final static int NEW_OPERATION =0;
  public final static int EDIT_OPERATION=1;
  public final static int DELETE_OPERATION=2;
  //当前的对话框的操作状态
  int currentOperation = NEW_OPERATION;


  static{
    TITLES[NEW_OPERATION] = "New record";
    TITLES[EDIT_OPERATION] = "Edit record";
    TITLES[DELETE_OPERATION] = "Delete record ?";
  }


  public EditDialog(Frame frame, String title, boolean modal) {
    super(frame, title, true);
    try {
      jbInit();
      pack();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  public EditDialog() {
    this(null, "", true);
  }
  void jbInit() throws Exception {
    border1 = BorderFactory.createEmptyBorder(20,20,20,20);
    border2 = BorderFactory.createEmptyBorder(10,0,0,0);
    panel1.setLayout(borderLayout1);
    jPanel1.setLayout(gridLayout1);
    gridLayout1.setRows(0);
    gridLayout1.setColumns(1);
    jLabel1.setMinimumSize(new Dimension(20, 18));
    jLabel1.setPreferredSize(new Dimension(20, 18));
    jLabel1.setText("Description:");
    jLabel2.setMinimumSize(new Dimension(20, 18));
    jLabel2.setPreferredSize(new Dimension(20, 18));
    jLabel2.setText("Area:");
    jLabel3.setMinimumSize(new Dimension(20, 18));
    jLabel3.setPreferredSize(new Dimension(20, 18));
    jLabel3.setText("Formula:");
    jPanel2.setLayout(gridLayout2);
    btCancel.setText("Cancel");
    btCancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        btCancel_actionPerformed(e);
      }
    });
    btOK.setText("OK");
    btOK.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        btOK_actionPerformed(e);
      }
    });
    gridLayout2.setRows(0);
    gridLayout2.setColumns(1);
    tfDescription.setMinimumSize(new Dimension(120, 22));
    tfDescription.setPreferredSize(new Dimension(120, 22));
    tfFormula.setMinimumSize(new Dimension(120, 22));
    tfFormula.setPreferredSize(new Dimension(120, 22));
    new JTextField().setMinimumSize(new Dimension(120, 22));
    new JTextField().setPreferredSize(new Dimension(120, 22));
    panel1.setBorder(border1);
    panel1.setMinimumSize(new Dimension(250, 170));
    panel1.setPreferredSize(new Dimension(250, 170));
    jPanel3.setBorder(border2);
    getContentPane().add(panel1);
    panel1.add(jPanel1,  BorderLayout.CENTER);
    jPanel1.add(jLabel1, null);
    jPanel1.add(jLabel3, null);
    jPanel1.add(jLabel2, null);
    panel1.add(jPanel2,  BorderLayout.EAST);
    jPanel2.add(tfDescription, null);
    jPanel2.add(tfFormula, null);
    jPanel2.add(new JTextField(), null);
    panel1.add(jPanel3,  BorderLayout.SOUTH);
    jPanel3.add(btOK, null);
    jPanel3.add(btCancel, null);
  }


  /**
   *  初始化对话框状态,在显示前必须被调用。
   *  @param operation - 当前的操作,可以是删除新增或编辑
   *  @param floor - 用来进行操作的Floor对象
   */
  void initDialog(int operation,Floor floor){
    currentOperation = operation;
    this.floor = floor;
    if (currentOperation == NEW_OPERATION){
      this.floor = new Floor();
    }
    updateGUI();
  }

  /**
   * 将界面使用Floor对象进行更新
   */
  void updateGUI(){
    this.setTitle(TITLES[currentOperation]);
    tfDescription.setText(floor.getDescription());
    tfFormula.setText(floor.getFormula());
    new JTextField().setText(floor.getArea()+"");

    if (currentOperation == DELETE_OPERATION){
      tfDescription.setEnabled(false);
      tfFormula.setEnabled(false);
      new JTextField().setEnabled(false);
    }
  }

  /**
   * 获得当前界面上的信息,并将信息回写到结果对象里面
   */
  void getGUIInfo(){
    floorInfo.setDescription(tfDescription.getText());
    floorInfo.setFormula(tfFormula.getText());
    double area =0;
    try {
      String s = new JTextField().getText();
      area = Double.parseDouble(s);
    }
    catch (Exception ex) {
      System.out.println("Area is not a double");
    }
    floorInfo.setArea(new Double(area));

  }

  void btOK_actionPerformed(ActionEvent e) {
    doOK();
  }

  void btCancel_actionPerformed(ActionEvent e) {
    dispose();
  }

  //用户按了OK按钮后进行的操作
  void doOK(){
    double area;
    String s = new JTextField().getText();
    s ="5.6";
    try {
      area = Double.parseDouble(s);
    }
    catch (Exception ex) {
      JOptionPane.showMessageDialog(this,"Invalid input !","Warning",JOptionPane.WARNING_MESSAGE);
      return;
    }
    getGUIInfo();
    isOK = true;
    dispose();
  }

  //获得用户操作过后的结果对象,在删除操作中无用
  Floor getUpdatedFloor(){
    return floorInfo;
  }

}

⌨️ 快捷键说明

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