当前位置: 首页 > 面试题库 >

在JFrame中实现CardLayout并根据特定的按钮按下来切换卡

毕衡
2023-03-14
问题内容

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

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

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

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

    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。

所以代替:

main.setContentPane(welcomePanel());

做:

main.setContentPane(cards);

问题编号2:

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

cards.add(home, "Home");

但是,然后尝试像这样显示它:

cl.show(cards, "home");

但是“家”与“家”不同。

而是声明一个常量HOME:

public static final String HOME = "home";

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

举一个简单的例子:

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


 类似资料:
  • 主要内容:创建切换按钮,切换按钮组,ToggleButton行为,样式切换按钮切换按钮具有两种状态:选择或未选择。 我们通常将两个或多个切换按钮组合成一个组,并允许用户只选择一个按钮或不选择。 创建切换按钮 我们可以使用类的三个构造函数创建一个切换按钮。要创建没有任何字幕或图标的切换按钮。 要创建带有文字说明的切换按钮 要创建带有文字说明和图标的切换按钮。 方法可以将文本设置为,以及方法可以将图像安装到。 切换按钮组 切换组不强制选择至少一个按钮。单击所选的切换按钮可取消选

  • 我目前正在开发一个聊天程序的登录表单,希望该程序加载框架并等待用户输入。不可饶恕的是,程序打开了框架,但同时又恢复了主方法。我希望你有一些想法来帮助我。 问候语 JFrame类:

  • 所以我在我的一个程序中使用cardLayout,我试图使它成为当你点击一个按钮时下一个面板加载。我有一个panelHolder类,其中保存了cardlayout,每次按下面板上的按钮时,它都会调用panelHolder类中的一个方法,该方法根据按钮将某个布尔变量设置为true,并调用repaint(其中显示了面板)。由于某种原因,我的按钮坏了,我似乎找不出原因。有人能帮帮我吗?

  • 我想做一个现代的图形用户界面,左边有标签,如图所示: 有什么想法,我如何让面板在不处理写入/设置的数据的情况下进行切换,例如进入文本字段、文本区域、复选框、滚动条等? 我本来想删除旧的一个面板,并添加另一个面板,但当我点击面板之前,我访问的控制数据将被重置,我认为它会闪烁。

  • 我正在尝试用我的自定义gui创建一个切换相机按钮。在我的CustomCam Extendes SherlockFragmentActivity中,我有一个名为onSwitch()的方法,该方法是从xml android调用的:onClick=“onSwitch” 方法如下: 我知道该方法会因为断点和日志语句而触发,但我的屏幕只是变黑,然后返回到相同的std视图。 注意:我意识到我必须检测手机是否有

  • 我创建了一个用于登录,我想在按钮“cont nou”被按下后用jpanel为新帐户打开一个新窗口,但不知道如何用jpanel将初始帧消失和出现。你有什么想法吗?谢谢你!这就是我到现在所做的: 这是,其登录名为: