按名称搜索 JPanel
Search JPanel by name
我正在学习 How to use CardLayout Java Swing 教程,我已经到了将面板添加到布局的地步:
JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";
//Create the "cards".
JPanel card1 = new JPanel();
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
他们还说:
To add a component to a container that a CardLayout object manages,
specify a string that identifies the component being added. For
example, in this demo, the first panel has the string "Card with
JButtons"
因此,如果该字符串用于标识正在添加的组件,我想知道是否有办法从 AssertJ Swing FrameFixture
或直接从 JFrame
对象,传递字符串。
我看到 add
方法继承自 java.awt.Container
。我期待找到一种方法,允许我做类似 frame.getComponent(BUTTONPANEL)
的事情,但该方法需要一个索引作为参数。我是不是忽略了什么?
我也知道我可以做 card1.setName(BUTTONPANEL)
然后在我的测试中检索它:
window = new FrameFixture(view);
window.panel(BUTTONPANEL);
但是如果没有用,在 add
方法中设置该字符串有什么意义呢?
谢谢。
编辑:
我错过了一个事实,当然字符串 BUTTONPANEL
它用于从卡片布局更改面板,如下所示:
cardLayout.show(getContentPane(), BUTTONPANEL);
很明显,该字符串很有用。
但是我一直在寻找一种方法,通过调用 JFrame
或 FrameFixture
对象上的方法并传递字符串来获取 JPanel
对象。像这样:
JPanel buttonPanelPanel = frame.getPanelFromName(BUTTONPANEL);
这是因为在测试中我无法直接访问面板,只能访问框架。为了在特定面板上做出断言,我希望能够使用 add
中使用的字符串执行面板的获取。但也许这是不可能的。
我在 MainPanel
class 中添加了一个方法,当你传递一个 String
时, return 一个 JPanel
。这个方法我没用过,但是有
我将所有额外的 classes 都放在 classes 中,这样我就可以 post 将代码作为一个块。
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CardPanelExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new CardPanelExample());
}
private final MainPanel mainPanel;
public CardPanelExample() {
this.mainPanel = new MainPanel(this);
}
@Override
public void run() {
JFrame frame = new JFrame("CardPanel Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel.getPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void setNextPanel() {
mainPanel.setNextPanel();
}
public JPanel getPanelFromName(String name) {
return mainPanel.getPanelFromName(name);
}
public class MainPanel {
private final CardLayout cardLayout;
private final JPanel panel;
private JPanel yellowPanel, orangePanel, whitePanel;
private final String[] panelStrings;
private String currentPanelString;
public MainPanel(CardPanelExample view) {
this.cardLayout = new CardLayout();
this.panelStrings = new String[] { "alpha", "beta", "gamma" };
this.currentPanelString = panelStrings[0];
this.panel = createMainPanel(view, cardLayout, panelStrings);
}
private JPanel createMainPanel(CardPanelExample view,
CardLayout cardLayout, String[] panelStrings) {
this.yellowPanel = new ColorPanel(view, Color.YELLOW).getPanel();
this.orangePanel = new ColorPanel(view, Color.ORANGE).getPanel();
this.whitePanel = new ColorPanel(view, Color.WHITE).getPanel();
JPanel panel = new JPanel(cardLayout);
panel.add(yellowPanel, panelStrings[0]);
panel.add(orangePanel, panelStrings[1]);
panel.add(whitePanel, panelStrings[2]);
return panel;
}
public JPanel getPanelFromName(String name) {
if (name.equals(panelStrings[0])) {
return yellowPanel;
} else if (name.equals(panelStrings[1])) {
return orangePanel;
} else if (name.equals(panelStrings[2])) {
return whitePanel;
} else {
return null;
}
}
public void setNextPanel() {
for (int index = 0; index < panelStrings.length; index++) {
if (currentPanelString.equals(panelStrings[index])) {
index = ++index % panelStrings.length;
currentPanelString = panelStrings[index];
cardLayout.show(panel, currentPanelString);
return;
}
}
}
public JPanel getPanel() {
return panel;
}
}
public class ColorPanel {
private final JPanel panel;
public ColorPanel(CardPanelExample view, Color backgroundColor) {
this.panel = createMainPanel(view, backgroundColor);
}
private JPanel createMainPanel(CardPanelExample view, Color backgroundColor) {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(backgroundColor);
panel.setBorder(BorderFactory.createEmptyBorder(25, 150, 25, 150));
JButton button = new JButton("Next Panel");
button.addActionListener(new ButtonListener(view));
panel.add(button);
return panel;
}
public JPanel getPanel() {
return panel;
}
}
public class ButtonListener implements ActionListener {
private final CardPanelExample view;
public ButtonListener(CardPanelExample view) {
this.view = view;
}
@Override
public void actionPerformed(ActionEvent event) {
view.setNextPanel();
}
}
}
我正在学习 How to use CardLayout Java Swing 教程,我已经到了将面板添加到布局的地步:
JPanel cards;
final static String BUTTONPANEL = "Card with JButtons";
//Create the "cards".
JPanel card1 = new JPanel();
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
他们还说:
To add a component to a container that a CardLayout object manages, specify a string that identifies the component being added. For example, in this demo, the first panel has the string "Card with JButtons"
因此,如果该字符串用于标识正在添加的组件,我想知道是否有办法从 AssertJ Swing FrameFixture
或直接从 JFrame
对象,传递字符串。
我看到 add
方法继承自 java.awt.Container
。我期待找到一种方法,允许我做类似 frame.getComponent(BUTTONPANEL)
的事情,但该方法需要一个索引作为参数。我是不是忽略了什么?
我也知道我可以做 card1.setName(BUTTONPANEL)
然后在我的测试中检索它:
window = new FrameFixture(view);
window.panel(BUTTONPANEL);
但是如果没有用,在 add
方法中设置该字符串有什么意义呢?
谢谢。
编辑:
我错过了一个事实,当然字符串 BUTTONPANEL
它用于从卡片布局更改面板,如下所示:
cardLayout.show(getContentPane(), BUTTONPANEL);
很明显,该字符串很有用。
但是我一直在寻找一种方法,通过调用 JFrame
或 FrameFixture
对象上的方法并传递字符串来获取 JPanel
对象。像这样:
JPanel buttonPanelPanel = frame.getPanelFromName(BUTTONPANEL);
这是因为在测试中我无法直接访问面板,只能访问框架。为了在特定面板上做出断言,我希望能够使用 add
中使用的字符串执行面板的获取。但也许这是不可能的。
我在 MainPanel
class 中添加了一个方法,当你传递一个 String
时, return 一个 JPanel
。这个方法我没用过,但是有
我将所有额外的 classes 都放在 classes 中,这样我就可以 post 将代码作为一个块。
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CardPanelExample implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new CardPanelExample());
}
private final MainPanel mainPanel;
public CardPanelExample() {
this.mainPanel = new MainPanel(this);
}
@Override
public void run() {
JFrame frame = new JFrame("CardPanel Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel.getPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void setNextPanel() {
mainPanel.setNextPanel();
}
public JPanel getPanelFromName(String name) {
return mainPanel.getPanelFromName(name);
}
public class MainPanel {
private final CardLayout cardLayout;
private final JPanel panel;
private JPanel yellowPanel, orangePanel, whitePanel;
private final String[] panelStrings;
private String currentPanelString;
public MainPanel(CardPanelExample view) {
this.cardLayout = new CardLayout();
this.panelStrings = new String[] { "alpha", "beta", "gamma" };
this.currentPanelString = panelStrings[0];
this.panel = createMainPanel(view, cardLayout, panelStrings);
}
private JPanel createMainPanel(CardPanelExample view,
CardLayout cardLayout, String[] panelStrings) {
this.yellowPanel = new ColorPanel(view, Color.YELLOW).getPanel();
this.orangePanel = new ColorPanel(view, Color.ORANGE).getPanel();
this.whitePanel = new ColorPanel(view, Color.WHITE).getPanel();
JPanel panel = new JPanel(cardLayout);
panel.add(yellowPanel, panelStrings[0]);
panel.add(orangePanel, panelStrings[1]);
panel.add(whitePanel, panelStrings[2]);
return panel;
}
public JPanel getPanelFromName(String name) {
if (name.equals(panelStrings[0])) {
return yellowPanel;
} else if (name.equals(panelStrings[1])) {
return orangePanel;
} else if (name.equals(panelStrings[2])) {
return whitePanel;
} else {
return null;
}
}
public void setNextPanel() {
for (int index = 0; index < panelStrings.length; index++) {
if (currentPanelString.equals(panelStrings[index])) {
index = ++index % panelStrings.length;
currentPanelString = panelStrings[index];
cardLayout.show(panel, currentPanelString);
return;
}
}
}
public JPanel getPanel() {
return panel;
}
}
public class ColorPanel {
private final JPanel panel;
public ColorPanel(CardPanelExample view, Color backgroundColor) {
this.panel = createMainPanel(view, backgroundColor);
}
private JPanel createMainPanel(CardPanelExample view, Color backgroundColor) {
JPanel panel = new JPanel(new FlowLayout());
panel.setBackground(backgroundColor);
panel.setBorder(BorderFactory.createEmptyBorder(25, 150, 25, 150));
JButton button = new JButton("Next Panel");
button.addActionListener(new ButtonListener(view));
panel.add(button);
return panel;
}
public JPanel getPanel() {
return panel;
}
}
public class ButtonListener implements ActionListener {
private final CardPanelExample view;
public ButtonListener(CardPanelExample view) {
this.view = view;
}
@Override
public void actionPerformed(ActionEvent event) {
view.setNextPanel();
}
}
}