gpt4 book ai didi

java - JPanels 不会在cardLayout 中切换

转载 作者:行者123 更新时间:2023-12-01 23:54:07 25 4
gpt4 key购买 nike

我创建了一个包含多个 JPanel 的类,我想使用 cardLayout 在这些 JPanel 之间进行切换。每个Jpanel和cardLayout都有自己的方法。当我单击任何按钮时,它应该显示下一个 Jpanel,但没有。我在这里没有得到什么

import javax.swing.*;
import java.awt.*;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.event.*;

public class guiFrames extends JFrame implements ActionListener{

String name = "login";
JButton loginSubmit, list;
JPanel cardGUI;
CardLayout cards;

public guiFrames(){
}

public Component inputFrame(){

JPanel inputPnl = new JPanel();
inputPnl.setLayout(new GridLayout(3,2));

JLabel loginLbl = new JLabel("Login");
inputPnl.add(loginLbl);
JTextField loginTxt = new JTextField();
inputPnl.add(loginTxt);
JLabel pwLbl = new JLabel("Password");
inputPnl.add(pwLbl);
JTextField pwTxt = new JTextField();
inputPnl.add(pwTxt);

JPanel buttonPnl = new JPanel();
buttonPnl.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

loginSubmit = new JButton("Submit");
buttonPnl.add(loginSubmit);
JButton loginCancel = new JButton("Cancel");
buttonPnl.add(loginCancel);

JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(inputPnl, BorderLayout.CENTER);
container.add(buttonPnl, BorderLayout.SOUTH);

container.setVisible(true);

return container;
}

public Component issueList(){

JPanel listPnl = new JPanel();

JPanel listFlow = new JPanel();
listFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

JButton newIssue = new JButton("New");
listFlow.add(newIssue);
list = new JButton("Issues");
listFlow.add(list);
JButton newUser = new JButton("Add User");
listFlow.add(newUser);


JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(listFlow, BorderLayout.SOUTH);

listPnl.add(container);
// listPnl.pack();
listPnl.setVisible(true);
// listPnl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

return listPnl;
}

public Component issueDetails(){
JList list = new JList();

JPanel detailsFlow = new JPanel();
detailsFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

JButton detailsCancel = new JButton("Cancel");
detailsFlow.add(detailsCancel);
JButton detailsSubmit = new JButton("Submit");
detailsFlow.add(detailsSubmit);
JButton detailsAssign = new JButton("Assign");
detailsFlow.add(detailsAssign);
JButton detailsClose = new JButton("Close");
detailsFlow.add(detailsClose);
JButton detailsValidate = new JButton("Validate");
detailsFlow.add(detailsValidate);
JButton detailsFail = new JButton("Fail");
detailsFlow.add(detailsFail);
detailsFail.setVisible(false);

JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(detailsFlow, BorderLayout.SOUTH);
container.add(list, BorderLayout.CENTER);

container.setVisible(true);

return container;

}

public Component addUser(){

JPanel addUserGrid = new JPanel();
addUserGrid.setLayout(new GridLayout(3,2));


JLabel loginLbl = new JLabel("Login");
addUserGrid.add(loginLbl);
JTextField loginTxt = new JTextField();
addUserGrid.add(loginTxt);
JLabel pwLbl = new JLabel("Password");
addUserGrid.add(pwLbl);
JTextField pwTxt = new JTextField();
addUserGrid.add(pwTxt);

JLabel userLbl = new JLabel("User Type");
addUserGrid.add(userLbl);
JComboBox userCmb = new JComboBox();
userCmb.addItem("User");
userCmb.addItem("Manager");
userCmb.addItem("Developer");
addUserGrid.add(userCmb);

JPanel addUserFlow = new JPanel();
addUserFlow.setLayout(new FlowLayout(FlowLayout.LEFT, 1,5));

JButton addUserCancel = new JButton("Cancel");
addUserFlow.add(addUserCancel);
JButton addUserSubmit = new JButton("Submit");
addUserFlow.add(addUserSubmit);

JPanel container = new JPanel();
container.setLayout(new BorderLayout());
container.add(addUserFlow, BorderLayout.SOUTH);
container.add(addUserGrid, BorderLayout.CENTER);

container.setVisible(true);

return container;
}

public void cardView(){
JFrame cardFrame = new JFrame(name);
cardGUI = new JPanel();
cards = new CardLayout();
cardGUI.setLayout(cards);
cardGUI.add(inputFrame(), "login");
cardGUI.add(issueList(), "issueList");
cardGUI.add(issueDetails(), "issueDetails");
cardGUI.add(addUser(), "addUser");

cardFrame.add(cardGUI, BorderLayout.CENTER);
cardFrame.pack();
cardFrame.setVisible(true);
cardFrame.setDefaultCloseOperation(cardFrame.EXIT_ON_CLOSE);

cards.show(cardGUI, name);
}

public void actionPerformed(ActionEvent e){
if(e.getSource() == loginSubmit){
cards.show(cardGUI, "IssueList");
}else if(e.getSource() == list){
name = "issueDetails";
}
}
}

最佳答案

When I click on any button it should display the next Jpanel but doesn't.

我没有看到任何 button.addActionListener(...) 语句。

阅读 Swing 教程中关于 How to Write an Action Listener 的部分了解更多信息。

编辑:

您尝试显示的卡片似乎不存在。卡名错误。这就是为什么许多人使用静态最终常量,这样您就不会犯输入错误:

cardGUI.add(issueList(), "issueList");
...
cards.show(cardGUI, "IssueList");

关于java - JPanels 不会在cardLayout 中切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15865942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com