对同一个JFrame使用多个类 [英] Using multiple classes with the same JFrame

查看:171
本文介绍了对同一个JFrame使用多个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个泡菜。我已经撕开我的头发,如何完成这样的任务。对于我的国际Bacc我必须填写我的程序档案的一些标准,其中之一是使用继承和传递参数等。我正在制作我的原型的阶段,想实现在同一个使用多个JPanels的效果JFrame。我已经通过setVisivble()实现了这一点,并将这两个面板添加到JFrame中。我理解我可以使用CardLayout为此,并可能会尽快实现它。

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.

所有我想要实现的是,我有一个登录按钮加载其他jpanel,有没有办法在单独的类中这样做?因为当我似乎使用myframe.add(新的mypanelClass())它创建一个全新的JFrame!基本上我在这个文件中的小类我想分离成另一个类。另外,如何在其他面板上创建一个注销按钮,让我回到另一个类的登录屏幕?非常感谢您的帮助。

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.

这是我的代码:

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");
        }
    }
}
}


推荐答案

您需要停止在扩展JFrame的类中实例化一个JFrame,这就是2个JFrames。 Write JFrame container = this;

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.

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

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

这篇关于对同一个JFrame使用多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆