通过单击按钮打开一个新的 JFrame
Opening a new JFrame by clicking a button
我想知道如何在单击第二个 JFrame (2) 中的按钮时打开此 JFrame 窗体 (1)。问题是我无法在 Form 2 中获取 .setVisible 方法。请帮忙。感谢和问候 ! :)
表格 1(在表格 2 上单击按钮时打开
public class FlightForm {
public FlightForm() {
initialize();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FlightForm window = new FlightForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
表格 2
public class MainMenu{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainMenu window = new MainMenu();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainMenu() {
frame = new JFrame("Main Menu");
setBounds(100, 100, 830, 574);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Flight Form");
);
btnNewButton.setFont(new Font("Candara", Font.BOLD, 15));
btnNewButton.setBounds(169, 328, 193, 77);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Passenger Form");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PassengerForm window = new PassengerForm();
window.setVisible(true); // This is not working
仅当 PassengerForm
class 扩展了 JFrame 时,您才可以在 PassengerForm()
上调用 setVisible(true)
。如果不是,你应该使用类似的东西:
PassengerForm window = new PassengerForm();
window.getFrame().setVisible(true)
我想知道如何在单击第二个 JFrame (2) 中的按钮时打开此 JFrame 窗体 (1)。问题是我无法在 Form 2 中获取 .setVisible 方法。请帮忙。感谢和问候 ! :)
表格 1(在表格 2 上单击按钮时打开
public class FlightForm {
public FlightForm() {
initialize();
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FlightForm window = new FlightForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
表格 2
public class MainMenu{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainMenu window = new MainMenu();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainMenu() {
frame = new JFrame("Main Menu");
setBounds(100, 100, 830, 574);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Flight Form");
);
btnNewButton.setFont(new Font("Candara", Font.BOLD, 15));
btnNewButton.setBounds(169, 328, 193, 77);
frame.getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Passenger Form");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PassengerForm window = new PassengerForm();
window.setVisible(true); // This is not working
仅当 PassengerForm
class 扩展了 JFrame 时,您才可以在 PassengerForm()
上调用 setVisible(true)
。如果不是,你应该使用类似的东西:
PassengerForm window = new PassengerForm();
window.getFrame().setVisible(true)