gpt4 book ai didi

java - CardLayout 从另一个类更改面板

转载 作者:行者123 更新时间:2023-11-30 03:33:38 24 4
gpt4 key购买 nike

我正在自学 java GUI,在浏览了 stackoverflow 上的一些示例后,我陷入了这个问题(如下)。我想知道的是如何从另一个类转到上一个 JPanel?

如果我在 Window3 中有一个带有 Action 监听器的 JButton,我如何告诉它转到上一个类?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* Here we are first declaring our class that will act as the
* base for other panels or in other terms the base for CardLayout.
*/

public class CardLayoutTest
{
private static final String CARD_JBUTTON = "Card JButton";
private static final String CARD_JTEXTFIELD = "Card JTextField";
private static final String CARD_JRADIOBUTTON = "Card JRadioButton";

private static void createAndShowGUI()
{
JFrame frame = new JFrame("Card Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

// This JPanel is the base for CardLayout for other JPanels.
final JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout(20, 20));

/* Here we be making objects of the Window Series classes
* so that, each one of them can be added to the JPanel
* having CardLayout.
*/
Window1 win1 = new Window1();
contentPane.add(win1, CARD_JBUTTON);
Window2 win2 = new Window2();
contentPane.add(win2, CARD_JTEXTFIELD);
Window3 win3 = new Window3();
contentPane.add(win3, CARD_JRADIOBUTTON);

/* We need two JButtons to go to the next Card
* or come back to the previous Card, as and when
* desired by the User.
*/
JPanel buttonPanel = new JPanel();
final JButton previousButton = new JButton("PREVIOUS");
previousButton.setBackground(Color.BLACK);
previousButton.setForeground(Color.WHITE);
final JButton nextButton = new JButton("NEXT");
nextButton.setBackground(Color.RED);
nextButton.setForeground(Color.WHITE);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);

/* Adding the ActionListeners to the JButton,
* so that the user can see the next Card or
* come back to the previous Card, as desired.
*/
previousButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.previous(contentPane);
}
});
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});

// Adding the contentPane (JPanel) and buttonPanel to JFrame.
frame.add(contentPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.PAGE_END);

frame.pack();
frame.setVisible(true);
}

public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}

class Window1 extends JPanel
{
/*
* Here this is our first Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JButtons.
*/
private ActionListener action;

public Window1()
{
init();
}

private void init()
{
final JButton clickButton = new JButton("CLICK ME");
final JButton dontClickButton = new JButton("DON\'T CLICK ME");

action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == clickButton)
{
JOptionPane.showMessageDialog(null, "Hello there dude!"
, "Right Button", JOptionPane.INFORMATION_MESSAGE);
}
else if (ae.getSource() == dontClickButton)
{
JOptionPane.showMessageDialog(null, "I told you not to click me!"
, "Wrong Button", JOptionPane.PLAIN_MESSAGE);
}
}
};

clickButton.addActionListener(action);
dontClickButton.addActionListener(action);

add(clickButton);
add(dontClickButton);
}
}

class Window2 extends JPanel implements ActionListener
{
/*
* Here this is our second Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of a JLabel and a JTextField
* with GridLayout.
*/

private JTextField textField;

public Window2()
{
init();
}

private void init()
{
setLayout(new GridLayout(1, 2));
JLabel userLabel = new JLabel("Your Name : ");
textField = new JTextField();
textField.addActionListener(this);

add(userLabel);
add(textField);
}

public void actionPerformed(ActionEvent e)
{
if (textField.getDocument().getLength() > 0)
JOptionPane.showMessageDialog(null, "Your Name is : " + textField.getText()
, "User\'s Name : ", JOptionPane.QUESTION_MESSAGE);
}
}

class Window3 extends JPanel
{
/*
* Here this is our third Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JLabels and two JCheckBox
* with GridLayout.
*/
private ActionListener state;

public Window3()
{
init();
}

public void init()
{
setLayout(new GridLayout(2, 2));
JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
final JCheckBox maleBox = new JCheckBox();
JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
final JCheckBox femaleBox = new JCheckBox();

state = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (maleBox == (JCheckBox) ae.getSource())
{
femaleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Male"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
else if (femaleBox == (JCheckBox) ae.getSource())
{
maleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Female"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
}
};

maleBox.addActionListener(state);
femaleBox.addActionListener(state);
add(maleLabel);
add(maleBox);
add(femaleLabel);
add(femaleBox);
}
}

最佳答案

将 CardLayout 和 CardLayout-using JPanel 设置为 CardLayout-using 类的私有(private)字段,例如名为 cardLayout 和 cardShowingPanel,并为该类提供两个公共(public)方法:

public void nextCard() {
// in here call next() on the CardLayout
cardLayout.next(cardShowingPanel);
}

public void previousCard() {
// in here call previous() on the CardLayout
cardLayout.previous(cardShowingPanel);
}

为了获得最大的灵 active ,还可以考虑创建一个 showCard(...) 方法来镜像 CardLayout 的 show(...) 方法:

public void showCard(String key) {
cardLayout.show(cardShowingPanel, key);
}

完成此操作后,我使用公共(public)常量作为键来将组件添加到卡片显示 JPanel,以便外部类在想要显示特定卡片时可以重用这些常量。

然后任何持有此类引用的类都可以调用公共(public)方法并交换卡片。

<小时/>

例如,使用上面的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CardLayoutTestPanel extends JPanel {
public static final String CARD_JBUTTON = "Card JButton";
public static final String CARD_JTEXTFIELD = "Card JTextField";
public static final String CARD_JRADIOBUTTON = "Card JRadioButton";

private static void createAndShowGui() {
final CardLayoutTestPanel cardLayoutTestPanel = new CardLayoutTestPanel();

JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 5));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
final JButton previousButton = new JButton("PREVIOUS");
previousButton.setBackground(Color.BLACK);
previousButton.setForeground(Color.WHITE);
final JButton nextButton = new JButton("NEXT");
nextButton.setBackground(Color.RED);
nextButton.setForeground(Color.WHITE);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);

previousButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cardLayoutTestPanel.previousCard();
}
});
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cardLayoutTestPanel.nextCard();
}
});

JFrame frame = new JFrame("CardLayoutTest");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(cardLayoutTestPanel);
frame.getContentPane().add(buttonPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}

private CardLayout cardLayout = new CardLayout(20, 20);
private JPanel cardShowingPanel = new JPanel(cardLayout);

public CardLayoutTestPanel() {
/*
* Here we be making objects of the Window Series classes so that, each
* one of them can be added to the JPanel having CardLayout.
*/
Window1 win1 = new Window1();
cardShowingPanel.add(win1, CARD_JBUTTON);
Window2 win2 = new Window2();
cardShowingPanel.add(win2, CARD_JTEXTFIELD);
Window3 win3 = new Window3();
cardShowingPanel.add(win3, CARD_JRADIOBUTTON);

setLayout(new BorderLayout());

// Adding the contentPane (JPanel) and buttonPanel to JFrame.
add(cardShowingPanel, BorderLayout.CENTER);
}

public void nextCard() {
cardLayout.next(cardShowingPanel);
}

public void previousCard() {
cardLayout.previous(cardShowingPanel);
}

public void showCard(String key) {
cardLayout.show(cardShowingPanel, key);
}

}

class Window1 extends JPanel {
/*
* Here this is our first Card of CardLayout, which will be added to the
* contentPane object of JPanel, which has the LayoutManager set to
* CardLayout. This card consists of Two JButtons.
*/
private ActionListener action;

public Window1() {
init();
}

private void init() {
final JButton clickButton = new JButton("CLICK ME");
final JButton dontClickButton = new JButton("DON\'T CLICK ME");

action = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == clickButton) {
JOptionPane.showMessageDialog(null, "Hello there dude!",
"Right Button", JOptionPane.INFORMATION_MESSAGE);
} else if (ae.getSource() == dontClickButton) {
JOptionPane.showMessageDialog(null,
"I told you not to click me!", "Wrong Button",
JOptionPane.PLAIN_MESSAGE);
}
}
};

clickButton.addActionListener(action);
dontClickButton.addActionListener(action);

add(clickButton);
add(dontClickButton);
}
}

class Window2 extends JPanel implements ActionListener {
/*
* Here this is our second Card of CardLayout, which will be added to the
* contentPane object of JPanel, which has the LayoutManager set to
* CardLayout. This card consists of a JLabel and a JTextField with
* GridLayout.
*/

private JTextField textField;

public Window2() {
init();
}

private void init() {
setLayout(new GridLayout(1, 2));
JLabel userLabel = new JLabel("Your Name : ");
textField = new JTextField();
textField.addActionListener(this);

add(userLabel);
add(textField);
}

public void actionPerformed(ActionEvent e) {
if (textField.getDocument().getLength() > 0)
JOptionPane.showMessageDialog(null,
"Your Name is : " + textField.getText(), "User\'s Name : ",
JOptionPane.QUESTION_MESSAGE);
}
}

class Window3 extends JPanel {
/*
* Here this is our third Card of CardLayout, which will be added to the
* contentPane object of JPanel, which has the LayoutManager set to
* CardLayout. This card consists of Two JLabels and two JCheckBox with
* GridLayout.
*/
private ActionListener state;

public Window3() {
init();
}

public void init() {
setLayout(new GridLayout(2, 2));
JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
final JCheckBox maleBox = new JCheckBox();
JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
final JCheckBox femaleBox = new JCheckBox();

state = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (maleBox == (JCheckBox) ae.getSource()) {
femaleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Male",
"Gender : ", JOptionPane.INFORMATION_MESSAGE);
} else if (femaleBox == (JCheckBox) ae.getSource()) {
maleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Female",
"Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
}
};

maleBox.addActionListener(state);
femaleBox.addActionListener(state);
add(maleLabel);
add(maleBox);
add(femaleLabel);
add(femaleBox);
}
}

关于java - CardLayout 从另一个类更改面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28488458/

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