gpt4 book ai didi

java - 如何使用按钮切换到特定的JPanel?

转载 作者:行者123 更新时间:2023-12-02 03:15:50 31 4
gpt4 key购买 nike

我不想使用 JPanel().next 和 JPanel().previous 转到下一个面板,而是想使用按钮切换到特定面板。

假设我有 3 个页面,通过使用标有“转到第 3 页”的按钮,它将带我到我为第 3 页创建的面板;在该页面上,我会有更多按钮可以带我返回第 1 页,甚至返回第 2 页。假设我有第十页,一个按钮可以直接带我到它,而不必单击下一个按钮。

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 CardLayoutExample
{
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 are 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 page1Button = new JButton("Go to page 1");
final JButton page5Button = new JButton("Go to Page 5");
final JButton page10Button = new JButton("Go to Page 10");
buttonPanel.add(page1Button);
buttonPanel.add(page5Button);
buttonPanel.add(page10Button);

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

//page10Button.addActionListener(new ActionListener();
//Code to navigate to page 10...

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

我设置了单击按钮时的方法,但它仅导航到下一页,而不是我想要的页面。

.next 和 .previous 还有哪些其他替代方案?我想转到特定页面。

感谢您的帮助。

最佳答案

添加到卡片布局时,您可以指定一个“键”,稍后在尝试显示特定面板时可以引用该“键”。下面的示例应该可以帮助您入门:

CardLayout myCardLayout = new CardLayout();
JPanel myCardLayoutPanel = new JPanel(myCardLayout);
myCardLayoutPanel.add(myComponent, "A_KEY");
myCardLayout.show(myCardLayoutPanel,"A_KEY");

此外,您应该查看 docs

关于java - 如何使用按钮切换到特定的JPanel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40328735/

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