试图打破 CardLayout
Trying to break up CardLayout
我正在尝试将我的代码分解成更小的 classes。我目前正在使用 CardLayout。第一个 class 显示变量正常,但第二个 class 不显示任何内容。当我转到第二张卡片时,我得到一个空白页。我确定原因是因为它们没有分成 classes。我是否需要在不同的 class 中设置 CardLayout 并让这两个共享?
第一个Class
public test1()
{
mainCL.setLayout(c1);
main1.setPreferredSize(new Dimension(800, 900));
mainCL.add(main1,"1");
main1.setBackground(Color.BLUE);
main1.setLayout(null);
main2.setLayout(null);
btnNewButton.setBounds(254, 835, 117, 29);
main1.add(btnNewButton);
JComboBox comboBox = new JComboBox();
comboBox.setBounds(189, 130, 244, 27);
main1.add(comboBox);
mainCL.add(main1,"1");
mainCL.add(main2,"2");
c1.show(mainCL, "1");
frame.setBounds(100, 100, 450, 300);
frame.getContentPane().add(mainCL);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent arg0) {
c1.show(mainCL,"2");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ttt();
}
});
}
第二Class
public class test2 extends test1{
private final JButton btnNewButton1 = new JButton("drop");
test2(){
JComboBox comboBox1 = new JComboBox();
comboBox1.setBounds(189, 200, 244, 27);
btnNewButton1.setBounds(254, 835, 117, 29);
main2.add(comboBox1);
main2.add(btnNewButton1);
}
编辑
抱歉之前的代码。我只是对此进行测试,所以我没有考虑命名约定。做了一些研究,我想我可能已经解决了我的问题。下面是我更新的代码。如果我有问题,请告诉我。谢谢!
第一个Class
public test1()
{
private JLabel label;
JFrame frame;
JPanel panelCont;
JPanel panelOne;
JButton btnOne;
JComboBox signD = new JComboBox();
CardLayout cards;
test2 panelTwo;
public test1() {
frame = new JFrame("CardLayout in two classes test");
panelCont = new JPanel();
panelOne = new JPanel();
panelOne.setPreferredSize(new Dimension(600, 600));
cards = new CardLayout();
panelTwo = new test2(cards, panelCont);
label = new JLabel("Page 1");
btnOne = new JButton("Switch");
panelCont.setLayout(cards);
panelOne.setBackground(Color.BLUE);
panelCont.add(panelOne, "1");
//GridBag
GridBagConstraints gbc_label = new GridBagConstraints();
GridBagLayout gbl_panelOne = new GridBagLayout();
GridBagConstraints gbc_signD = new GridBagConstraints();
GridBagConstraints gbc_buttonOne = new GridBagConstraints();
//Panel
gbl_panelOne.columnWidths = new int[]{100,400,100};
gbl_panelOne.rowHeights = new int[]{100,400,100};
panelOne.setLayout(gbl_panelOne);
//Label
label.setFont(new Font("Avenir", Font.PLAIN, 35));
gbc_label.gridx = 1;
gbc_label.gridy = 0;
panelOne.add(label, gbc_label);
//Dropdown
signD.setPreferredSize(new Dimension(200, 27));
signD.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
signD.setModel(new DefaultComboBoxModel(new String[] {"Dropdown1", "Dropdown2"}));
signD.setForeground(Color.DARK_GRAY);
gbc_signD.gridx = 1;
gbc_signD.gridy = 1;
panelOne.add(signD, gbc_signD);
//Btn
btnOne.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btnOne.setHorizontalTextPosition(SwingConstants.CENTER);
gbc_buttonOne.gridy = 2;
gbc_buttonOne.insets = new Insets(0, 0, 20, 5);
gbc_buttonOne.anchor = GridBagConstraints.SOUTH;
gbc_buttonOne.gridx = 1;
panelOne.add(btnOne, gbc_buttonOne);
//ActionListener
btnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cards.show(panelCont, "2");
}
});
panelCont.add(panelTwo, "2");
cards.show(panelCont, "1");
//Frame
frame.getContentPane().add(panelCont);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
第二Class
public class test2 extends JPanel{
JButton btnTwo;
CardLayout layout;
JPanel panelCont;
JLabel label = new JLabel("Page 2");
JComboBox signD2 = new JComboBox();
public test2(final CardLayout layout, JPanel panelCont) {
this.layout = layout;
this.panelCont = panelCont;
GridBagLayout gridBagLayout2 = new GridBagLayout();
gridBagLayout2.columnWidths = new int[]{100,400,100};
gridBagLayout2.rowHeights = new int[]{100,400,100};
setLayout(gridBagLayout2);
setBackground(Color.RED);
btnTwo = new JButton("Back");
//GridBag
GridBagConstraints gbc_label = new GridBagConstraints();
GridBagConstraints gbc_signD = new GridBagConstraints();
GridBagConstraints gbc_buttonTwo = new GridBagConstraints();
//Label
label.setFont(new Font("Avenir", Font.PLAIN, 35));
gbc_label.gridx = 1;
gbc_label.gridy = 0;
add(label, gbc_label);
//Dropdown
signD2.setPreferredSize(new Dimension(200, 27));
signD2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
signD2.setModel(new DefaultComboBoxModel(new String[] {"Dropdown3", "Dropdown4"}));
signD2.setForeground(Color.DARK_GRAY);
gbc_signD.gridx = 1;
gbc_signD.gridy = 1;
add(signD2, gbc_signD);
//btn
btnTwo.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btnTwo.setHorizontalTextPosition(SwingConstants.CENTER);
gbc_buttonTwo.gridy = 2;
gbc_buttonTwo.insets = new Insets(0, 0, 20, 5);
gbc_buttonTwo.anchor = GridBagConstraints.SOUTH;
gbc_buttonTwo.gridx = 1;
add(btnTwo, gbc_buttonTwo);
btnTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layout.show(panelCont, "1");
}
});
}
}
主要
public class main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new test1();
}
});
}}
我不确定你的目标是什么,是什么驱使你问这个问题,但假设你正在尝试重构代码以允许你创建更小且更易于管理的 classes,这不是你应该做的,因为你在组合更有意义的情况下滥用继承。相反,我建议您:
- 为使用卡片布局的容器中交换的 JPanel 卡片创建单独的 classes
- 创建一个单独的 class 或 classes 来帮助控制卡片的交换,如果你愿意,可以进行卡片交换 "engine"
- 谨慎继承并有充分的理由,主要是如果您想以某种方式改变 class 的固有行为,例如 JPanel 的绘画
您的代码和问题的其他问题:
- 没有你在哪里创建 test1 或 test2 实例,而是似乎在创建一个完全不同的 class、
ttt
的实例。这一点以及您的代码缺少声明的变量,使我们很难重现您的问题以查看您可能做错了什么。据我们所知,您可能不会创建或使用 test2 实例。
- 您应该避免使用空布局和使用
setBounds(...)
来放置组件,因为这会导致 GUI 非常不灵活,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,而且很难更新和维护。
- 顺便说一句,您将想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。
例如请看下面的代码。请注意,它已被编写为可以将其全部复制并直接粘贴到 IDE 中,因为只有一个 class,具有主要方法的 CardExample 是 public 并且具有所有进口:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Paint;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class CardExample {
private static void createAndShowGui() {
CardExampleMain cardUser = new CardExampleMain();
JFrame frame = new JFrame("Card Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(cardUser.getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class CardExampleMain {
private JPanel mainPanel = new JPanel();
private CardUser cardUser = new CardUser();
private Card1 card1 = new Card1(cardUser);
private Card2 card2 = new Card2(cardUser);
private Card3 card3 = new Card3(cardUser);
private JComboBox<String> cardKeyBox;
public CardExampleMain() {
cardUser.addCard(card1, card1.getName());
cardUser.addCard(card2, card2.getName());
cardUser.addCard(card3, card3.getName());
int itemsSize = cardUser.getKeys().size();
String[] items = cardUser.getKeys().toArray(new String[itemsSize]);
cardKeyBox = new JComboBox<>(items);
cardKeyBox.setSelectedIndex(-1);
cardKeyBox.addActionListener(e -> cardUser.show(cardKeyBox.getSelectedItem().toString()));
JPanel topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createEtchedBorder());
topPanel.add(new JLabel("Select Card:"));
topPanel.add(cardKeyBox);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(cardUser.getMainPanel());
mainPanel.add(topPanel, BorderLayout.PAGE_START);
}
public JPanel getMainPanel() {
return mainPanel;
}
}
class CardUser {
private CardLayout cardLayout = new CardLayout();
private JPanel mainPanel = new JPanel(cardLayout);
private List<String> keys = new ArrayList<>();
public JPanel getMainPanel() {
return mainPanel;
}
public void addCard(Component component, String key) {
mainPanel.add(component, key);
keys.add(key);
}
public void show(String key) {
cardLayout.show(mainPanel, key);
}
public void next() {
cardLayout.next(mainPanel);
}
public void previous() {
cardLayout.previous(mainPanel);
}
public List<String> getKeys() {
return keys;
}
}
@SuppressWarnings("serial")
abstract class CardPanel extends JPanel {
private static final int EB_GAP = 5;
protected CardUser cardUser;
protected Action nextAction;
protected Action previousAction;
protected Action showAction;
public CardPanel(CardUser cardUser) {
this.cardUser = cardUser;
nextAction = new CardAction("Next", KeyEvent.VK_N, e -> cardUser.next());
previousAction = new CardAction("Previous", KeyEvent.VK_P, e -> cardUser.previous());
setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(false);
buttonPanel.add(new JButton(previousAction));
buttonPanel.add(new JButton(nextAction));
add(buttonPanel, BorderLayout.PAGE_END);
}
}
@SuppressWarnings("serial")
class CardAction extends AbstractAction {
private ActionListener listener;
public CardAction(String name, int mnemonic, ActionListener listener) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
this.listener = listener;
}
@Override
public void actionPerformed(ActionEvent e) {
listener.actionPerformed(e);
}
}
@SuppressWarnings("serial")
class Card1 extends CardPanel {
public static final String NAME = "Card 1";
private static final Color COLOR_1 = Color.PINK;
private static final Color COLOR_2 = new Color(150, 150, 255);
private static final float WDTH = 20f;
private static final int LBL_GAP = 40;
public Card1(CardUser cardUser) {
super(cardUser);
setName(NAME);
JLabel label = new JLabel(NAME);
label.setFont(label.getFont().deriveFont(Font.BOLD, 128));
label.setBorder(BorderFactory.createEmptyBorder(LBL_GAP, LBL_GAP, LBL_GAP, LBL_GAP));
add(label);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Paint paint = new GradientPaint(0f, 0f, COLOR_1, WDTH, WDTH, COLOR_2, true);
g2.setPaint(paint);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
@SuppressWarnings("serial")
class Card2 extends CardPanel {
public static final String NAME = "Card 2";
private static final Color COLOR_1 = Color.BLACK;
private static final Color COLOR_2 = Color.BLUE;
private static final Color LABEL_FG = Color.LIGHT_GRAY;
private static final String[] DATA = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private JLabel label = new JLabel("Card 2", SwingConstants.CENTER);
private JComboBox<String> comboBox = new JComboBox<>(DATA);
public Card2(CardUser cardUser) {
super(cardUser);
setName(NAME);
label.setForeground(LABEL_FG);
JPanel centralPanel = new JPanel(new GridBagLayout());
centralPanel.setOpaque(false);
centralPanel.add(comboBox);
add(label, BorderLayout.PAGE_START);
add(centralPanel);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Paint paint = new GradientPaint(0f, 0f, COLOR_1, getWidth(), getHeight(), COLOR_2, false);
g2.setPaint(paint);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
@SuppressWarnings("serial")
class Card3 extends CardPanel {
public static final String NAME = "Card 3";
public Card3(CardUser cardUser) {
super(cardUser);
setName(NAME);
add(new JLabel(NAME, SwingConstants.CENTER));
// TODO put more components in here
}
}
我正在尝试将我的代码分解成更小的 classes。我目前正在使用 CardLayout。第一个 class 显示变量正常,但第二个 class 不显示任何内容。当我转到第二张卡片时,我得到一个空白页。我确定原因是因为它们没有分成 classes。我是否需要在不同的 class 中设置 CardLayout 并让这两个共享?
第一个Class
public test1()
{
mainCL.setLayout(c1);
main1.setPreferredSize(new Dimension(800, 900));
mainCL.add(main1,"1");
main1.setBackground(Color.BLUE);
main1.setLayout(null);
main2.setLayout(null);
btnNewButton.setBounds(254, 835, 117, 29);
main1.add(btnNewButton);
JComboBox comboBox = new JComboBox();
comboBox.setBounds(189, 130, 244, 27);
main1.add(comboBox);
mainCL.add(main1,"1");
mainCL.add(main2,"2");
c1.show(mainCL, "1");
frame.setBounds(100, 100, 450, 300);
frame.getContentPane().add(mainCL);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent arg0) {
c1.show(mainCL,"2");
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new ttt();
}
});
}
第二Class
public class test2 extends test1{
private final JButton btnNewButton1 = new JButton("drop");
test2(){
JComboBox comboBox1 = new JComboBox();
comboBox1.setBounds(189, 200, 244, 27);
btnNewButton1.setBounds(254, 835, 117, 29);
main2.add(comboBox1);
main2.add(btnNewButton1);
}
编辑
抱歉之前的代码。我只是对此进行测试,所以我没有考虑命名约定。做了一些研究,我想我可能已经解决了我的问题。下面是我更新的代码。如果我有问题,请告诉我。谢谢!
第一个Class
public test1()
{
private JLabel label;
JFrame frame;
JPanel panelCont;
JPanel panelOne;
JButton btnOne;
JComboBox signD = new JComboBox();
CardLayout cards;
test2 panelTwo;
public test1() {
frame = new JFrame("CardLayout in two classes test");
panelCont = new JPanel();
panelOne = new JPanel();
panelOne.setPreferredSize(new Dimension(600, 600));
cards = new CardLayout();
panelTwo = new test2(cards, panelCont);
label = new JLabel("Page 1");
btnOne = new JButton("Switch");
panelCont.setLayout(cards);
panelOne.setBackground(Color.BLUE);
panelCont.add(panelOne, "1");
//GridBag
GridBagConstraints gbc_label = new GridBagConstraints();
GridBagLayout gbl_panelOne = new GridBagLayout();
GridBagConstraints gbc_signD = new GridBagConstraints();
GridBagConstraints gbc_buttonOne = new GridBagConstraints();
//Panel
gbl_panelOne.columnWidths = new int[]{100,400,100};
gbl_panelOne.rowHeights = new int[]{100,400,100};
panelOne.setLayout(gbl_panelOne);
//Label
label.setFont(new Font("Avenir", Font.PLAIN, 35));
gbc_label.gridx = 1;
gbc_label.gridy = 0;
panelOne.add(label, gbc_label);
//Dropdown
signD.setPreferredSize(new Dimension(200, 27));
signD.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
signD.setModel(new DefaultComboBoxModel(new String[] {"Dropdown1", "Dropdown2"}));
signD.setForeground(Color.DARK_GRAY);
gbc_signD.gridx = 1;
gbc_signD.gridy = 1;
panelOne.add(signD, gbc_signD);
//Btn
btnOne.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btnOne.setHorizontalTextPosition(SwingConstants.CENTER);
gbc_buttonOne.gridy = 2;
gbc_buttonOne.insets = new Insets(0, 0, 20, 5);
gbc_buttonOne.anchor = GridBagConstraints.SOUTH;
gbc_buttonOne.gridx = 1;
panelOne.add(btnOne, gbc_buttonOne);
//ActionListener
btnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cards.show(panelCont, "2");
}
});
panelCont.add(panelTwo, "2");
cards.show(panelCont, "1");
//Frame
frame.getContentPane().add(panelCont);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
第二Class
public class test2 extends JPanel{
JButton btnTwo;
CardLayout layout;
JPanel panelCont;
JLabel label = new JLabel("Page 2");
JComboBox signD2 = new JComboBox();
public test2(final CardLayout layout, JPanel panelCont) {
this.layout = layout;
this.panelCont = panelCont;
GridBagLayout gridBagLayout2 = new GridBagLayout();
gridBagLayout2.columnWidths = new int[]{100,400,100};
gridBagLayout2.rowHeights = new int[]{100,400,100};
setLayout(gridBagLayout2);
setBackground(Color.RED);
btnTwo = new JButton("Back");
//GridBag
GridBagConstraints gbc_label = new GridBagConstraints();
GridBagConstraints gbc_signD = new GridBagConstraints();
GridBagConstraints gbc_buttonTwo = new GridBagConstraints();
//Label
label.setFont(new Font("Avenir", Font.PLAIN, 35));
gbc_label.gridx = 1;
gbc_label.gridy = 0;
add(label, gbc_label);
//Dropdown
signD2.setPreferredSize(new Dimension(200, 27));
signD2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
signD2.setModel(new DefaultComboBoxModel(new String[] {"Dropdown3", "Dropdown4"}));
signD2.setForeground(Color.DARK_GRAY);
gbc_signD.gridx = 1;
gbc_signD.gridy = 1;
add(signD2, gbc_signD);
//btn
btnTwo.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
btnTwo.setHorizontalTextPosition(SwingConstants.CENTER);
gbc_buttonTwo.gridy = 2;
gbc_buttonTwo.insets = new Insets(0, 0, 20, 5);
gbc_buttonTwo.anchor = GridBagConstraints.SOUTH;
gbc_buttonTwo.gridx = 1;
add(btnTwo, gbc_buttonTwo);
btnTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
layout.show(panelCont, "1");
}
});
}
}
主要
public class main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new test1();
}
});
}}
我不确定你的目标是什么,是什么驱使你问这个问题,但假设你正在尝试重构代码以允许你创建更小且更易于管理的 classes,这不是你应该做的,因为你在组合更有意义的情况下滥用继承。相反,我建议您:
- 为使用卡片布局的容器中交换的 JPanel 卡片创建单独的 classes
- 创建一个单独的 class 或 classes 来帮助控制卡片的交换,如果你愿意,可以进行卡片交换 "engine"
- 谨慎继承并有充分的理由,主要是如果您想以某种方式改变 class 的固有行为,例如 JPanel 的绘画
您的代码和问题的其他问题:
- 没有你在哪里创建 test1 或 test2 实例,而是似乎在创建一个完全不同的 class、
ttt
的实例。这一点以及您的代码缺少声明的变量,使我们很难重现您的问题以查看您可能做错了什么。据我们所知,您可能不会创建或使用 test2 实例。 - 您应该避免使用空布局和使用
setBounds(...)
来放置组件,因为这会导致 GUI 非常不灵活,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,而且很难更新和维护。 - 顺便说一句,您将想要学习和使用 Java naming conventions。变量名称应全部以小写字母开头,而 class 名称应以大写字母开头。了解并遵循这一点将使我们能够更好地理解您的代码,并使您能够更好地理解其他人的代码。
例如请看下面的代码。请注意,它已被编写为可以将其全部复制并直接粘贴到 IDE 中,因为只有一个 class,具有主要方法的 CardExample 是 public 并且具有所有进口:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Paint;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class CardExample {
private static void createAndShowGui() {
CardExampleMain cardUser = new CardExampleMain();
JFrame frame = new JFrame("Card Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(cardUser.getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class CardExampleMain {
private JPanel mainPanel = new JPanel();
private CardUser cardUser = new CardUser();
private Card1 card1 = new Card1(cardUser);
private Card2 card2 = new Card2(cardUser);
private Card3 card3 = new Card3(cardUser);
private JComboBox<String> cardKeyBox;
public CardExampleMain() {
cardUser.addCard(card1, card1.getName());
cardUser.addCard(card2, card2.getName());
cardUser.addCard(card3, card3.getName());
int itemsSize = cardUser.getKeys().size();
String[] items = cardUser.getKeys().toArray(new String[itemsSize]);
cardKeyBox = new JComboBox<>(items);
cardKeyBox.setSelectedIndex(-1);
cardKeyBox.addActionListener(e -> cardUser.show(cardKeyBox.getSelectedItem().toString()));
JPanel topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createEtchedBorder());
topPanel.add(new JLabel("Select Card:"));
topPanel.add(cardKeyBox);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(cardUser.getMainPanel());
mainPanel.add(topPanel, BorderLayout.PAGE_START);
}
public JPanel getMainPanel() {
return mainPanel;
}
}
class CardUser {
private CardLayout cardLayout = new CardLayout();
private JPanel mainPanel = new JPanel(cardLayout);
private List<String> keys = new ArrayList<>();
public JPanel getMainPanel() {
return mainPanel;
}
public void addCard(Component component, String key) {
mainPanel.add(component, key);
keys.add(key);
}
public void show(String key) {
cardLayout.show(mainPanel, key);
}
public void next() {
cardLayout.next(mainPanel);
}
public void previous() {
cardLayout.previous(mainPanel);
}
public List<String> getKeys() {
return keys;
}
}
@SuppressWarnings("serial")
abstract class CardPanel extends JPanel {
private static final int EB_GAP = 5;
protected CardUser cardUser;
protected Action nextAction;
protected Action previousAction;
protected Action showAction;
public CardPanel(CardUser cardUser) {
this.cardUser = cardUser;
nextAction = new CardAction("Next", KeyEvent.VK_N, e -> cardUser.next());
previousAction = new CardAction("Previous", KeyEvent.VK_P, e -> cardUser.previous());
setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setOpaque(false);
buttonPanel.add(new JButton(previousAction));
buttonPanel.add(new JButton(nextAction));
add(buttonPanel, BorderLayout.PAGE_END);
}
}
@SuppressWarnings("serial")
class CardAction extends AbstractAction {
private ActionListener listener;
public CardAction(String name, int mnemonic, ActionListener listener) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
this.listener = listener;
}
@Override
public void actionPerformed(ActionEvent e) {
listener.actionPerformed(e);
}
}
@SuppressWarnings("serial")
class Card1 extends CardPanel {
public static final String NAME = "Card 1";
private static final Color COLOR_1 = Color.PINK;
private static final Color COLOR_2 = new Color(150, 150, 255);
private static final float WDTH = 20f;
private static final int LBL_GAP = 40;
public Card1(CardUser cardUser) {
super(cardUser);
setName(NAME);
JLabel label = new JLabel(NAME);
label.setFont(label.getFont().deriveFont(Font.BOLD, 128));
label.setBorder(BorderFactory.createEmptyBorder(LBL_GAP, LBL_GAP, LBL_GAP, LBL_GAP));
add(label);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Paint paint = new GradientPaint(0f, 0f, COLOR_1, WDTH, WDTH, COLOR_2, true);
g2.setPaint(paint);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
@SuppressWarnings("serial")
class Card2 extends CardPanel {
public static final String NAME = "Card 2";
private static final Color COLOR_1 = Color.BLACK;
private static final Color COLOR_2 = Color.BLUE;
private static final Color LABEL_FG = Color.LIGHT_GRAY;
private static final String[] DATA = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
private JLabel label = new JLabel("Card 2", SwingConstants.CENTER);
private JComboBox<String> comboBox = new JComboBox<>(DATA);
public Card2(CardUser cardUser) {
super(cardUser);
setName(NAME);
label.setForeground(LABEL_FG);
JPanel centralPanel = new JPanel(new GridBagLayout());
centralPanel.setOpaque(false);
centralPanel.add(comboBox);
add(label, BorderLayout.PAGE_START);
add(centralPanel);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Paint paint = new GradientPaint(0f, 0f, COLOR_1, getWidth(), getHeight(), COLOR_2, false);
g2.setPaint(paint);
g2.fillRect(0, 0, getWidth(), getHeight());
}
}
@SuppressWarnings("serial")
class Card3 extends CardPanel {
public static final String NAME = "Card 3";
public Card3(CardUser cardUser) {
super(cardUser);
setName(NAME);
add(new JLabel(NAME, SwingConstants.CENTER));
// TODO put more components in here
}
}