Java 通过单击按钮打开新的JFrame
我想知道当我点击第二个JFrame(2)中的按钮时,如何打开这个JFrame表单(1)。问题是我无法在表单2中获取.setVisible方法。请帮忙。谢谢和问候!:) 表格1(点击表格2上的按钮时打开)Java 通过单击按钮打开新的JFrame,java,swing,jframe,Java,Swing,Jframe,我想知道当我点击第二个JFrame(2)中的按钮时,如何打开这个JFrame表单(1)。问题是我无法在表单2中获取.setVisible方法。请帮忙。谢谢和问候!:) 表格1(点击表格2上的按钮时打开) public class FlightForm { public FlightForm() { initialize(); } public static void main(String[] args) { EventQueue.in
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();
}
}
}
}
}
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
表格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();
}
}
}
}
}
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
类扩展了JFrame时,才可以在PassengerForm()上调用setVisible(true)
。如果没有,则应该使用如下内容:
PassengerForm window = new PassengerForm();
window.getFrame().setVisible(true)
可能重复的See This not work man。我已经自己解决了:D我需要首先以我想要打开的形式扩展一个JPanel。