【问题标题】:Accessing JFrame created in main from actionlistener从 actionlistener 访问 main 中创建的 JFrame
【发布时间】:2011-11-21 16:56:40
【问题描述】:

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

removeAll 方法似乎工作正常,因为登录面板消失了,但是当我使用 add、validate 和 repaint 方法时,它的位置没有出现任何内容。我试图在 actionlistener 中明确引用大型机(即 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) {

        }});

}

【问题讨论】:

  • 能否请您填写sscce.org 表单中的代码
  • 我随机猜测您是否要添加到已删除的嵌套面板中。您要添加到哪个面板的那个或其他一些错误。
  • 在相当基本的层面上,我想知道为什么在我的主类中公开静态的 JFrame 无法在另一个类中解决:S - 有什么想法吗? @mKorbel

标签: java swing jframe jpanel actionlistener


【解决方案1】:

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

听起来你应该使用Card Layout

【讨论】:

  • 我解决了!我所做的是引用制作框架的类:Main.mainframe。然后在 actionlistener 部分,我创建了一个容器并将其用作引用它的更方便的方法,如下所示: Container content = Main.mainframe.getContent(); content.removeAll();等
【解决方案2】:

mainframe定义为类字段:

private JFrame mainframe;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2011-01-26
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多