为了账号安全,请及时绑定邮箱和手机立即绑定

JButton 不会显示另一个 jframe

JButton 不会显示另一个 jframe

吃鸡游戏 2023-05-17 14:39:48
我目前正在处理一个项目,每当我单击 jframe 2(登录后的第二个 jframe)上的 jbutton“设置约会”/btn1 时,它不会显示另一个 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);我是 Java 的初学者,我真的很想知道如何解决这个问题。
查看完整描述

1 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

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

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

不需要这个代码块——一个按钮不会被“选中”,除非它从 JToggleButton(例如 JCheckBox)扩展并且已经被选中,这是 JButton 不允许的,更重要的是,它正在阻止它从运行中保存的侦听器代码。解决方案:摆脱它。


查看完整回答
反对 回复 2023-05-17
  • 1 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信