我希望它在按下按钮时关闭活动窗口并打开新窗口。
我在它打开一个新窗口的地方得到它,但旧窗口仍然存在。我试图实现 frame.dispose(); 但我不认为我做对了。
这是我所拥有的
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPanel3 extends JPanel {
private JTextField jcomp1;
private JLabel jcomp2;
private JLabel jcomp3;
private JButton jcomp4;
private JLabel clock;
static final String ADD = "add";
public MyPanel3() {
//construct components
jcomp1 = new JTextField(5);
jcomp2 = new JLabel("How long were you parked?");
jcomp3 = new JLabel("Minutes");
jcomp4 = new JButton("Calculate Total");
clock = new JLabel("newLabel");
ImageIcon clockpic = new ImageIcon(
"/Users/bnproductions/ALL/123FinalProject/bin/clock2.gif");
JLabel clock = new JLabel(clockpic);
jcomp4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
final ImageIcon icon4 = new ImageIcon(
"/Users/bnproductions/ALL/123FinalProject/bin/money2.gif");
JOptionPane.showMessageDialog(null, " Your total is $0.00",
"Parking Total", +JOptionPane.INFORMATION_MESSAGE, icon4);
}
});
//adjust size and set layout
setPreferredSize(new Dimension(335, 92));
setLayout(null);
//add components
add(jcomp1);
add(jcomp2);
add(jcomp3);
add(jcomp4);
add(clock);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds(280, 30, 50, 20);
jcomp2.setBounds(100, 25, 175, 30);
jcomp3.setBounds(280, 15, 55, 20);
jcomp4.setBounds(110, 55, 150, 25);
clock.setBounds(5, 5, 90, 80);
}
public static void main(String[] args) {
JFrame frame = new JFrame("MyPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(new MyPanel3());
frame.pack();
frame.setVisible(true);
}
}