java - JButton 不会显示另一个 jframe

标签 java swing awt jbutton

我目前正在开发一个项目,每当我单击 jframe 2(登录后的第二个 jframe)“设置约会”/btn1 上的 jbutton 时,它都不会显示另一个 jframe,即 jframe3。 程序本身可以工作,但按钮不会显示其他 jframe。

import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Container;

public class me {
public static void main (String [] args) {
    JFrame jframe = new JFrame();
    jframe.setSize(450,350);
    jframe.getContentPane().setBackground(Color.WHITE);

    ImageIcon c = new ImageIcon("teethlogo5.png");
    JLabel bi = new JLabel("",c,JLabel.RIGHT);
    bi.setBounds(25,35,400,40);
    jframe.add(bi);

    ImageIcon a = new ImageIcon("teethlogo2.png");
    JLabel si = new JLabel("",a,JLabel.RIGHT);
    si.setBounds(50,90,100,120);
    jframe.add(si);


    JLabel jl1 = new JLabel("Username:");
    jl1.setBounds(190,100,100,50);
    jframe.add(jl1);

    JTextField uss = new JTextField();
    uss.setBounds(270,110,120,30);
    jframe.add(uss);

    JLabel jl2 = new JLabel("Password:");
    jl2.setBounds(190,150,100,50);
    jframe.add(jl2);

    JPasswordField pss = new JPasswordField();
    pss.setBounds(270,160,120,30);
    jframe.add(pss);

    JButton enter = new JButton("log in");
    enter.setBounds(250,210,100,40);
    jframe.add(enter);

    enter.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            String userText;
            String pwdText;
            userText = uss.getText();
            pwdText = String.valueOf(pss.getPassword());

            if (userText.equals("user") && pwdText.equals("pass")) {
                JOptionPane.showMessageDialog(null, "LoginSuccessful","Message",JOptionPane.PLAIN_MESSAGE);
                jframe.setVisible(false);
                JFrame jframe2 = new JFrame();
                jframe2.setSize(850,560);
                jframe2.getContentPane().setBackground(Color.WHITE);

                ImageIcon b = new ImageIcon("teethlogo4.png");
                JLabel sii = new JLabel("",b,JLabel.RIGHT);
                sii.setBounds(10,0,600,100);
                jframe2.add(sii);


                JButton btn1 = new JButton("Set an Appointment");
                btn1.setBounds(100,100,150,30);
                jframe2.add(btn1);

                JButton btn2 = new JButton("View Appointments");
                btn2.setBounds(270,100,150,30);
                jframe2.add(btn2);

                btn1.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        if (btn1.isSelected()){
                            jframe2.setVisible(false);
                            JFrame jframe3 = new JFrame();
                            jframe3.setSize(850,560);
                            jframe3.getContentPane().setBackground(Color.WHITE);



                            jframe3.setLayout(null);
                            jframe3.setVisible(true);
                            jframe3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        }
                    }
                });

                jframe2.setLayout(null);
                jframe2.setVisible(true);
                jframe2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            } 

            else { 
                JOptionPane.showMessageDialog(null, "Invalid Username or Password","Message",JOptionPane.PLAIN_MESSAGE);
            uss.setText(null);
            pss.setText(null);
            }
        }
    });


    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setResizable(false);
    jframe.setLayout(null);
    jframe.setVisible(true);
    }
}

我是java初学者,我真的很想知道如何解决这个问题。

最佳答案

你这是在搬起石头砸自己的脚:

if (btn1.isSelected()){
    // ...
}

不需要这段代码——按钮不会被“选择”,除非它从 JToggleButton 扩展(例如 JCheckBox)并且已经被检查,这是 JButton 不允许的,而且,它阻止了它所持有的监听器代码的运行。解决方案:将其删除即可。

关于java - JButton 不会显示另一个 jframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57343696/

相关文章:

java - 通过 Servlet 设置属性

java - 如何运行另一个类的方法?

java Graphics2D 的drawString质量

java - JTextField/JTextArea 未显示

java - 在哪里可以找到一组带有注入(inject)错误的Java测试程序?

将纬度/经度转换为 MGRS 坐标的 Java 库,反之亦然?

java - 非突然关闭JFrame结束程序中的JButton

java - 通过单击列表上的项目填充文本区域

java - 当滚动条位于底部时将其滚动到底部

java - 在按键时添加 JLabel