从actionlistener访问main中创建的JFrame

发布于 2024-12-17 09:49:01 字数 2333 浏览 1 评论 0 原文

我在主类中为我的程序创建了一个框架(大型机),我想从中添加和删除面板,以便在程序的不同屏幕之间切换。我的程序的第一个屏幕是登录面板,其中有一个开始按钮。当我按下开始按钮时,我想切换到菜单框架。

由于登录面板消失,removeAll 方法似乎工作正常,但当我使用添加、验证和重绘方法时,它的位置上什么也没有出现。我尝试在动作侦听器中明确引用大型机(即 mainframe.add(menu)),但它无法识别该对象。

提前致谢!

public class Main {

    public static JFrame mainframe = new JFrame();

    public static void main(String[] args) {

        // Create mainframe to add and remove panels from
        LoginPanel lp = new LoginPanel();
        System.out.println("mainframe created!");

        // Set size of mainframe
        mainframe.setBounds(0, 0, 500, 500);
        mainframe.add(lp);

        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the mainframe
        int w = mainframe.getSize().width;
        int h = mainframe.getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the mainframe
        mainframe.setLocation(x, y);
        mainframe.setVisible(true);     
    }
}

这是我的登录面板类:

public class LoginPanel extends JPanel {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private final Action action = new SwingAction();

    /**
     * Create the panel.
     */
    public LoginPanel() {

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String (passwordField.getPassword());

                Login login = new Login();
                boolean Correct = login.isCorrect(username, password);
                **if (Correct == true){
                    removeAll();
                    Menu menu = new Menu();
                    add(menu);
                    validate();  
                    repaint();
                    setBounds(0, 0, 500, 500);
                    System.out.println("Attempted to start menu!");
                }**
            }
        });
        btnLogin.setAction(action);
        btnLogin.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

        }});

}

I've created a frame (mainframe) for my program in the main class which I want to add and remove panels from in order switch between different screens of my program. The first screen of my program is the login panel which has a start button. When I press the start button I want to switch to the menu frame.

The removeAll method seems to work fine since the login panel disappears, but nothing appears in its place when I use the add, validate and repaint methods. I have tried to refer explicitly to the mainframe in the actionlistener (i.e. mainframe.add(menu)) but it does not recognise the object.

Thanks in advance!

public class Main {

    public static JFrame mainframe = new JFrame();

    public static void main(String[] args) {

        // Create mainframe to add and remove panels from
        LoginPanel lp = new LoginPanel();
        System.out.println("mainframe created!");

        // Set size of mainframe
        mainframe.setBounds(0, 0, 500, 500);
        mainframe.add(lp);

        // Get the size of the screen
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the mainframe
        int w = mainframe.getSize().width;
        int h = mainframe.getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the mainframe
        mainframe.setLocation(x, y);
        mainframe.setVisible(true);     
    }
}

This is my login panel class:

public class LoginPanel extends JPanel {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private final Action action = new SwingAction();

    /**
     * Create the panel.
     */
    public LoginPanel() {

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String (passwordField.getPassword());

                Login login = new Login();
                boolean Correct = login.isCorrect(username, password);
                **if (Correct == true){
                    removeAll();
                    Menu menu = new Menu();
                    add(menu);
                    validate();  
                    repaint();
                    setBounds(0, 0, 500, 500);
                    System.out.println("Attempted to start menu!");
                }**
            }
        });
        btnLogin.setAction(action);
        btnLogin.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

        }});

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛泛之交 2024-12-24 09:49:01

我想添加和删除面板,以便在程序的不同屏幕之间切换

听起来您应该使用 卡片布局

I want to add and remove panels from in order switch between different screens of my program

Sounds like you should be using a Card Layout.

冰雪之触 2024-12-24 09:49:01

mainframe 定义为类字段:

private JFrame mainframe;

Define mainframe as a class field:

private JFrame mainframe;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文