gpt4 book ai didi

java - 尝试分解 CardLayout

转载 作者:行者123 更新时间:2023-11-30 06:07:17 25 4
gpt4 key购买 nike

我正在尝试将我的代码分解为更小的类。我目前正在使用 CardLayout。第一类可以很好地显示变量,但第二类没有显示任何内容。当我转到第二张卡时,我看到一张空白页。我确信原因是因为他们没有分类。我是否需要在不同的类中设置 CardLayout 并共享这两个类?

头等舱

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

二等

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

}

编辑

抱歉之前的代码。我只是对此进行测试,所以我没有考虑命名约定。做了一些研究,我想我可能已经解决了我的问题。下面是我更新的代码。如果我有问题请告诉我。谢谢!

头等舱

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

二等

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

最佳答案

我不确定您的目标是什么,是什么促使您提出这个问题,但假设您正在尝试重构代码以允许您创建更小且更易于管理的类,那么这不是您应该做的这样做就像在组合更有意义的情况下滥用继承一样。相反,我建议您:

  • 为使用卡片布局的容器中交换的 JPanel 卡创建单独的类
  • 创建一个或多个单独的类来帮助控制卡片交换,如果您愿意的话,可以创建一个卡片交换“引擎”
  • 继承时要小心并有充分的理由,主要是如果您想以某种方式改变类的固有行为,例如 JPanel 的绘制

您的代码和问题的其他问题:

  • 不知道您在哪里创建 test1 或 test2 实例,而是似乎创建了完全不同的类的实例,ttt。这以及您的代码缺少声明的变量,使我们很难重现您的问题以了解您可能做错了什么。据我们所知,您可能没有创建或使用 test2 实例。
  • 您应该避免使用 null 布局并使用 setBounds(...) 进行组件放置,因为这会导致 GUI 非常不灵活,虽然它们在一个平台上看起来不错,但在大多数其他平台上看起来很糟糕平台或屏幕分辨率,并且很难更新和维护。
  • 顺便说一句,您会想要学习和使用 Java naming conventions 。变量名应全部以小写字母开头,而类名应以大写字母开头。学习并遵循这一点将使我们能够更好地理解您的代码,并且将使您更好地理解其他人的代码。

例如,请参阅下面的代码。请注意,它的编写方式是为了将所有内容都可以直接复制并粘贴到 IDE 中,因为只有一个类 CardExample(具有 main 方法的类)是公共(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
}

}

关于java - 尝试分解 CardLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052739/

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