我有一个jframe作为程序的一部分,用户将通过按下添加按钮来填充数组列表。当他们完成填充数组列表时,我希望他们在程序继续运行时单击完成按钮来隐藏jframe。当单击按钮时隐藏JFrame而不停止程序
有没有办法做到这一点。最初我使用System.exit(0),但这似乎终止了程序。
public class Street extends JFrame {
private static final Random randomNumbers = new Random();
private ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();
private JLabel messageJLabel; // displays vehicle that was added
private JButton addBicycleJButton;
private JButton addCarJButton;
private JButton doneJButton;
private Color background; // background color of application
public Street() {
super("Street Simulation");
background = Color.LIGHT_GRAY;
messageJLabel = new JLabel("No vehicles added.");
addBicycleJButton = new JButton("Add Bicycle");
addBicycleJButton.addActionListener(
new ActionListener() // anonymous inner class
{
public void actionPerformed(ActionEvent e) {
background = Color.LIGHT_GRAY;
Bicycle b = new Bicycle(2, 0);
vehicles.add(b);
messageJLabel.setText(b.toString()
+ " added to vehicles");
repaint();
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener
addCarJButton = new JButton("Add Car");
addCarJButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
background = Color.LIGHT_GRAY;
Car c = new Car(4, 0);
vehicles.add(c);
messageJLabel.setText(c.toString() + " added to vehicles");
repaint();
} // end method actionPerformed
} // end anonymous inner class
);// end call to addActionListener
doneJButton = new JButton("Done");
doneJButton.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// code to exit goes here
background = Color.LIGHT_GRAY;
//I would like to have the jframe running in the background after this button is pushed.
// System.exit(0);
repaint();
} // end method actionPerformed
} // end anonymous inner class
);// end call to addActionListener
setLayout(new FlowLayout());
add(addBicycleJButton);
add(addCarJButton);
add(doneJButton);
add(messageJLabel);
} // end street constructor
? 'setVisible(false)'? '处置()'?你知道在一个程序中有一堆JFrames通常是新手程序的一个标志,并且经常表明该设计可能从改变中受益。相反,为什么不使用CardLayout更改视图。或者如果你需要一个模态窗口,然后使用模态JDialog。 – 2014-11-22 22:01:22
请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556)。更好的设计是使用'CardLayout',请参阅[如何使用CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)以获得更多详细信息 – MadProgrammer 2014-11-22 22:06:50
为了更快地获得更好的帮助,发布[MCVE](http://stackoverflow.com/help/mcve)(最小完整可验证示例)或[SSCCE](http://www.sscce.org/)(简短,独立,正确的示例)。 – 2014-11-23 01:05:13