我目前正在處理一個項目,每當我單擊 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 不允許的,更重要的是,它正在阻止它從運行中保存的偵聽器代碼。解決方案:擺脫它。
添加回答
舉報
0/150
提交
取消