I'm currently working on a project and whenever I click the jbutton on jframe 2 (the 2nd jframe after the log in) "set appointments"/btn1, it won't show the other jframe which is jframe3. The program itself works but the button won't show the other 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);
}
}
I'm a beginner in java and I really want to know how to fix this problem.
You're shooting your own self in the foot with:
if (btn1.isSelected()){
// ...
}
There is no need for this block of code -- a button isn't "selected" unless it extends from JToggleButton (such as JCheckBox) and has been checked, something that a JButton does not allow, and what is more, it is preventing the listener's code that it holds from running. Solution: just get rid of it.