62,614
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MyFrame extends JFrame
{
JComboBox<String> options = new JComboBox<>();
JPanel cards = new JPanel();
public MyFrame(String title)
{
super(title);
Container contentPane = getContentPane();
//CardLayout cardLayout = (CardLayout)cards.getLayout();
// 给顶层容器,设置 BorderLayout
contentPane.setLayout(new BorderLayout());
contentPane.add(cards, BorderLayout.CENTER);
// 创建第1个面板
MyPanel p1 = new MyPanel();
JButton P1Button = new JButton("build a character");
p1.add(P1Button);
// P1Button.addActionListener(new ActionListener(){
// public void actionPerformed(ActionEvent e)
// {
// cardLayout.show(cards, "CharacterCreation");
// }
// });
//////////////////////////////////////////////////////////////////////////////////////
// 创建第2个面板
MyPanel p2 = new MyPanel();
JButton P2Button = new JButton("start battle");
p2.add(P2Button);
//P2Button.addActionListener();
//////////////////////////////////////////////////////////////////////////////////////
// 创建第3个面板
MyPanel p3 = new MyPanel();
JButton P3Button = new JButton("play again");
p3.add(P3Button);
//P3Button.addActionListener();
/////////////////////////////////////////////////
cards.setLayout(new CardLayout());
cards.add(p1, "Splash");
cards.add(p2, "CharacterCreation");
cards.add(p3, "BattleSummary");
if(???)
{
cardLayout.show(cards, "Splash");
}
else if(??)
{
cardLayout.show(cards, "CharacterCreation");
}
else if(???)
{
cardLayout.show(cards, "BattleSummary");
}
}
}
·public class MyFrame extends JFrame implements ActionListener {
CardLayout cl=new CardLayout();
public MyFrame() throws HeadlessException {
setTitle("卡片布局");
setSize(400,300);
setLocation(200,200);
setLayout(cl);
Panel p1=new Panel();
Button b1=new Button("第二张卡片");
b1.addActionListener(this::actionPerformed);
p1.add(b1);
Panel p2=new Panel();
Button b2=new Button("第三张卡片");
b2.addActionListener(this::actionPerformed);
p2.add(b2);
Panel p3=new Panel();
Button b3=new Button("第一张卡片");
b3.addActionListener(this::actionPerformed);
p3.add(b3);
add(p1);
add(p2);
add(p3);
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
cl.next(getContentPane());
}
public static void main(String[] args) {
new MyFrame();
}
}
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestCardLayout extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new TestCardLayout().setVisible(true);
}
// 窗体的内容面板
private JPanel contentPanel;
// 卡片式布局管理器,用于在内容面板中依次切换需要的面板
private CardLayout layout;
public TestCardLayout() {
setBounds(0, 0, 400, 300);
contentPanel = new JPanel();
layout = new CardLayout();
contentPanel.setLayout(layout);
setContentPane(contentPanel);
// 构造面板1,注意给按钮增加的事件监听
JPanel p1 = new JPanel();
JButton btn1 = new JButton("Go p2");
btn1.addActionListener(this);
p1.add(btn1);
// 构造面板2
JPanel p2 = new JPanel();
JButton btn2 = new JButton("Go p3");
btn2.addActionListener(this);
p2.add(btn2);
// 构造面板3
JPanel p3 = new JPanel();
JButton btn3 = new JButton("Go p1");
btn3.addActionListener(this);
p3.add(btn3);
// 将各个面板增加到内容面板
contentPanel.add(p1);
contentPanel.add(p2);
contentPanel.add(p3);
}
@Override
public void actionPerformed(ActionEvent e) {
// 卡片式布局管理器是可以依次切换面板的
layout.next(contentPanel);
}
}