Java 用另一个类的JButton换卡
我正在一步一步地到达那里,但遇到了另一个障碍 这个标题是非常不言自明的,我想知道的是我如何能够使用带有ActionListener的JButton“logout”来交换我的主LoginScreen.class中的卡 我打了几下,但没用。此外,任何关于改进我的编码和格式的提示都是欢迎的。提前谢谢 这是我的密码: LoginScreen.classJava 用另一个类的JButton换卡,java,swing,jbutton,cardlayout,Java,Swing,Jbutton,Cardlayout,我正在一步一步地到达那里,但遇到了另一个障碍 这个标题是非常不言自明的,我想知道的是我如何能够使用带有ActionListener的JButton“logout”来交换我的主LoginScreen.class中的卡 我打了几下,但没用。此外,任何关于改进我的编码和格式的提示都是欢迎的。提前谢谢 这是我的密码: LoginScreen.class /*Login Screen class for allowing multiple levels of access and security*/
/*Login Screen class for allowing
multiple levels of access and security*/
//Imports library files
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
//Creates a LoginScreen class that extends the JFrame library class
class LoginScreen extends JFrame {
//Creates a swing components and CardLayout for organising JPanels
JPanel cardScreen;
JPanel screen = new JPanel();
Image ProgramIcon = Toolkit.getDefaultToolkit().getImage("imageIco.png");
ImageIcon logo = new ImageIcon ("Logo.png");
JLabel icon = new JLabel(logo);
JLabel username = new JLabel("Username");
JLabel password = new JLabel("Password");
JTextField user = new JTextField(18);
JPasswordField pass = new JPasswordField(18);
JButton login = new JButton("Login");
JLabel errorInfo = new JLabel("");
int WIDTH = 800;
int HEIGHT = 500;
int currentPanel = 1;
public static void main(String[] args){
//Sets the GUI (Look and Feel) to the NimROD theme
try {UIManager.setLookAndFeel("com.nilo.plaf.nimrod.NimRODLookAndFeel");}
catch (UnsupportedLookAndFeelException e){ JOptionPane.showMessageDialog(null, "GUI Load Error: Unsupported");}
catch (ClassNotFoundException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: NimROD Missing");}
catch (InstantiationException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: Instantiation Missing");}
catch (IllegalAccessException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: Illegal Access"); }
//Creates a new LoginScreen via the LoginScreen method
LoginScreen LS = new LoginScreen();
}
public LoginScreen(){
//Adds the JPanel to the JFrame and set the JFrame's properties
//Sets the main JPanel to CardLayout platform and adds other JPanels it
final CardLayout cardL = new CardLayout();
cardScreen = new JPanel();
cardScreen.setLayout(cardL);
cardScreen.add(screen, "1");;
BaseScreen base = new BaseScreen();
cardScreen.add(base, "2");
this.setIconImage(ProgramIcon);
this.setTitle("Login");
this.setSize(WIDTH,HEIGHT);
this.setResizable(false);
this.add(cardScreen);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//Place the components on the JPanel and set their absolute posistions
screen.setLayout(null);
screen.add(username);
screen.add(password);
screen.add(user);
screen.add(pass);
screen.add(login);
screen.add(icon);
Dimension iconSize = icon.getPreferredSize();
Dimension usernameSize = username.getPreferredSize();
Dimension passwordSize = password.getPreferredSize();
Dimension loginSize = login.getPreferredSize();
Dimension userSize = user.getPreferredSize();
Dimension passSize = pass.getPreferredSize();
username.setBounds(252,170,usernameSize.width,usernameSize.height);
password.setBounds(495,170,passwordSize.width,passwordSize.height);
user.setBounds(180,200,userSize.width,userSize.height);
pass.setBounds(420,200,passSize.width,passSize.height);
login.setBounds(375,250,loginSize.width,loginSize.height);
icon.setBounds(250,50,iconSize.width,iconSize.height);
this.setVisible(true);
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//Checks if both the user and pass text fields are empty
if((user.getText().equals("")) && (pass.getText().equals(""))){
//Displays an error in the form of a label and adds it to the JPanel
errorInfo.setText("Please enter username and password");
screen.add(errorInfo);
errorInfo.setForeground(Color.RED);
Dimension errorInfoSize = errorInfo.getPreferredSize();
errorInfo.setBounds(300,300,errorInfoSize.width,errorInfoSize.height);
}
if((user.getText().equals("admin"))&&(pass.getText().equals("password"))){
cardL.show(cardScreen,"2");
}
}
});
}
}
//Basescreen class
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class BaseScreen extends JPanel{
JPanel screen = this;
JButton logout = new JButton("Logout");
ImageIcon title = new ImageIcon("title.png");
JLabel header = new JLabel(title);
public BaseScreen(){
screen.setLayout(null);
screen.add(logout);
screen.add(header);
Dimension headerSize = header.getPreferredSize();
Dimension logoutSize = logout.getPreferredSize();
logout.setBounds(720,440,logoutSize.width,logoutSize.height);
header.setBounds(0,0,headerSize.width,headerSize.height);
screen.setVisible(true);
}
}
BaseScreen.class
/*Login Screen class for allowing
multiple levels of access and security*/
//Imports library files
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
//Creates a LoginScreen class that extends the JFrame library class
class LoginScreen extends JFrame {
//Creates a swing components and CardLayout for organising JPanels
JPanel cardScreen;
JPanel screen = new JPanel();
Image ProgramIcon = Toolkit.getDefaultToolkit().getImage("imageIco.png");
ImageIcon logo = new ImageIcon ("Logo.png");
JLabel icon = new JLabel(logo);
JLabel username = new JLabel("Username");
JLabel password = new JLabel("Password");
JTextField user = new JTextField(18);
JPasswordField pass = new JPasswordField(18);
JButton login = new JButton("Login");
JLabel errorInfo = new JLabel("");
int WIDTH = 800;
int HEIGHT = 500;
int currentPanel = 1;
public static void main(String[] args){
//Sets the GUI (Look and Feel) to the NimROD theme
try {UIManager.setLookAndFeel("com.nilo.plaf.nimrod.NimRODLookAndFeel");}
catch (UnsupportedLookAndFeelException e){ JOptionPane.showMessageDialog(null, "GUI Load Error: Unsupported");}
catch (ClassNotFoundException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: NimROD Missing");}
catch (InstantiationException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: Instantiation Missing");}
catch (IllegalAccessException e) { JOptionPane.showMessageDialog(null, "GUI Load Error: Illegal Access"); }
//Creates a new LoginScreen via the LoginScreen method
LoginScreen LS = new LoginScreen();
}
public LoginScreen(){
//Adds the JPanel to the JFrame and set the JFrame's properties
//Sets the main JPanel to CardLayout platform and adds other JPanels it
final CardLayout cardL = new CardLayout();
cardScreen = new JPanel();
cardScreen.setLayout(cardL);
cardScreen.add(screen, "1");;
BaseScreen base = new BaseScreen();
cardScreen.add(base, "2");
this.setIconImage(ProgramIcon);
this.setTitle("Login");
this.setSize(WIDTH,HEIGHT);
this.setResizable(false);
this.add(cardScreen);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//Place the components on the JPanel and set their absolute posistions
screen.setLayout(null);
screen.add(username);
screen.add(password);
screen.add(user);
screen.add(pass);
screen.add(login);
screen.add(icon);
Dimension iconSize = icon.getPreferredSize();
Dimension usernameSize = username.getPreferredSize();
Dimension passwordSize = password.getPreferredSize();
Dimension loginSize = login.getPreferredSize();
Dimension userSize = user.getPreferredSize();
Dimension passSize = pass.getPreferredSize();
username.setBounds(252,170,usernameSize.width,usernameSize.height);
password.setBounds(495,170,passwordSize.width,passwordSize.height);
user.setBounds(180,200,userSize.width,userSize.height);
pass.setBounds(420,200,passSize.width,passSize.height);
login.setBounds(375,250,loginSize.width,loginSize.height);
icon.setBounds(250,50,iconSize.width,iconSize.height);
this.setVisible(true);
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
//Checks if both the user and pass text fields are empty
if((user.getText().equals("")) && (pass.getText().equals(""))){
//Displays an error in the form of a label and adds it to the JPanel
errorInfo.setText("Please enter username and password");
screen.add(errorInfo);
errorInfo.setForeground(Color.RED);
Dimension errorInfoSize = errorInfo.getPreferredSize();
errorInfo.setBounds(300,300,errorInfoSize.width,errorInfoSize.height);
}
if((user.getText().equals("admin"))&&(pass.getText().equals("password"))){
cardL.show(cardScreen,"2");
}
}
});
}
}
//Basescreen class
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class BaseScreen extends JPanel{
JPanel screen = this;
JButton logout = new JButton("Logout");
ImageIcon title = new ImageIcon("title.png");
JLabel header = new JLabel(title);
public BaseScreen(){
screen.setLayout(null);
screen.add(logout);
screen.add(header);
Dimension headerSize = header.getPreferredSize();
Dimension logoutSize = logout.getPreferredSize();
logout.setBounds(720,440,logoutSize.width,logoutSize.height);
header.setBounds(0,0,headerSize.width,headerSize.height);
screen.setVisible(true);
}
}
如果你想知道我为什么要把JPanel划分成另一个类,这是因为我想用我的BaseScreen类被许多其他JPanel类继承,并将每个类添加为一张卡,因此在其中一个类中使用JButton对于程序的结构起到至关重要的作用。希望我没有完全错误地处理这个问题,也不需要对程序进行etire重写
-Zalx您可能想看看这个。基本上,它包括使你的卡片布局为静态,这样你可以在actionlistener中为按钮创建一个临时的新“屏幕”对象后访问和更改静态卡片布局。希望我的这篇文章能在这个方向上帮助你:-)非常感谢,现在一切都正常了,你是一个梦想!