我有一个jframe作为程序的一部分,用户将通过按添加按钮填充arraylist。当他们完成填充arraylist时,我希望他们在程序继续运行时单击完成按钮以隐藏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
答案 0 :(得分:1)
JFrame类的方法setVisible(boolean)
可用于更改JFrame的可见性。例如,this.setVisible(false);
隐藏它。
答案 1 :(得分:1)
请参阅The Use of Multiple JFrames, Good/Bad Practice?,了解您不应该避免目前的设计/思考的原因。
更好的设计是使用CardLayout
,有关详细信息,请参阅How to Use CardLayout