【问题标题】:Second frame window linked to 1st frame is not showing the content-java链接到第一帧的第二帧窗口未显示内容 java
【发布时间】: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。将所有组件添加到框架后,只需在构造函数中显示“框架”即可。

标签: java swing jframe


【解决方案1】:

你确实...

public class LoginGUI1 extends JFrame {

那你就……

private JFrame frame;
//...
private void initialize() {
    frame = new JFrame();

那么,问题是,哪个框架是哪个?

所以,当你这样做时......

LoginGUI1 login = new LoginGUI1();
login.setVisible(true);
LaunchGUI.this.dispose();

您使“类”框架可见,而不是 LoginGUI1frame 属性可见。

这是一个很好的例子,说明了为什么你应该避免直接从JFrame 扩展。首先从JPanel 扩展

public class LoginGUI1 extends JPanel {

    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Create the application.
     */
    public LoginGUI1() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        // This is a bad idea
        setLayout(null);

        textField = new JTextField();
        textField.setBounds(258, 236, 116, 22);
        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);
        add(btnLogin);

        JLabel lblNewLabel = new JLabel("Username");
        lblNewLabel.setBounds(157, 239, 68, 16);
        add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Password");
        lblNewLabel_1.setBounds(163, 301, 56, 16);
        add(lblNewLabel_1);

        passwordField = new JPasswordField();
        passwordField.setBounds(258, 298, 116, 22);
        add(passwordField);
    }
}

现在,当你想显示它时,你可以将它添加到你想要的任何容器中,或者为它创建一个新窗口

LoginGUI1 login = new LoginGUI1();
JFrame frame = new JFrame();
frame.setBounds(100, 100, 721, 526);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(login);

反馈

null layouts 会回来骂你。与其浪费大量时间,不如花时间学习如何使用各种可用的布局管理器。详情请见A Visual Guide to Layout Managers

其实与其尝试创建多个窗口,不如考虑使用CardLayout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 2016-08-30
    • 2016-05-31
    • 2015-04-07
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多