最近做一个东西. 用到了模式对话框. 比如一个类如下: 是我的工程中一部分. 是一个新建的向导. 有上一步下一步. 但是现在不好运行. 因为除非你有工程.
package xmu.File;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;import xmu.MainFrame;
import static constants.Constants.*;public class NewRecord extends JDialog implements ActionListener
{
private JButton confirm, cancel;
private FirstStep firstStep;
private SecondStep secondStep;
private ThirdStep thirdStep;
private Point location;
private Frame mainFrame;
public NewRecord(Frame frame)
{
// super (frame, true);
setTitle("新建账簿");
mainFrame = frame;
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension dim = kit.getScreenSize();
location = new Point(dim.width / 3, dim.height / 3);
Container container = getContentPane();
Icon icon = new ImageIcon(getClass().getResource("/images/File/new.jpg"));
JLabel label = new JLabel(icon);
container.add(label, BorderLayout.NORTH);
cancel = new JButton("取消");
cancel.addActionListener(this);
confirm = new JButton("确定");
confirm.addActionListener(this);
Box hori = Box.createHorizontalBox();
hori.add(Box.createHorizontalStrut(20));
hori.add(cancel);
hori.add(Box.createHorizontalStrut(250));
hori.add(confirm);
container.add(hori, BorderLayout.SOUTH);
setLocation(location);
setSize(420, 200);
setVisible(true);
setResizable(false);
}
package xmu.File;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;import xmu.MainFrame;
import static constants.Constants.*;public class NewRecord extends JDialog implements ActionListener
{
private JButton confirm, cancel;
private FirstStep firstStep;
private SecondStep secondStep;
private ThirdStep thirdStep;
private Point location;
private Frame mainFrame;
public NewRecord(Frame frame)
{
// super (frame, true);
setTitle("新建账簿");
mainFrame = frame;
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension dim = kit.getScreenSize();
location = new Point(dim.width / 3, dim.height / 3);
Container container = getContentPane();
Icon icon = new ImageIcon(getClass().getResource("/images/File/new.jpg"));
JLabel label = new JLabel(icon);
container.add(label, BorderLayout.NORTH);
cancel = new JButton("取消");
cancel.addActionListener(this);
confirm = new JButton("确定");
confirm.addActionListener(this);
Box hori = Box.createHorizontalBox();
hori.add(Box.createHorizontalStrut(20));
hori.add(cancel);
hori.add(Box.createHorizontalStrut(250));
hori.add(confirm);
container.add(hori, BorderLayout.SOUTH);
setLocation(location);
setSize(420, 200);
setVisible(true);
setResizable(false);
}
{
if (event.getSource() == cancel)
{
freeTheResource();
}
if (event.getSource() == confirm)
{
this.setVisible(false);
// this.setModal(false);
if (firstStep == null)
{
firstStep = new FirstStep();
}
else
{
firstStep.setVisible(true);
}
}
}
private class FirstStep extends JDialog implements ActionListener
{
private JTextField nameOfRecord;
private JButton cancelStep, preStep, nextStep;
public FirstStep()
{
// super(mainFrame, true);
setTitle("新建账簿");
Container container = getContentPane();
Icon icon = new ImageIcon(getClass().getResource("/images/File/firstStep.jpg"));
JLabel northLabel = new JLabel(icon);
container.add(northLabel);
JPanel panelSouth = new JPanel();
Box top = Box.createHorizontalBox();
JLabel name = new JLabel("账簿名称");
nameOfRecord = new JTextField(20);
top.add(name);
top.add(nameOfRecord);
Box bottom = Box.createHorizontalBox();
cancelStep = new JButton("取消");
cancelStep.addActionListener(this);
preStep = new JButton("上一步");
preStep.addActionListener(this);
nextStep = new JButton("下一步");
nextStep.addActionListener(this);
bottom.add(cancelStep);
bottom.add(Box.createHorizontalStrut(60));
bottom.add(preStep);
bottom.add(nextStep);
Box vertical = Box.createVerticalBox();
vertical.add(top);
vertical.add(bottom);
panelSouth.add(vertical);
container.add(panelSouth, BorderLayout.SOUTH);
setLocation(location);
setSize(420, 200);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == cancelStep)
{
freeTheResource();
}
if (event.getSource() == preStep)
{
// firstStep.setModal(false);
firstStep.setVisible(false);
NewRecord.this.setVisible(true);
// NewRecord.this.setModal(true);
}
if (event.getSource() == nextStep)
{
String name = nameOfRecord.getText();
if (name.equals(""))
{
JOptionPane.showMessageDialog(null, "The field cannt be empty", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}
if (checkNameOfRecord (name))
{
JOptionPane.showMessageDialog(null, "Sorry, The name has already existed", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}
firstStep.setVisible(false);
// firstStep.setModal(false);
if (secondStep == null)
{
secondStep = new SecondStep();
}
else
{
secondStep.setVisible(true);
}
}
}
// to check if the name existed
private boolean checkNameOfRecord(String name)
{
Connection connection = null;
Statement statement = null;
try
{
Class.forName(DRIVER_FOR_DATEBASE);
connection = DriverManager.getConnection(URL_FOR_DATEBASE_USER);
statement = connection.createStatement();
ResultSet resultset = statement.executeQuery("SELECT * FROM 所有账簿");
while (resultset.next())
{
if (resultset.getString(1).equals(name))
return true;
}
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
freeTheResource();
}
catch (SQLException ex)
{
ex.printStackTrace();
freeTheResource();
}
finally
{
try
{
statement.close();
connection.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
freeTheResource();
}
}
return false;
}
public String getRecordName()
{
return nameOfRecord.getText();
}
}
private class SecondStep extends JDialog implements ActionListener
{
private JPasswordField password, confirmPassword;
private JComboBox nameOfCreator;
private JButton cancelStep, preStep, nextStep;
private String[] choices = {"管理员", "我"};
public SecondStep()
{
// super(mainFrame, true);
setTitle("新建账户");
Container container = getContentPane();
Icon icon = new ImageIcon(getClass().getResource("/images/File/secondStep.jpg"));
JLabel northLabel = new JLabel(icon);
container.add(northLabel, BorderLayout.NORTH);
JPanel panelSouth = new JPanel();
Box firstLine = Box.createHorizontalBox();
firstLine.add(Box.createHorizontalStrut(60));
JLabel name = new JLabel("建账者");
firstLine.add(Box.createHorizontalStrut(20));
nameOfCreator = new JComboBox(choices);
nameOfCreator.setEditable(true);
firstLine.add(name);
firstLine.add(nameOfCreator);
firstLine.add(Box.createHorizontalStrut(60));
Box secondLine = Box.createHorizontalBox();
secondLine.add(Box.createHorizontalStrut(60));
JLabel pass = new JLabel("密码 ");
secondLine.add(Box.createHorizontalStrut(10));
password = new JPasswordField(16);
secondLine.add(pass);
secondLine.add(password);
secondLine.add(Box.createHorizontalStrut(60));
Box thirdLine = Box.createHorizontalBox();
thirdLine.add(Box.createHorizontalStrut(60));
JLabel confirm = new JLabel("确认密码");
thirdLine.add(Box.createHorizontalStrut(5));
confirmPassword = new JPasswordField(16);
thirdLine.add(confirm);
thirdLine.add(confirmPassword);
thirdLine.add(Box.createHorizontalStrut(60));
Box vertical = Box.createVerticalBox();
vertical.add(firstLine);
vertical.add(Box.createVerticalStrut(5));
vertical.add(secondLine);
vertical.add(Box.createVerticalStrut(5));
vertical.add(thirdLine);
panelCenter.add(vertical);
Box bottom = Box.createHorizontalBox();
cancelStep = new JButton("取消");
cancelStep.addActionListener(this);
preStep = new JButton("上一步");
preStep.addActionListener(this);
nextStep = new JButton("下一步");
nextStep.addActionListener(this);
bottom.add(cancelStep);
bottom.add(Box.createHorizontalStrut(60));
bottom.add(preStep);
bottom.add(nextStep);
panelSouth.add(bottom);
panelCenter.setSize(200, 100);
container.add(panelCenter, BorderLayout.CENTER);
container.add(panelSouth, BorderLayout.SOUTH);
setLocation(location);
setSize(430, 260);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == cancelStep)
{
freeTheResource();
}
if (event.getSource() == preStep)
{
// secondStep.setModal(false);
secondStep.setVisible(false);
firstStep.setVisible(true);
// firstStep.setModal(true);
}
if (event.getSource() == nextStep)
{
if (!getPassWord().equals(getConfirmPass()))
{
password.setText("");
confirmPassword.setText("");
JOptionPane.showMessageDialog(null, "Password not match", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}
// secondStep.setModal(false);
secondStep.setVisible(false);
if (thirdStep == null)
{
thirdStep = new ThirdStep();
}
else
{
thirdStep.setVisible(true);
}
}
}
public String getUserName()
{
return nameOfCreator.getSelectedItem().toString();
}
public String getPassWord()
{
return new String(password.getPassword());
}
public String getConfirmPass()
{
return new String(confirmPassword.getPassword());
}
}
private class ThirdStep extends JDialog implements ActionListener
{
private JLabel nameOfRecord, userName;
private JButton cancelStep, preStep, nextStep;
public ThirdStep()
{
// super(mainFrame, true);
setTitle("新建账户");
Container container = getContentPane();
Icon icon = new ImageIcon(getClass().getResource("/images/File/thirdStep.jpg"));
JLabel northLabel = new JLabel(icon);
container.add(northLabel, BorderLayout.NORTH);
JPanel panelCenter = new JPanel();
JPanel panelSouth = new JPanel();
Box firstLine = Box.createHorizontalBox();
firstLine.add(Box.createHorizontalStrut(60));
JLabel name = new JLabel("账簿名称 ");
firstLine.add(Box.createHorizontalStrut(20));
firstLine.add(name);
nameOfRecord = new JLabel();
nameOfRecord.setText(firstStep.getRecordName());
firstLine.add(nameOfRecord);
firstLine.add(Box.createHorizontalStrut(60));
Box secondLine = Box.createHorizontalBox();
secondLine.add(Box.createHorizontalStrut(60));
JLabel user = new JLabel("用户名");
secondLine.add(user);
secondLine.add(Box.createHorizontalStrut(10));
userName = new JLabel();
userName.setText(secondStep.getUserName());
secondLine.add(userName);
secondLine.add(Box.createHorizontalStrut(60));
vertical.add(firstLine);
vertical.add(Box.createVerticalStrut(5));
vertical.add(secondLine);
vertical.add(Box.createVerticalStrut(5));
panelCenter.add(vertical);
Box bottom = Box.createHorizontalBox();
cancelStep = new JButton("取消");
cancelStep.addActionListener(this);
preStep = new JButton("上一步");
preStep.addActionListener(this);
nextStep = new JButton(" 完成 ");
nextStep.addActionListener(this);
bottom.add(cancelStep);
bottom.add(Box.createHorizontalStrut(60));
bottom.add(preStep);
bottom.add(nextStep);
panelSouth.add(bottom);
panelCenter.setSize(200, 100);
container.add(panelCenter, BorderLayout.CENTER);
container.add(panelSouth, BorderLayout.SOUTH);
setLocation(location);
setSize(430, 260);
setVisible(true);
setResizable(false);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == cancelStep)
{
freeTheResource();
}
if (event.getSource() == preStep)
{
thirdStep.setVisible(false);
// thirdStep.setModal(false);
secondStep.setVisible(true);
// secondStep.setModal(true);
}
if (event.getSource() == nextStep)
{
// thirdStep.setModal(false);
freeTheResource();
updateDataBase();
}
}
private void updateDataBase()
{
String nameOfRecord = firstStep.getRecordName();
String nameOfCreator = secondStep.getUserName();
String password = secondStep.getPassWord();
Connection connection = null;
Statement statement = null;
try
{
Class.forName(DRIVER_FOR_DATEBASE);
connection = DriverManager.getConnection(URL_FOR_DATEBASE_USER);
statement = connection.createStatement();
String updateStr = "INSERT INTO 所有账簿 VALUES" + "(" + "'" + nameOfRecord + "'" + "," +
"'" + nameOfCreator + "'" + "," + "'" + password + "'" + ")";
statement.executeUpdate(updateStr);
String command = "CREATE TABLE " + nameOfRecord + "(账户名称 varchar(20), 账户类型 varchar(20)," + "primary key (账户名称))";
statement.execute(command);
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
freeTheResource();
}
catch (SQLException ex)
{
ex.printStackTrace();
freeTheResource();
}
finally
{
try
{
statement.close();
connection.close();
}
catch(SQLException ex)
{
ex.printStackTrace();
freeTheResource();
}
}
}
}
private void freeTheResource()
{
this.dispose();
if (firstStep != null)
firstStep.dispose();
if (secondStep != null)
secondStep.dispose();
if (thirdStep != null)
thirdStep.dispose();
}
}
发现问题一: 如果通过一个接口函数得到最后一步种完成按钮的引用, 然后在其他地方
写进行响应的代码. 发现消息无法响应. 问题二: 发现上一步下一步的时候会出现空指针异常,
如果如上, 把所有的super(frame, true); (frame是那个父窗口)注释掉的就会没有问题.
事件也可以响应. 也不会出现空指针异常.
有那个仁兄遇到过这样的问题啊. 据说是模式对话框的线程阻塞了其他的线程. 有方法解决啊.