java - Toggle Java JPanels - 每个面板不同的类

标签 java jpanel

我是 Java GUI 的初学者,我在尝试在其他类的面板之间切换时遇到问题。 我的主类“MyBoxGUI”有 2 个面板,“Login”和“UserMenu” 默认为“登录”面板,按“确定”时窗口移至“用户菜单”面板。 到目前为止,在同一个类(class)中表现良好。 问题是,当我在新类中设置一个新面板并设置该面板时,我无法返回到“UserMenu”面板 - ( Action 监听器工作正常)。 有人可以帮我吗?

MyBoxGUI 类

package main;

import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.CardLayout;



import java.awt.Component;






//import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;

import java.awt.Font;
import java.awt.Color;

import javax.swing.JTextField;
import javax.swing.JPasswordField;





import java.awt.Toolkit;

import GUIs.*;
//import ocsf.client.*;



public class MyBoxGUI extends JPanel
{
private Mtds mtd;
public JFrame frmMybox;
private static JTextField UserName;
private static JTextField port;
private static JTextField IP;
private static JPasswordField passwordField;
public final JPanel Login = new JPanel();
public final JPanel UserMenu = new JPanel();


/**
 * Launch the application.
 */
public static void main(String[] args) 
{
    EventQueue.invokeLater(new Runnable() 
    {
        public void run() 
        {
            try 
            {
                MyBoxGUI window = new MyBoxGUI();
                window.frmMybox.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
/**
 * Create the application.
 */
public MyBoxGUI() 
{
    mtd = new Mtds();

    initialize();
}
/**
 * Initialize the contents of the frame.
 */
    private void initialize() 
    {
    frmMybox = new JFrame();
    frmMybox.setFont(new Font("Tempus Sans ITC", Font.BOLD, 15));
//  frmMybox.setIconImage(Toolkit.getDefaultToolkit().getImage(MyBoxGUI.class.getResource("/images/gift-ideas-gift-card-bridal-gift-baby-shower-gift-gift-box-groom-gift-christmas-gift-party-gift-gift-for-wedding-friend-gift-birthday-gift-baby-gift-good-gift-box-ideas-for-friend-necklace-gift-box.jpg")));
    frmMybox.setBounds(100, 100, 800, 500);
    frmMybox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmMybox.getContentPane().setLayout(new CardLayout(0, 0));
    frmMybox.setTitle("MyBox");
    frmMybox.getContentPane().add(Login);
    Login.setLayout(null);
    JButton btnNewButton = new JButton("OK");
    btnNewButton.addActionListener(new ActionListener() 
    {
        @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent arg0) 
        {               
            Boolean ok = false;
            String UName, Pass, Prt, SIP;
            UName = UserName.getText();
            Pass = passwordField.getText();
            Prt = port.getText();
            SIP = IP.getText();
            if( ( mtd.isUserValid(UName) && mtd.isPasswordValid(Pass) && mtd.isPortValid(Prt) && mtd.isIPValid(SIP)) 
                    && ( !UName.equals("")  && !Pass.equals("") && !Prt.equals("")  && !SIP.equals("") ) )
                ok = true;
            else
            {
                if(!mtd.isUserValid(UName) || UName.equals(""))
                    JOptionPane.showMessageDialog(frmMybox,"User Name Can Contain Only The Following Characters: ( 0-9 , a-z , A-Z , _ )"
                            + " ","Invalid characters",JOptionPane.WARNING_MESSAGE);
                else if(!mtd.isPasswordValid(Pass) || Pass.equals(""))
                    JOptionPane.showMessageDialog(frmMybox,"Invalid characters","Error Message",JOptionPane.WARNING_MESSAGE);
                else if(!mtd.isPortValid(Prt) || Prt.equals(""))
                    JOptionPane.showMessageDialog(frmMybox,"Port Can Contain Only Numbers","Invalid characters",JOptionPane.WARNING_MESSAGE);
                else if(!mtd.isIPValid(SIP) || SIP.equals(""))
                    JOptionPane.showMessageDialog(frmMybox,"IP Address Can Contain Only Numbers seperated By Dots '.' ","Invalid characters",JOptionPane.WARNING_MESSAGE);
            }
            if(ok)
            {

                //LoginController user = new LoginController();
                //chat= new ClientGUI(IP.getText(),port.getText());
                //client = ClientGUI.getClient();
                //msg=new Msg("login",user);
                //client.handleMessageFromClientUI(msg);

                setScreen(Login,UserMenu);
        //      frmMybox.getContentPane().add(UserMenu, "UserMenu");
    //          UserMenu.setVisible(true);
//              Login.setVisible(false);
            }
        }
    });


    btnNewButton.setBounds(344, 313, 82, 48);
    Login.add(btnNewButton);

    JLabel lblWellcomeToMybox = new JLabel("Wellcome to MyBox");
    lblWellcomeToMybox.setForeground(new Color(51, 204, 255));
    lblWellcomeToMybox.setFont(new Font("Arial", Font.PLAIN, 36));
    lblWellcomeToMybox.setBounds(224, 23, 327, 42);
    Login.add(lblWellcomeToMybox);

    JLabel lblUserName = new JLabel("User Name:");
    lblUserName.setBounds(268, 109, 89, 14);
    lblUserName.setFont(new Font("Arial", Font.PLAIN, 14));
    Login.add(lblUserName);

    JLabel lblPassword = new JLabel("Password:");
    lblPassword.setBounds(268, 139, 69, 14);
    lblPassword.setFont(new Font("Arial", Font.PLAIN, 14));
    Login.add(lblPassword);

    JLabel lblServerPort = new JLabel("Server port:");
    lblServerPort.setBounds(268, 197, 82, 14);
    lblServerPort.setFont(new Font("Arial", Font.PLAIN, 14));
    Login.add(lblServerPort);

    JLabel lblServerIp = new JLabel("Server IP:");
    lblServerIp.setBounds(266, 222, 71, 14);
    lblServerIp.setFont(new Font("Arial", Font.PLAIN, 14));
    Login.add(lblServerIp);

    UserName = new JTextField();
    UserName.setBounds(406, 107, 96, 20);
    Login.add(UserName);
    UserName.setColumns(10);

    port = new JTextField();
    port.setBounds(406, 195, 96, 20);
    Login.add(port);
    port.setColumns(10);

    IP = new JTextField();
    IP.setBounds(406, 220, 96, 20);
    Login.add(IP);
    IP.setColumns(10);

    passwordField = new JPasswordField();
    passwordField.setBounds(406, 137, 96, 20);
    Login.add(passwordField);

    frmMybox.getContentPane().add(UserMenu, "User Menu");
    UserMenu.setLayout(null);

    JButton LeaveGOI = new JButton("Back");
    LeaveGOI.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {
            setScreen(UserMenu,Login);
        }
    });
    LeaveGOI.setBounds(10, 66, 153, 23);
    UserMenu.add(LeaveGOI);

    JButton btnJoinAGoi = new JButton("Join a GOI");
    btnJoinAGoi.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0)
        {

        }
    });
    btnJoinAGoi.setBounds(10, 100, 153, 23);
    UserMenu.add(btnJoinAGoi);

    JButton btnSearchForA = new JButton("Search for a GOI");
    btnSearchForA.setBounds(10, 134, 153, 23);
    UserMenu.add(btnSearchForA);

    JButton btnCreateAGoi = new JButton("Create a GOI");
    btnCreateAGoi.setBounds(10, 32, 153, 23);
    UserMenu.add(btnCreateAGoi);

    JButton btnFilesSharedWith = new JButton("Files shared with you");
    btnFilesSharedWith.setBounds(271, 66, 153, 23);
    UserMenu.add(btnFilesSharedWith);

    JButton btnUploadAFile = new JButton("Upload a file");
    btnUploadAFile.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent arg0) 
        {
            JPanel UpLoad = new UploadFile();
            setScreen(UserMenu,UpLoad);
//              frmMybox.getContentPane().add(UpLoad, "UpLoad");
    //          UserMenu.setVisible(false);
    //      UpLoad.setVisible(true);
        }
    });
    btnUploadAFile.setBounds(271, 32, 153, 23);
    UserMenu.add(btnUploadAFile);

    JButton btnSignout = new JButton("Sign-Out");
    btnSignout.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int dialog=JOptionPane.showConfirmDialog(Login, getUserName()+", are you sure you wants to leave?", "Do you want to leave?", JOptionPane.YES_NO_OPTION);
            if(dialog==0){
                //client.quit();

            }
        }
    });
    btnSignout.setBounds(293, 227, 131, 23);
    UserMenu.add(btnSignout);

    JButton btnHelpme = new JButton("Help-Me");
    btnHelpme.addActionListener(new ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
            JOptionPane.showMessageDialog(Login,"Hello "+ getUserName()+", wellcom to Mybox!\n"
                    + "Inside GOIs buttons you can ask to join, leave and even create a GOI.\n"
                    + "Inside Files buttons you can check and edit files people shared with you.\n"
                    + "You can even become an uploader and share files you own.\n"
                    + "We wish you good luck and have fun using MyBox!");
        }
    });
    btnHelpme.setBounds(10, 227, 131, 23);
    UserMenu.add(btnHelpme);


}

    public static  String getUserName(){
        return UserName.getText();
    }
    @SuppressWarnings("deprecation")
    public static  String getPassword(){
        return passwordField.getText();
    }
    public static   String getIP(){
        return IP.getText();
    }
    public static   String getPort(){
        return port.getText();
    }


    public void setScreen(JPanel fls, JPanel tru)
    {
        frmMybox.getContentPane().add(tru);
        tru.setVisible(true);
        fls.setVisible(false);
    }

}

上传文件类

    package GUIs;

import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextField;

import main.MyBoxGUI;

public class UploadFile extends MyBoxGUI {
private JTextField textField;

/**
 * Create the panel.
 */
public static final JPanel UpLoad = new JPanel();

public UploadFile() 
{

    setBounds(100, 100, 800, 500);
    setLayout(null);

    JButton btnNewButton = new JButton("Back");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) 
        {

            setScreen(UpLoad,UserMenu);
            System.out.print("A+++");

        }
    });
    btnNewButton.setBounds(126, 145, 205, 30);
    add(btnNewButton);

    textField = new JTextField();
    textField.setBounds(409, 150, 300, 20);
    add(textField);
    textField.setColumns(10);

    JButton btnDone = new JButton("Done");
    btnDone.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnDone.setBounds(365, 223, 89, 30);
    add(btnDone);

    JButton btnHelpMe = new JButton("Help Me");
    btnHelpMe.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnHelpMe.setBounds(188, 390, 122, 52);
    add(btnHelpMe);

    JButton SignOut = new JButton("Sign-Out");
    SignOut.setBounds(501, 392, 122, 48);
    add(SignOut);
}
}

最佳答案

public void setScreen(JPanel fls, JPanel tru)
{
    frmMybox.getContentPane().add(tru);
    tru.setVisible(true);
    fls.setVisible(false);
    frmMybox.getContentPane().revalidate();
    frmMybox.getContentPane().repaint();
}

如上所述更新您的 setScreen 方法。

关于java - Toggle Java JPanels - 每个面板不同的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30548753/

相关文章:

java - 使用 JNA 和 Ada 的 Interface.C 包将 Ada 动态库与 Java 接口(interface)

java - 错误: IOException - how to get more specific error

java - 我可以连续放置 5 个面板吗

java - JFrame JPanel 刷新/更新/重绘

java - paintComponent 不可见 java

java - 如何将全局 JPanel 添加到 JFrame

java - 将可序列化数组从一个 Activity 传递到另一个 Activity 时发生类转换异常

java - 变量引用不存在的资源: ${project_loc:project_name}

JavaWebStart 以及图像和文件等资源

java - 我的 JFrame 中的另一个重画问题