📄 configdialog.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import edu.njust.cs.*;
public class ConfigDialog extends JDialog implements ActionListener{
private JButton btnOk=new JButton("确定");
private JButton btnCancel=new JButton("取消");
//SQL参数
private JLabel labDBName=new JLabel(" 数据库名称 ");
private JTextField txtDBName=new JTextField();
private JLabel labDBip=new JLabel(" 数据库IP ");
private JTextField txtDBip=new JTextField();
private JLabel labDBport=new JLabel(" 侦听端口号 ");
private JTextField txtDBport=new JTextField();
private JLabel labUserName=new JLabel(" 用 户 名 ");
private JTextField txtUserName=new JTextField();
private JLabel labUserPwd=new JLabel(" 密 码 ");
private JPasswordField txtUserPwd=new JPasswordField();
private JLabel labProductSn=new JLabel(" 产品序列号 ");
private JTextField txtProductSn=new JTextField();
private JLabel labPicOfSql=new JLabel();
private String strForSql="<html>说明:"+
"<br> 本页面设置和SQL服务器相关的选项<br>"+
"请注意:<br>修改设置后需要重新启动才能生效!</html>";
private JLabel labSqlDescript=new JLabel(strForSql);
private Properties prop=new Properties();
private JTabbedPane tab=new JTabbedPane();
private TransferFocusListener tfl=new TransferFocusListener();
private MainApp father=null;
public ConfigDialog(MainApp f,String s,boolean b){
super((MainApp)f,s,b);
father=f;
btnOk.setIcon(new ImageIcon("image/ok20.gif"));
btnCancel.setIcon(new ImageIcon("image/cancel20.gif"));
constructTabbedPane();
this.setSize(450,300);
Dimension d=f.getSize();
Point p=f.getLocation();
this.setLocation(p.x+(d.width-450)/2,p.y+(d.height-300)/2);
this.setResizable(false);
}
public void constructTabbedPane(){
tab=new JTabbedPane();
Insets insets=new Insets(2,2,2,2);
JPanel sqlPanel=new JPanel();
sqlPanel.setBorder(BorderFactory.createTitledBorder("SQL Server"));
sqlPanel.setLayout(new GridBagLayout());
txtDBName.addActionListener(tfl);
txtDBip.addActionListener(tfl);
txtDBport.addActionListener(tfl);
txtUserName.addActionListener(tfl);
txtUserPwd.addActionListener(tfl);
txtProductSn.addActionListener(tfl);
//布局SQL参数
LayoutUtil.add(sqlPanel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,0,1,1,this.labDBName,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,0,1,1,this.txtDBName,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,0,1,1,this.labDBip,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.HORIZONTAL,
GridBagConstraints.WEST,100,0,3,0,1,1,this.txtDBip,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,1,1,1,this.labDBport,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,1,1,1,this.txtDBport,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,1,1,1,this.labUserName,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,1,1,1,this.txtUserName,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,0,2,1,1,this.labUserPwd,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,1,2,1,1,this.txtUserPwd,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,2,1,1,this.labProductSn,insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,3,2,1,1,this.txtProductSn,insets);
labPicOfSql.setIcon(new ImageIcon("image/sqldb32.gif"));
labPicOfSql.setVerticalAlignment(SwingConstants.BOTTOM);
labSqlDescript.setBorder(BorderFactory.createLoweredBevelBorder());
LayoutUtil.add(sqlPanel,GridBagConstraints.BOTH,
GridBagConstraints.CENTER,100,100,0,6,4,1,new JLabel(),insets);
LayoutUtil.add(sqlPanel,GridBagConstraints.BOTH,
GridBagConstraints.SOUTHWEST,100,100,0,6,4,1,this.labPicOfSql);
LayoutUtil.add(sqlPanel,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,7,4,1,this.labSqlDescript);
//数据库参数属性页
tab.add("数据库参数",sqlPanel);
//您还可以根据需要添加更多的属性页
//这里作为演示,使用了一个空白的属性页
tab.add("更多参数",new JPanel());
this.getContentPane().add(tab,"Center");
initPara(); //读入参数值
btnOk.addActionListener(this);
btnCancel.addActionListener(this);
JPanel panel=new JPanel();
panel.setLayout(new GridBagLayout());
LayoutUtil.add(panel,GridBagConstraints.HORIZONTAL,
GridBagConstraints.CENTER,100,0,0,0,1,1,new JLabel(),insets);
LayoutUtil.add(panel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,1,0,1,1,btnOk,insets);
LayoutUtil.add(panel,GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0,2,0,1,1,btnCancel,insets);
this.getContentPane().add(panel,"South");
//设置btnOk为默认按钮
this.getRootPane().setDefaultButton(this.btnOk);
//进入设置对话框后,txtDBName立刻获得焦点
SwingUtilities.invokeLater(new Runnable(){
public void run(){
txtDBName.requestFocus();
}
});
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
if(source==btnOk){
if(savePara())
this.setVisible(false);
}
else if(source==btnCancel)
this.setVisible(false);
}
public boolean savePara(){
String fileName=System.getProperties().get("user.dir")
+"/config/"+"Config.properties";
try{
FileOutputStream out=new FileOutputStream(fileName);
prop.setProperty("DB_NAME",this.txtDBName.getText().trim());
prop.setProperty("DB_IP",this.txtDBip.getText().trim());
prop.setProperty("DB_PORT",this.txtDBport.getText().trim());
prop.setProperty("DB_USER_NAME",this.txtUserName.getText().trim());
prop.setProperty("DB_USER_PWD",new String(this.txtUserPwd.getPassword()));
prop.setProperty("PRODUCT_SN", this.txtProductSn.getText().trim());
prop.store(out,"Configuration Parameters");
out.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null,"保存配置文件时出错!",
"信息",
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
public void initPara(){
String fileName=System.getProperties().get("user.dir")
+"/config/"+"Config.properties";
try{
FileInputStream in=new FileInputStream(fileName);
prop.load(in);
in.close();
this.txtDBName.setText(prop.getProperty("DB_NAME"));
this.txtDBip.setText(prop.getProperty("DB_IP"));
this.txtDBport.setText(prop.getProperty("DB_PORT"));
this.txtUserName.setText(prop.getProperty("DB_USER_NAME"));
this.txtUserPwd.setText( prop.getProperty("DB_USER_PWD"));
this.txtProductSn.setText(prop.getProperty("PRODUCT_SN"));
}catch(Exception e){
JOptionPane.showMessageDialog(null,"配置文件丢失!"+
"\n建议重新安装程序",
"信息",
JOptionPane.ERROR_MESSAGE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -