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

📄 connectdbpanel.java

📁 酒店管理软件的源代码
💻 JAVA
字号:
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import java.io.File;

public class ConnectDBPanel extends ExchangeDialogPanel
{
  public ConnectDBPanel(MainFrame parent)
  {
    this.parent = parent;
    setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.NONE;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.weightx = 0;
    constraints.weighty = 0;

    userNameField = new JTextField(20);
    passwordField = new JPasswordField(20);
    FilePathField = new JTextField(12);
    FilePathField.setEditable(false);
    JButton selectFileButton = new JButton("选择文件");
    autoConnectCheckBox = new JCheckBox("记住密码,并在下次运行时自动连接");
    add(this, new JLabel("用 户 名:"), constraints, 0, 0, 1, 1);
    add(this, new JLabel("密   码:"), constraints, 0, 1, 1, 1);
    add(this, new JLabel("数据库文件:"), constraints, 0, 2, 1, 1);
    add(this, userNameField, constraints, 1, 0, 2, 1);
    add(this, passwordField, constraints, 1, 1, 2, 1);
    add(this, FilePathField, constraints, 1, 2, 1, 1);
    add(this, selectFileButton, constraints, 2, 2, 1, 1);
    add(this, autoConnectCheckBox, constraints, 0, 3, 3, 1);

    selectFileButton.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        int result = chooser.showOpenDialog(ConnectDBPanel.this);
        if (result == JFileChooser.APPROVE_OPTION)
        {
          FilePathField.setText(chooser.getSelectedFile().getPath());
        }
      }
    });

    initFileChooser();
  }

  public boolean initData()
  {
    System.out.println("initData");
    userNameField.setText(parent.userName);
    passwordField.setText(parent.password);
    FilePathField.setText(parent.formerFilePath);
    autoConnectCheckBox.setSelected(parent.autoConnect);
    return true;
  }

  public boolean updateData()
  {
    System.out.println("updateData");
    if (!FilePathField.getText().equals(parent.formerFilePath) &&
        FilePathField.getText().endsWith(".mdb"))
    {
      Registry.add("酒店管理", FilePathField.getText(), "酒店管理临时文件.reg");
      File tempREGFile = new File("酒店管理临时文件.reg");
      try
      {
        this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
        parent.formerFilePath = FilePathField.getText();
        Thread.sleep(500);
        parent.showMessage("注册表项修改成功!");
        this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        tempREGFile.delete();
      }
      catch (InterruptedException e)
      {
        Toolkit.getDefaultToolkit().beep();
        JOptionPane.showMessageDialog(ConnectDBPanel.this, e.toString(),
                                      "错误", JOptionPane.WARNING_MESSAGE);
        System.exit(0);
      }
    }
    else if (!FilePathField.getText().endsWith(".mdb"))
    {
      Toolkit.getDefaultToolkit().beep();
      JOptionPane.showMessageDialog(ConnectDBPanel.this,
                                    "数据库文件应该以.mdb结尾!\n请重新选择",
                                    "错误", JOptionPane.WARNING_MESSAGE);
      return false;
    }

    parent.userName = userNameField.getText();
    parent.password = new String(passwordField.getPassword());
    parent.autoConnect = autoConnectCheckBox.isSelected();
    parent.reConnect();
    return true;
  }

  public boolean cancelData()
  {
    System.out.println("cancelData");
    return true;
  }

  private void add(Container parent, Component c,
                   GridBagConstraints constraints, int x,
                   int y, int w, int h)
  {
    constraints.gridx = x;
    constraints.gridy = y;
    constraints.gridwidth = w;
    constraints.gridheight = h;
    parent.add(c, constraints);
  }

  private void initFileChooser()
  {
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));
    final ExtensionFileFilter filter = new ExtensionFileFilter();
    filter.addExtension("mdb");
    filter.setDescription("微软数据库文件");
    chooser.setFileFilter(filter);
  }

  private MainFrame parent;
  private JTextField userNameField;
  private JPasswordField passwordField;
  private JTextField FilePathField;
  private JCheckBox autoConnectCheckBox;
  private JFileChooser chooser;
}

⌨️ 快捷键说明

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