可以在jframe的main方法中自身new出的类保留起来,这里比方你的JFrame叫ListPrice
public class ListPrice extends JFrame
{
private static ListPrice thisClass;
//你的其他操作。。。。
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
thisClass = new ListPrice();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
//这个ListPrice中有一个JComboBox,列举了打印机的种类。有一个方法是刷新表中的DefaultComboBoxModel,一个方法是从数据库中获得打印机的类型。现在要做的是,如何在弹出的PrintSetDialog中变动了打印机之后,能在ListPrice中立即显示。
//ListPrice添加如下操作
public DefaultComboBoxModel getPrintsDefaultComboBoxModel()
{
//从数据库中查出打印机
}
//刷新打印机Model
public void flushJComboBox()
{
jComboBox.setModel(getPrintsDefaultComboBoxModel());
}
//生成JComboBox
private JComboBox getJComboBox()
{
if (jComboBox == null)
{
jComboBox = new JComboBox(getPrintsDefaultComboBoxModel());
}
return jComboBox;
}
//创建打印机设置窗口是将,父jframe的参数传递进去
PrintSetDialog pb = new PrintSetDialog(thisClass);
//在弹出的PrintSetDialog中的添加关闭操作
private ListPrice pl;
public class PrintSetDialog extends JDialog
{
public PrintSetDialog(ListPrice owner) //获得父窗体的对象
{
super(owner);
initialize();
pl = owner;
}
//你的其他操作。。。。
//添加一个关闭PrintSetDialog 时,刷新ListPrice 中的JComboBox的方法。
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(java.awt.event.WindowEvent e)
{
System.out.println("设置打印机窗口准备关闭!");
pl.flushJComboBox(); //对父窗体刷新
}
});