我在做一个人员管理系统,创建好了测试程序,一个主界面,一个监听器,一个副界面
点击主界面的添加按钮,会弹出副界面,再点一次会再弹出一个,所以我把副界面在主界面中新建,再设置为不可显示,等到监听到鼠标点击了添加按钮,再设置为显示,那
这里面的参数传递应该怎么样的,我要在测试程序和主界面都添加副界面的属性?
通过引用传递,要确保传递过去的对象一定是指向同一个空间的,不然一旦出现了new关键字你就达不到效果了
//这是主界面
public class PersonManager
{
private JTable table=null;
private JFrame frame=null;
private DefaultTableModel model=null;
public JTable getTable()
{
return table;
}
public JFrame getFrame()
{
return frame;
}
public DefaultTableModel getModel()
{
return model;
}
public void mainManager()
{
frame=new JFrame(); //创建窗体
Container con=frame.getContentPane(); //创建窗体模型
con.setLayout(new FlowLayout()); //设置模型布局为流式
JLabel label=new JLabel("人事管理系统"); //创建主标签
con.add(label); //将主标签放在第一行
//创建菜单栏
JMenuBar bar=new JMenuBar();
JMenu file=new JMenu("File"); JMenu Help=new JMenu("Help");
JMenuItem a=new JMenuItem("添加"); JMenuItem b=new JMenuItem("删除");
JMenuItem c=new JMenuItem("查询"); JMenuItem d=new JMenuItem("退出");
JMenuItem e=new JMenuItem("导入"); JMenuItem f=new JMenuItem("导出");
//确定关系
bar.add(file); bar.add(Help);
file.add(a); file.add(b);
file.add(c); file.addSeparator();
file.add(e); file.add(f);
file.add(d);
frame.setJMenuBar(bar); //通过窗体设置菜单栏
JOptionPane.showMessageDialog(frame, "欢迎进入人事系统!");//输入对话框--消息对话框
String[] ss={"编号","名字","性别","部门","工资"}; //创建一个表格
model=new DefaultTableModel(ss,0); //创建表格模型
table=new JTable(model); //通过表格模型的对象创建表格
JScrollPane scp = new JScrollPane(table); //创建滚动面板ScrollPane,把表格对象放进去
con.add(scp); //把滚动面板添加到容器中
//创建按钮
JButton btn1=new JButton("添加");
JButton btn2=new JButton("删除");
JButton btn3=new JButton("修改");
JButton btn4=new JButton("更新");
JButton btn5=new JButton("查询");
//监听按钮
PersonListener pl=new PersonListener(this);
btn1.addActionListener(pl);
btn2.addActionListener(pl);
btn3.addActionListener(pl);
btn4.addActionListener(pl);
btn5.addActionListener(pl);
//将按钮添加到容器中
con.add(btn1);
con.add(btn2);
con.add(btn3);
con.add(btn4);
con.add(btn5);
frame.setVisible(true); //显示窗口
frame.setSize(480,565); //设置窗口的大小
frame.setResizable(false); //固定窗口不可拉伸
frame.setLocation(250, 150); //设置窗口的初始位置
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置默认关闭动作--运行结束
}
}
//这是监听器
public class PersonListener implements ActionListener
{
PersonManager pmi;
public PersonListener(PersonManager pmi)
{
super();
this.pmi = pmi;
}
@Override
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
if("添加".equals(str))
{
AddM addm=new AddM();
addm.addM();
addm.getFrame().setVisible(true);
}
else if("删除".equals(str))
{
}
else if("修改".equals(str))
{
}
else if("查询".equals(str))
{
}
else if("更新".equals(str))
{
}
else if("确定".equals(str))
{
}
else if("导入".equals(str))
{
}
else if("导出".equals(str))
{
}
}
}
//这是点击添加按钮后的界面
public class AddM
{
private JLabel title=null;
private JTextField tid=null;
private JTextField tname=null;
private JComboBoxjsex=null;
private JComboBoxjdept=null;
private JTextField tsalary=null;
private JFrame frame=null;
private JTextField ttitle=null;
public JLabel getTitle()
{
return title;
}
public void setTitle(JLabel title)
{
this.title = title;
}
public JTextField getTid()
{
return tid;
}
public void setTid(JTextField tid)
{
this.tid = tid;
}
public JTextField getTname()
{
return tname;
}
public void setTname(JTextField tname)
{
this.tname = tname;
}
public JComboBox<String> getJsex()
{
return jsex;
}
public JComboBox<String> getJdept()
{
return jdept;
}
public JTextField getTsalary()
{
return tsalary;
}
public void setTsalary(JTextField tsalary)
{
this.tsalary = tsalary;
}
public void setFrame(JFrame frame)
{
this.frame = frame;
}
public JFrame getFrame()
{
return frame;
}
public void addM()
{
frame=new JFrame(); //创建添加窗口
Container con=frame.getContentPane(); //创建添加窗口的容器
con.setLayout(new GridLayout(7,2)); //设置容器布局为网格布局,7行2列
title=new JLabel("Add");
ttitle=new JTextField(0); //编号标签的文本框
JLabel lid=new JLabel("编号:"); //设置编号标签
tid=new JTextField(10); //编号标签的文本框
JLabel lname=new JLabel("姓名:"); //设置姓名标签
tname=new JTextField(10); //姓名标签的文本框
JLabel lsex=new JLabel("性别:"); //设置性别标签
String[] s={"男","女"};
jsex=new JComboBox<String>(s); //下拉菜单,可选男女
JLabel ldept=new JLabel("部门:"); //设置部门标签
String[] sdept={"vendition","lead","secretary"};
jdept=new JComboBox<String>(sdept); //下拉菜单,可选vendition、lead、secretary
JLabel lsalary=new JLabel("工资:"); //设置工资标签
tsalary=new JTextField(10); //工资标签的文本框
JButton badd=new JButton("确定"); //创建确定按钮
JButton bcancel=new JButton("取消"); //创建取消按钮
//将按钮添加到容器中
con.add(title); con.add(ttitle);
con.add(lid); con.add(tid);
con.add(lname); con.add(tname);
con.add(lsex); con.add(jsex);
con.add(ldept); con.add(jdept);
con.add(lsalary); con.add(tsalary);
con.add(badd); con.add(bcancel);
frame.setVisible(false); //设置窗口为不可见
frame.setSize(300,300); //设置窗口大小
frame.setResizable(false); //设置窗口不可拉伸
frame.setLocation(500,500); //设置窗口初始位置
}
}
我想实现点一次添加按钮,只出现一个窗口,我应该怎么写呢,有没有大神给我解答下
监听类里面的那个new无视啊,刚才写了忘记删了