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

📄 inputdialog.java

📁 java DataBase Exp
💻 JAVA
字号:
/*源代码清单11-5*/

package database;

import java.awt.*;
import java.awt.event.*;
import borland.jbcl.layout.*;
import borland.jbcl.control.*;

public class InputDialog extends Dialog
{
  Panel panel1 = new Panel();
  GroupBox groupBox1 = new GroupBox();
  Button button1 = new Button();
  Button button2 = new Button();
  PaneLayout paneLayout1 = new PaneLayout();
  Label label1 = new Label();
  Label label2 = new Label();
  Label label3 = new Label();
  Label label4 = new Label();
  Label label5 = new Label();
  TextField textField1 = new TextField();
  TextField textField2 = new TextField();
  TextField textField3 = new TextField();
  TextField textField4 = new TextField();
  TextField textField5 = new TextField();
  PaneLayout paneLayout2 = new PaneLayout();
  MainFrame frame=null;

  public InputDialog(MainFrame frame, String title, boolean modal)
  {
    super(frame, title, modal);
    try
    {
      this.frame=frame;
      jbInit();
      add(panel1);
      pack();
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public InputDialog(MainFrame frame)
  {
    this(frame, "", false);
  }

  public InputDialog(MainFrame frame, boolean modal)
  {
    this(frame, "", modal);
  }

  public InputDialog(MainFrame frame, String title)
  {
    this(frame, title, false);
  }

  private void jbInit() throws Exception
  {
    groupBox1.setLayout(paneLayout2);
    panel1.setSize(new Dimension(327, 154));
    groupBox1.setLabel("请输入添加用户信息:");
    button1.setLabel("确定");
    button1.addActionListener(new InputDialog_button1_actionAdapter(this));
    button2.setLabel("取消");
    button2.addActionListener(new InputDialog_button2_actionAdapter(this));
    label1.setAlignment(2);
    label1.setText("姓名:");
    label2.setAlignment(2);
    label2.setText("性别:");
    label3.setAlignment(2);
    label3.setText("年龄:");
    label4.setAlignment(2);
    label4.setText("电话:");
    label5.setAlignment(2);
    label5.setText("备注:");
    panel1.setLayout(paneLayout1);
    panel1.add(groupBox1, new PaneConstraints("groupBox1", "groupBox1", PaneConstraints.ROOT, 0.5f));
    groupBox1.add(textField3, new PaneConstraints("textField3", "textField3", PaneConstraints.ROOT, 0.5f));
    groupBox1.add(textField5, new PaneConstraints("textField5", "textField3", PaneConstraints.BOTTOM, 0.37623763f));
    groupBox1.add(textField4, new PaneConstraints("textField4", "textField5", PaneConstraints.TOP, 0.53125f));
    groupBox1.add(label4, new PaneConstraints("label4", "textField4", PaneConstraints.LEFT, 0.2525252f));
    groupBox1.add(label5, new PaneConstraints("label5", "textField5", PaneConstraints.LEFT, 0.2525252f));
    groupBox1.add(label3, new PaneConstraints("label3", "textField3", PaneConstraints.LEFT, 0.25063288f));
    groupBox1.add(label2, new PaneConstraints("label2", "label3", PaneConstraints.TOP, 0.63492066f));
    groupBox1.add(label1, new PaneConstraints("label1", "label2", PaneConstraints.TOP, 0.5263158f));
    groupBox1.add(textField1, new PaneConstraints("textField1", "textField3", PaneConstraints.TOP, 0.6507937f));
    groupBox1.add(textField2, new PaneConstraints("textField2", "textField1", PaneConstraints.BOTTOM, 0.4634146f));
    panel1.add(button1, new PaneConstraints("button1", "groupBox1", PaneConstraints.BOTTOM, 0.14814818f));
    panel1.add(button2, new PaneConstraints("button2", "button1", PaneConstraints.RIGHT, 0.54128444f));
  }

  void button1_actionPerformed(ActionEvent e)
  {
    boolean flag=textField1.isEditable();
    if(flag)
      addDatabase();
    else
      changeDatabase();
  }

  void button2_actionPerformed(ActionEvent e)
  {
    this.dispose();
  }

  //修改记录
  void changeDatabase()
  {
    try
    {
      String sql="update people set sex='";
      sql+=textField2.getText()+"',age=";
      sql+=textField3.getText()+",phone='";
      sql+=textField4.getText()+"',other='";
      sql+=textField5.getText()+"' where name='";
      sql+=textField1.getText()+"'";
      frame.statement=frame.connection.prepareStatement(sql);
      frame.statement.executeUpdate();
      frame.statement.close();
      //调用主窗体显示数据函数
      frame.setGridControl();
      this.dispose();
    }catch(Exception e)
    {
      System.out.println(e.toString());
      }
  }

  //增加记录
  void addDatabase()
  {
   try
   {
     String sql="insert into people(name,sex,age,phone,other) values('";
     sql+=textField1.getText()+"','";
     sql+=textField2.getText()+"',";
     sql+=textField3.getText()+",'";
     sql+=textField4.getText()+"','";
     sql+=textField5.getText();
     sql+="')";
     frame.statement=frame.connection.prepareStatement(sql);
     frame.statement.executeUpdate();
     frame.statement.close();
     //调用主窗体显示数据函数
     frame.setGridControl();
     this.dispose();
   }catch(Exception e)
   {
     System.out.println(e.toString());
     }
  }

}

class InputDialog_button1_actionAdapter implements java.awt.event.ActionListener
{
  InputDialog adaptee;

  InputDialog_button1_actionAdapter(InputDialog adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.button1_actionPerformed(e);
  }
}

class InputDialog_button2_actionAdapter implements java.awt.event.ActionListener
{
  InputDialog adaptee;

  InputDialog_button2_actionAdapter(InputDialog adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.button2_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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