【发布时间】:2021-11-12 05:21:50
【问题描述】:
我正在使用 eclipse 进行作业,第二帧内容没有显示任何内容,只有窗口正在打开,这意味着窗口 1 关闭,窗口 2 打开,但我创建的内容和设计没有显示
这是第一个窗口代码
package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LaunchGUI extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LaunchGUI frame = new LaunchGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LaunchGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 722, 470);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("Sign In");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoginGUI1 login = new LoginGUI1();
login.setVisible(true);
LaunchGUI.this.dispose();
}
});
btnNewButton.setBounds(35, 211, 97, 25);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("Sign Out");
btnNewButton_1.setBounds(543, 211, 97, 25);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Start Game");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainGameGUI start = new MainGameGUI();
start.setVisible(true);
LaunchGUI.this.dispose();
}
});
btnNewButton_2.setBounds(292, 211, 97, 25);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("View Score");
btnNewButton_3.setBounds(494, 339, 97, 25);
contentPane.add(btnNewButton_3);
JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnRegister.setBounds(61, 339, 97, 25);
contentPane.add(btnRegister);
}
}
这是第二个窗口代码
package GUI;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class LoginGUI1 extends JFrame{
private JFrame frame;
private JTextField textField;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginGUI1 window = new LoginGUI1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public LoginGUI1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 721, 526);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(258, 236, 116, 22);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnLogin.setBounds(258, 386, 97, 25);
frame.getContentPane().add(btnLogin);
JLabel lblNewLabel = new JLabel("Username");
lblNewLabel.setBounds(157, 239, 68, 16);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Password");
lblNewLabel_1.setBounds(163, 301, 56, 16);
frame.getContentPane().add(lblNewLabel_1);
passwordField = new JPasswordField();
passwordField.setBounds(258, 298, 116, 22);
frame.getContentPane().add(passwordField);
}
}
我似乎无法理解我所犯的错误请帮忙!!!!
【问题讨论】:
-
您的代码扩展了 JFrame,然后在构造函数中创建另一个 JFrame,因此您有两个框架实例。您使可见的那个没有组件。不要扩展 JFrame。将所有组件添加到框架后,只需在构造函数中显示“框架”即可。