Java 使用构造函数参数在 JFrame 中创建 cardLayout
Java create cardLayout in JFrame with constructor parameter
我想用 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 对象上。您用于 show
组件的此 CardLayout 对象:
public MainFrame(String card) {
initComponents();
cardLayout = new CardLayout();
与此处 contentPane 的当前布局不同:
this.contentPane.setLayout(cardLayout);
实际上,后一个 cardLayout 变量似乎在使用时实际上为 null。
一个解决方案,改成这样:
public class 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));
}
}
我想用 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 对象上。您用于 show
组件的此 CardLayout 对象:
public MainFrame(String card) {
initComponents();
cardLayout = new CardLayout();
与此处 contentPane 的当前布局不同:
this.contentPane.setLayout(cardLayout);
实际上,后一个 cardLayout 变量似乎在使用时实际上为 null。
一个解决方案,改成这样:
public class 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));
}
}