【问题标题】:Redrawing a new JFrame in an existing one?在现有的 JFrame 中重绘新的 JFrame?
【发布时间】:2014-05-26 06:27:48
【问题描述】:

我试图在用户在他们正在使用的同一窗口中执行某些操作后显示不同的 JFrame,类似于登录功能。还没想好怎么做。

我现在的解决方法是隐藏当前的 JFrame,然后打开一个新的,模拟类似的效果。但理想情况下,我希望它只在同一个现有窗口中显示下一个 JFrame。

import java.awt.FlowLayout;
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.JTextField;

public class Login extends JFrame {
    private static int x = 0;
    static JTextField txtInput = new JTextField(10);
    static JButton btnSwitch = new JButton("Log on");

    public Login(){
        setLayout(new FlowLayout());

        //add button and register
        add(new JLabel("Enter password:"));
        add(txtInput);
        add(btnSwitch);
    }

    public static void main(String[] args) {
        final JFrame frame = new Login();

        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 150);
        frame.setVisible(true);


        btnSwitch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(txtInput.getText().equals("123")){
                    //frame = new GUIHelloWorld(); this doesn't work because "The final local variable frame cannot be assigned, since it is defined in an enclosing type"
                    //so I went with the below workaround
                    GUIHelloWorld frame = new GUIHelloWorld();

                    frame.setLocationRelativeTo(null); // Center the frame
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(300, 100);
                    frame.setVisible(true);
                }
            }
        });
    }
}

一旦用户通过了 GUI 的第一部分,我想向他们展示其他类似的东西:

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class GUIHelloWorld extends JFrame {
    public GUIHelloWorld(){
        setLayout(new GridLayout(0,1));

        add(new JLabel("Hello World"));
        add(new JLabel("Welcome to the 2nd part of the GUI"));
    }

    public static void main(String[] args) {
        JFrame frame = new GUIHelloWorld();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 100);
        frame.setVisible(true);
    }

}

有人可以告诉我如何在用户正在使用的现有窗口中显示新的 JFrame 吗?

【问题讨论】:

    标签: java swing layout-manager jdialog cardlayout


    【解决方案1】:
    1. 不要从JFrame扩展,尤其是在这种情况下,框架不能添加到其他框架。相反,基于您的个人 UI 视图,例如 JPanel
    2. 创建JFrame 的单个实例,将其布局管理器设置为使用CardLayout。
    3. 将每个视图添加到框架中,并适当命名每个视图
    4. 根据需要使用CardLayout在视图之间切换

    您也可以考虑在登录窗口中使用JDialog,但基本建议仍然存在;创建窗口,扩展组件...

    【讨论】:

    • 谢谢!直到现在才知道这个布局管理器,似乎它正是我所需要的。还要感谢关于不要扩展 JFrame 而不是 JPanel 的建议。
    • @rawrrang 很高兴它有帮助 ;)
    猜你喜欢
    • 2015-12-18
    • 2014-06-12
    • 2016-07-06
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多