gpt4 book ai didi

Java在JFrame中使用构造函数参数创建cardLayout

转载 作者:行者123 更新时间:2023-12-01 17:58:38 25 4
gpt4 key购买 nike

我想使用带参数的 CardLayout 创建一个 JPanel 。我有以下代码:

public class MainFrame extends JFrame{

private JPanel contentPane;
private CardLayout cardLayout;
private String card;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame("");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public MainFrame(String card) {
initComponents();
cardLayout = new CardLayout();

if(card.equals("ADMIN")){
contentPane.revalidate();
cardLayout.show(this.contentPane, "ADMIN");
}else if(card.equals("USER")){
contentPane.revalidate();
cardLayout.show(this.contentPane, "USER");
}
}

private void initComponents() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1280, 800);
this.contentPane = new JPanel();
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
this.contentPane.setLayout(cardLayout);
this.contentPane.add(new PanelOne(), "ADMIN");
this.contentPane.add(new PanelTwo(), "USER");
setContentPane(this.contentPane);
}

但是如果我运行这段代码,我会得到一个异常:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
wrong parent for CardLayout

我必须做什么才能解决这个问题?

我已经尝试过更改main方法中的参数,但没有任何解决方案。

最佳答案

您的问题出在您正在使用的 CardLayout 对象中。此 CardLayout 对象,您用来显示组件:

public MainFrame(String card) {
initComponents();
cardLayout = new CardLayout();

与此处 contentPane 当前的布局不同:

this.contentPane.setLayout(cardLayout);

事实上,后一个 cardLayout 变量在使用时实际上为 null。

解决办法,改成这样:

公共(public)类 MainFrame 扩展 JFrame{

private JPanel contentPane;
private CardLayout cardLayout = new CardLayout();

并且不要在其他地方调用 new CardLayout();

<小时/>

另一个问题是调用构造函数时传入空字符串。

例如,

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MainFrame extends JFrame {

private JPanel contentPane;
private CardLayout cardLayout = new CardLayout();
private String card;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame(PanelOne.NAME);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public MainFrame(String card) {
initComponents();
// cardLayout = new CardLayout();
cardLayout.show(getContentPane(), card);
}

private void initComponents() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width / 2 - this.getSize().width / 2,
dim.height / 2 - this.getSize().height / 2);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1280, 800);
this.contentPane = new JPanel();
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
this.contentPane.setLayout(cardLayout);
this.contentPane.add(new PanelOne(), PanelOne.NAME);
this.contentPane.add(new PanelTwo(), PanelTwo.NAME);
setContentPane(this.contentPane);
}
}

public class PanelOne extends JPanel {
public static final String NAME = "Administrator";

public PanelOne() {
setBorder(BorderFactory.createTitledBorder(NAME));
}
}

public class PanelTwo extends JPanel {
public static final String NAME = "User";

public PanelTwo() {
setBorder(BorderFactory.createTitledBorder(NAME));
}

}

关于Java在JFrame中使用构造函数参数创建cardLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42657348/

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