package viewer;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.scene.layout.Border;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import util.EmpUtil;
import model.EmpException;
import model.User;
public class UserPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JPanel jp1, jp2;
private JLabel jl1;
private JTable jt;
private JScrollPane jsp;
private JButton jb1, jb2, jb3;
private JTable jtable;
private UserTableModel usermodel;
private AddDialog ad;
private JFrame jf;
public UserPanel(JFrame jf) {
this.setLayout(new BorderLayout());
this.jf = jf;
jp1 = new JPanel();
jp2 = new JPanel();
jl1 = new JLabel("用户管理界面");
jb1 = new JButton("添加用户");
jb1.addActionListener(new UserManagerClick());
jb2 = new JButton("删除用户");
jb2.addActionListener(new UserManagerClick());
jb3 = new JButton("修改用户");
jb3.addActionListener(new UserManagerClick());
jp1.add(jl1);
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
usermodel = new UserTableModel();
jtable = new JTable(usermodel);
jsp = new JScrollPane(jtable);
this.add(jp1, BorderLayout.NORTH);
this.add(jp2, BorderLayout.SOUTH);
this.add(jsp);
}
private class UserManagerClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb1) {
// 添加
ad = new AddDialog();
} else if (e.getSource() == jb2) {
// 删除
} else if (e.getSource() == jb3) {
// 修改
}
}
}
private class AddDialog extends JDialog {
private JLabel jl1, jl2, jl3;
private JButton jb1, jb2;
private JPanel jp1, jp2, jp3, jp4;
private JTextField jtf1, jtf2;
private JPasswordField jpf;
private JDialog jdg=this;
public AddDialog() {
this.setSize(300, 200);
this.setModal(true); // 设置为模态对话框,不能操作主窗体。如果这里用JFrame就无法实现该功能了。
this.setLocation(jf.getX() + 50, jf.getY() + 50);
this.setTitle("添加用户信息");
this.setLayout(new GridLayout(4, 1));
jp1 = new JPanel();
jp2 = new JPanel();
jp3 = new JPanel();
jp4 = new JPanel();
jl1 = new JLabel("用 户 名:");
jl2 = new JLabel("用户密码:");
jl3 = new JLabel("用户昵称:");
jb1 = new JButton("添加用户");jb1.addActionListener(new AddDialogClick());
jb2 = new JButton("重置数据");jb2.addActionListener(new AddDialogClick());
jtf1 = new JTextField(20);
jtf2 = new JTextField(20);
jpf = new JPasswordField(20);
jp1.add(jl1);
jp1.add(jtf1);
jp2.add(jl2);
jp2.add(jpf);
jp3.add(jl3);
jp3.add(jtf2);
jp4.add(jb1);
jp4.add(jb2);
this.add(jp1);
this.add(jp2);
this.add(jp3);
this.add(jp4);
this.pack();
this.setResizable(false);
this.setVisible(true);
}
private void reset() {
jtf1.setText("");
jtf2.setText("");
jpf.setText("");
}
private class AddDialogClick implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
if (e.getSource() == jb1) {
// 添加用户
String username=jtf1.getText();
if (username == null
||"".equals(username.trim())) {
//属于哪个窗口就是当弹出提示对话框时,非属于的其他窗体全部隐藏不显示.
/* JOptionPane.showMessageDialog(jdg, "请输入正确的用户名!", "发现错误!",
JOptionPane.ERROR_MESSAGE);*/
EmpUtil.showError(jdg, "请输入正确的用户名!");
return;//必须得有return。虽然弹出提示对话框,但是程序还是继续往下执行的。
}
String password=new String(jpf.getPassword());
String nickname=jtf2.getText();
User u=new User();
u.setUsername(username);
u.setPassword(password);
u.setNickname(nickname);
usermodel.getUd().add(u);
AddDialog.this.setVisible(false);
//ad.dispose();
} else if (e.getSource() == jb2) {
// 重置数据
reset();
}
} catch (EmpException e1) {
EmpUtil.showError(jdg, e1.getMessage());
}
}
}
}}
AddDialog.this.setVisible(false);为什么这句改成ad.setVisible(false);
会报空指针异常。