通过单击按钮打开新的JFrame
问题描述:
我想知道如何在第二个JFrame(2)中单击按钮时打开此JFrame表单(1)。问题是我无法获取Form 2中的.setVisible方法。请帮忙。谢谢&关心! :)通过单击按钮打开新的JFrame
表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()
只有PassengerForm
类叫setVisible(true)
被打开扩展JFrame。如果不是,你应该使用类似于:
PassengerForm window = new PassengerForm();
window.getFrame().setVisible(true)
+0
这不工作的人。 我已经解决了我自己的问题:D 我需要首先以我想要打开的形式扩展JPanel。 – niibz
[Java的可能重复通过单击按钮打开一个新窗口](http://stackoverflow.com/questions/11273267/java-open-a-new-window-by-clicking-a-button) – ControlAltDel
请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556) –