在JFrame中实现CardLayout并根据特定的按钮按下来切换卡 [英] Implementing CardLayout within a JFrame and switching cards based on specific button presses

查看:83
本文介绍了在JFrame中实现CardLayout并根据特定的按钮按下来切换卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在下面发布了我的代码.我有创建可导航GUI的简单任务.在过去的几个小时中,我一直在研究如何实现此目标,这是我编写的代码.

I've posted my code below. I have the simple task of creating a navigable GUI. I've spent the past few hours doing research on how to accomplish this, and this is the code that I've put together.

最初,我想在没有任何布局或任何内容的情况下执行导航.用户单击欢迎面板上的登录"按钮后,我需要显示主页面板.

Originally I wanted to perform the navigation without any layouts or anything. I need the home panel to display after the user clicks on the "login" button on the welcome panel.

它可以很好地显示欢迎卡,但是当我进入validateLogin方法(按下登录按钮时将被激活,并且在成功登录后它应该显示卡内的主面板),它只是停留在欢迎面板上即使我已经验证我的程序通过system.out.Println()

It displays the welcome card just fine, but when I get to the validateLogin method(which is activated when the login button is press, and upon successful login it should show the home panel within cards) it simply stays on the welcome panel even though I have validated that my program reaches the loop to change cards via the system.out.Println()

请帮助.我花了整个星期六试图通过试验和研究解决这一问题,但没有成功.这对我来说是不得已的办法,因此,如果有人可以向我展示我的缺点,那么我会很乐意继续进行修复.然后将该修复程序应用到我的程序所需的许多其他卡上.

Please help. I spent my entire Saturday trying to solve this one problem through trials and research, but with no success. This is a last resort for me so if anyone can show me my flaws then I'll happily be on my way and fix it. Then apply that fix to the many other cards that are required for my program.

    enter code here
    public class mainGUI implements ActionListener{
JFrame main;
JPanel cards = new JPanel(new CardLayout());
CardLayout cl = (CardLayout)(cards.getLayout());

//Items for the welcome panel
JPanel welcome = welcomePanel();
JButton login;
JButton register;
JTextField username;
JTextField password;

//home panel
JPanel home = homePanel();


//WelcomePanel welcome = new WelcomePanel();

ArrayList<Student> students = new ArrayList<Student>();
Student workingStudent;


/**
 * calls load() at start and save() on exit
 * 
 */
public mainGUI(){
    load();

    main = new JFrame();
    main.setSize(900, 600);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setTitle("MyCourses 2k16");
    main.setContentPane(welcomePanel());

    //fill out the cards
    cards.add(welcome, "Welcome");
    cards.add(home, "Home");
            //display welcome card
    cl.show(cards, "welcome");

    main.setVisible(true);

    saveState();
}

private JPanel welcomePanel() {
    JPanel welcome = new JPanel();
    welcome.setLayout(null);
    welcome.setBackground(Color.DARK_GRAY);

    JLabel hi = new JLabel("Welcome to MyCourses 2K16");
    hi.setSize(800, 100);
    hi.setLocation(50,50);
    hi.setFont(new Font("Serif", Font.BOLD, 48));
    hi.setForeground(Color.WHITE);

    JLabel select = new JLabel("Fill in the information, then click login or register to proceed, no special characters allowed");
    select.setSize(700,100);
    select.setLocation(75,100);
    select.setFont(new Font("Serif", Font.PLAIN, 18));
    select.setForeground(Color.WHITE);

    login = new JButton( "login");
    login.setSize(100, 50);
    login.setLocation(50, 200);
    login.addActionListener(this);

    register = new JButton( "register");
    register.setSize(100,50);
    register.setLocation(200, 200);
    register.addActionListener(this);

    JLabel un = new JLabel("username");
    un.setSize(100, 30);
    un.setLocation(50, 270);
    un.setForeground(Color.WHITE);

    username = new JTextField();
    username.setSize(200, 30);
    username.setLocation(50,300);

    JLabel pw = new JLabel("password");
    pw.setSize(100, 30);
    pw.setLocation(50, 350);
    pw.setForeground(Color.WHITE);

    password = new JTextField();
    password.setSize(200, 30);
    password.setLocation(50,380);

    welcome.add(hi);
    welcome.add(select);
    welcome.add(login);
    welcome.add(register);
    welcome.add(un);
    welcome.add(username);
    welcome.add(pw);
    welcome.add(password);

    return welcome;
}

private JPanel homePanel() {
    JPanel home = new JPanel();
    home.setLayout(null);
    home.setBackground(Color.DARK_GRAY);

    JLabel hi = new JLabel("HOME");
    hi.setSize(800, 100);
    hi.setLocation(50,50);
    hi.setFont(new Font("Serif", Font.BOLD, 48));
    hi.setForeground(Color.WHITE);

    return home;

}

public void load(){

}

private void saveState(){
    Iterator<Student> it = students.iterator();
    while(it.hasNext()){
        it.next().saveStudent();
    }
}



public static void main(String[] args) {

    new mainGUI();

}


@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource()==login){
        System.out.println("Logging in...");
        validateLogin(students);
    }
    else if (e.getSource()==register){

    }
}

private void validateLogin(ArrayList<Student> students){
    boolean valid = false;

    for(int i = 0; i < students.size(); i++){

        if(username.getText().equals(students.get(i).getUsername())
                && password.getText().equals(students.get(i).getPassword()))
        {   
            valid = true;
            workingStudent=(students.get(i));
            System.out.println("Successful Login!");
            cl.show(cards, "home");
        }
    }
    if(valid == false){
        System.out.println("Invalid Login, try again");
    }

}

}

推荐答案

您创建了一个使用CardLayout,卡片的JPanel,但是没有添加任何内容,因此它当然不会显示自身,也不会显示其卡片.解决方案:将此JPanel添加到您的GUI.

You create a JPanel that uses CardLayout, cards, but you add it to nothing, so it will of course not display itself, nor its cards. Solution: add this JPanel to your GUI.

所以代替:

main.setContentPane(welcomePanel());

这样做:

main.setContentPane(cards);


问题2:


Issue number 2:

在将字符串用作键的类型时,请使用字符串常量.请注意,您因此将一个JPanel添加到卡JPanel中:

Use String constants when using Strings as a type of key. Note that you add one JPanel to the cards JPanel thusly:

cards.add(home, "Home");

但是尝试像这样显示它:

But then try to display it like so:

cl.show(cards, "home");

但是家和家不一样.

改为声明一个常量,HOME:

Instead declare a constant, HOME:

public static final String HOME = "home";

并使用相同的常量添加并显示JPanel.

and use the same constant to add the JPanel and to display it.

一个简单的例子:

import java.awt.CardLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class MainGui2 extends JPanel {
    private CardLayout cardLayout = new CardLayout();
    private WelcomePanel welcomePanel = new WelcomePanel(this);
    private HomePanel homePanel = new HomePanel();

    public MainGui2() {
        setLayout(cardLayout);
        add(welcomePanel, WelcomePanel.NAME);
        add(homePanel, HomePanel.NAME);
    }

    public void showCard(String name) {
        cardLayout.show(this, name);
    }

    private static void createAndShowGui() {
        MainGui2 mainPanel = new MainGui2();

        JFrame frame = new JFrame("MainGui2");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }

}

class WelcomePanel extends JPanel {
    public static final String NAME = "welcome panel";
    private MainGui2 mainGui2;

    public WelcomePanel(final MainGui2 mainGui2) {
        this.mainGui2 = mainGui2;
        add(new JLabel(NAME));
        add(new JButton(new AbstractAction("Logon") {

            @Override
            public void actionPerformed(ActionEvent e) {
                mainGui2.showCard(HomePanel.NAME);
            }
        }));
    }
}

class HomePanel extends JPanel {
    public static final String NAME = "home panel";

    public HomePanel() {
        add(new JLabel(NAME));
    }
}

这篇关于在JFrame中实现CardLayout并根据特定的按钮按下来切换卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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