我在这里有点吃不消。我一直在为如何完成这样的任务而烦恼。对于我的 International Bacc,我必须为我的 Program dossier 填写某些标准,其中之一是使用继承和传递参数等。我正处于制作原型的阶段,并希望实现在同一个内使用多个 JPanel 的效果框架。我用 setVisivble() 粗略地实现了这一点,并将两个面板都添加到 JFrame 中。我知道我可以为此使用 CardLayout 并且可能会尽快实现它。
总而言之,我想要实现的是我有一个登录按钮来加载另一个 jpanel,有没有办法在单独的类中做到这一点?因为当我似乎使用 myframe.add(new mypanelClass()) 时,它会创建一个全新的 JFrame!本质上,我想将这个文件中的迷你类分离到另一个类中。另外,如何在另一个面板上制作一个注销按钮,让我从另一个班级回到登录屏幕?提前感谢您的帮助。
这是我的代码:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
class Menu extends JFrame
{
JFrame container = new JFrame();
JPanel screen = new JPanel();
JPanel screenBase = 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;
JPanel screen2 = new JPanel();
JButton logout = new JButton("Logout");
ImageIcon title = new ImageIcon("title.png");
JLabel header = new JLabel(title);
public static void main(String[] args)
{
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"); }
Menu admin = new Menu();
}
public Menu()
{
container.setIconImage(ProgramIcon);
container.setTitle("Login");
container.setSize(WIDTH,HEIGHT);
container.setResizable(false);
container.setVisible(true);
container.add(screen);
container.setDefaultCloseOperation(EXIT_ON_CLOSE);
screen.add(username);
screen.add(password);
screen.add(user);
screen.add(pass);
screen.add(login);
screen.add(icon);
screen.setLayout(null);
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);
ButtonHandler handle = new ButtonHandler();
login.addActionListener(handle);
new BaseScreen();
}
public class BaseScreen
{
public BaseScreen()
{
container.add(screen2);
screen2.setLayout(null);
screen2.add(logout);
screen2.add(header);
screen2.setVisible(false);
Dimension headerSize = header.getPreferredSize();
Dimension logoutSize = logout.getPreferredSize();
logout.setBounds(720,440,logoutSize.width,logoutSize.height);
header.setBounds(0,0,headerSize.width,headerSize.height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
ButtonHandler handle = new ButtonHandler();
logout.addActionListener(handle);
}
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == login)
{
if((user.getText().equals("")) && (pass.getText().equals("")))
{
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")))
{
screen.setVisible(false);
screen2.setVisible(true);
container.setTitle("Menu");
user.setText("");
pass.setText("");
}
}
if (event.getSource() == logout)
{
screen2.setVisible(false);
screen.setVisible(true);
container.setTitle("Login");
}
}
}
}