当前位置: 首页 > 知识库问答 >
问题:

关上窗户和打开其他窗户都有困难

孙翰墨
2023-03-14

我使用的是Java8,我正试图使用这些代码从一个帧移动到另一个帧。

问题:第一个JPanel没有关闭第二个JPanel在我尝试关闭窗口时出错

错误消息:

请帮帮忙。

public class ex2 {

    private JFrame frmLogin;
    private JTextField textField;
    private JPasswordField passwordField;

//cuted the methods to start the frame

    private void initialize() {
        frmLogin = new JFrame();
        frmLogin.getContentPane().setFont(new Font("Dialog", Font.BOLD, 18));
        frmLogin.setTitle("Login");
        frmLogin.setBounds(100, 100, 400, 300);
        frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmLogin.getContentPane().setLayout(null);

        JLabel lblUserName = new JLabel("Username");
        lblUserName.setBounds(130, 40, 150, 15);
        lblUserName.setFont(new Font("Dialog", Font.BOLD, 18)); 
        frmLogin.getContentPane().add(lblUserName);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(130, 100, 150, 15);
        lblPassword.setFont(new Font("Dialog", Font.BOLD, 18));
        frmLogin.getContentPane().add(lblPassword);

        textField = new JTextField();
        textField.setBounds(130, 70, 150, 19);
        textField.setFont(new Font("Dialog", Font.BOLD, 18));
        frmLogin.getContentPane().add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(130, 130, 150, 19);
        passwordField.setFont(new Font("Dialog", Font.BOLD, 18));
        frmLogin.getContentPane().add(passwordField);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String user = textField.getText();
                String pass = String.copyValueOf(passwordField.getPassword());

                if (user.equals("Rod") && pass.equals("123")) {
                    Main2 segundo = new Main2();

                    segundo.setVisible(true);
                    ex2.setVisible(false);

                }
            }
        });
        btnLogin.setBounds(130, 190, 150, 25);
        btnLogin.setFont(new Font("Dialog", Font.BOLD, 18));
        frmLogin.getContentPane().add(btnLogin);

}
public class Main2 extends JFrame {

    private JPanel contentPane;
    private static Main2 frame2;

    public Main2() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 400, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblEntramos = new JLabel("Entramos");
        lblEntramos.setFont(new Font("Dialog", Font.BOLD, 22));
        lblEntramos.setBounds(150, 100, 200, 15);
        contentPane.add(lblEntramos);

        JButton btnSair = new JButton("Sair");
        btnSair.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {             
                frame2.setVisible(false);
            }
        });     
        btnSair.setBounds(125, 175, 150, 25);
        contentPane.add(btnSair);           
    }
}

共有1个答案

长孙鸿
2023-03-14

我使用下面的代码复制了您的登录表单,我没有得到任何错误…

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
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.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class ex3 extends JFrame {

    public JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ex3 frame = new ex3();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ex3() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);



        JLabel lblUserName = new JLabel("Username");
        lblUserName.setBounds(130, 40, 150, 15);
        lblUserName.setFont(new Font("Dialog", Font.BOLD, 18));
        contentPane.add(lblUserName);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(130, 100, 150, 15);
        lblPassword.setFont(new Font("Dialog", Font.BOLD, 18));
        contentPane.add(lblPassword);

        textField = new JTextField();
        textField.setBounds(130, 70, 150, 19);
        textField.setFont(new Font("Dialog", Font.BOLD, 18));
        contentPane.add(textField);
        textField.setColumns(10);

        passwordField = new JPasswordField();
        passwordField.setBounds(130, 130, 150, 19);
        passwordField.setFont(new Font("Dialog", Font.BOLD, 18));
        contentPane.add(passwordField);

        JButton btnLogin = new JButton("Login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String user = textField.getText();
                String pass = String.copyValueOf(passwordField.getPassword());

                if (user.equals("Rod") && pass.equals("123")) {
                    Main2 segundo = new Main2();

                    segundo.setVisible(true);
                    setVisible(false);

                }
            }
        });
        btnLogin.setBounds(5, 196, 424, 60);
        btnLogin.setFont(new Font("Dialog", Font.BOLD, 18));
        contentPane.add(btnLogin);
    }

}

你确定你“删掉”的表单代码没有出错吗?

 类似资料:
  • 点击咨询入口时通过类方法调用以下方法,创建聊窗实体类,给参数传值。 1)请求指定接待组内的客服,需要给参数类chatParems的settingId属性传入指定的接待组id,具体的接待组id可以登录客服端,打开【设置-接入设置-智能分配】中创建和查看; 注意: 1、初始化方法要在该打开聊窗操作之前;该方法不可与初始化和登录等方法同时调用! 2、要保证传入的接待组存在且可用(即保证接待组是存在的且接

  • 打开聊窗 点击咨询入口时只需要调用startChat方法,前提是已经执行了initSDK初始化方法。 startChat方法可以请求指定接待组内的客服,需要用到ChatParamsBody对象传入指定的接待组id,必填。 参数 类型 是否必传 说明 来源 chatparams ChatParamsBody 是 ChatParamsBody是聊天实体,有商品id,接待组id和erpparam参数 t

  • 新窗口打开链接 该方法可以新起webview打开页面,由于客户端窗口过多会占用较多内存,请谨慎使用 Tida.pushWindow("http://www.tmall.com/go/chn/common/u-award.php?disableptf=1"); 关闭窗口 关闭当前webview窗口 Tida.popWindow();

  • 我试图创建一个程序,它有一个tkinter窗口打开,然后当你按下一个按钮,它关闭tkinter窗口,打开一个pyplay窗口。然而,当我点击按钮打开pyplay窗口时,它会打开pyplay窗口,而tkinter窗口保持打开状态。 代码: 我还尝试使用: 我怎样才能解决这个问题?(我正在MacOS 11.1上运行Python 3.7.7)

  • 问题内容: 我将Python 2与和一起使用,然后使用。 该程序运行良好,但是每次打开该程序时,窗口都会显示为隐藏状态,因此,直到我单击扩展坞上的图标以将其打开时,该窗口才会出现。 有什么方法可以控制此,使该窗口位于应用程序启动时打开的其他窗口之上? 需要澄清的是,在应用程序运行的整个过程中,它不一定都位于最前面。我只需要它在启动时位于其他窗口之上。 问题答案: 如果我接受您提供的代码并添加第一行

  • 我需要帮助我的代码。我已经通读了几次代码,我没有看到它有什么问题。期望用户提交作业申请并将用户重定向到仪表板,但它没有提交作业申请,也没有将用户定向到仪表板。这是我的代码:mode.py Views.py 从django.contrib.auth.decorators进口login_required 从。窗体导入addJobForm,应用程序窗体从。模型导入作业 defjob_detail(请求,