使用具有相同JFrame的多个类

时间:2012-10-10 19:47:23

标签: java class jframe jpanel

我在这里吃了一点腌菜。我一直在讨论如何完成这项任务。对于我的国际Bacc,我必须填写我的程序档案的某些标准,其中一个是使用继承和传递参数等。我正处于制作原型的阶段,并希望实现在同一个中使用多个JPanels的效果JFrame中。我用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");
        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

你需要停止在扩展JFrame的那个类中实例化一个JFrame,那就是2个JFrame。写JFrame容器=这个;然后使用IDE的内联功能内联容器字段。

如果BaseScreen需要访问JFrame'this',您可以将该值传递给BaseScreen的构造函数,BaseScreen可以将该值存储为字段。没有魔法'将类连接在一起',你通过传递值告诉一个对象另一个对象。如果我所说的不熟悉,您需要访问Java教程的构造函数部分 - http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html