问题
I've been in a bit of a pickle here. I've been ripping my hair out over how to accomplish such a task. For my International Bacc I have to fill out certain criteria for my Program dossier and one of them is using inheritance and passing parameters etc. I'm in the stage of making my prototype and wanted to achieve the effect of using multiple JPanels within the same JFrame. I've achieved this rather crudely with setVisivble() and adding both panels to the JFrame. I understand that I can use the CardLayout for this and will probably implement it as soon as possible.
All in all what I'm trying to achieve is that I have a login button the loads the other jpanel, is there a way of doing this in separate classes? Because when I seem to use the myframe.add(new mypanelClass()) it creates an entirely new JFrame! Essentially the miniclass I have in this file I want to separate out into another class. Also how can I make a logout button on the other panel bring me back to the login screen from another class? Thanks in advance for any help.
Here's my code:
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");
}
}
}
}
回答1:
You'll need to stop instantiating a JFrame within that class that extends JFrame, that's 2 JFrames right there. Write JFrame container = this; then use your IDE's inline feature to inline the container field.
If BaseScreen needs access to the JFrame 'this' you can pass that value into BaseScreen's constructor and BaseScreen can store that value as a field. There's no magic 'linking the classes together', you tell one object about another one by passing values around. If what I'm saying is unfamiliar you'll need to visit the constructors section of the Java tutorial - http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
来源:https://stackoverflow.com/questions/12827202/using-multiple-classes-with-the-same-jframe