java jframe 切换_java – 在JFrame中切换面板

此博客展示了如何在Java Swing应用程序中利用CardLayout管理器在多个JPanel之间进行切换。通过示例代码,作者演示了创建两个JPanel(分别为红色和绿色背景)并在它们之间导航的方法。此外,还更新了代码,添加了一个JComboBox来让用户选择要显示的面板,增强了交互性。
摘要由CSDN通过智能技术生成

以下是一个帮助您的小例子:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CardLayoutExample

{

private JPanel contentPane;

private MyPanel panel1;

private MyPanel2 panel2;

private void displayGUI()

{

JFrame frame = new JFrame("Card Layout Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel contentPane = new JPanel();

contentPane.setBorder(

BorderFactory.createEmptyBorder(5, 5, 5, 5));

contentPane.setLayout(new CardLayout());

panel1 = new MyPanel(contentPane);

panel2 = new MyPanel2(contentPane);

contentPane.add(panel1, "Panel 1");

contentPane.add(panel2, "Panel 2");

frame.setContentPane(contentPane);

frame.pack();

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public static void main(String... args)

{

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

new CardLayoutExample().displayGUI();

}

});

}

}

class MyPanel extends JPanel

{

private JButton jcomp4;

private JPanel contentPane;

public MyPanel(JPanel panel)

{

contentPane = panel;

setOpaque(true);

setBackground(Color.RED.darker().darker());

//construct components

jcomp4 = new JButton ("openNewWindow");

jcomp4.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

CardLayout cardLayout = (CardLayout) contentPane.getLayout();

cardLayout.next(contentPane);

}

});

add(jcomp4);

}

@Override

public Dimension getPreferredSize()

{

return (new Dimension(500, 500));

}

}

class MyPanel2 extends JPanel

{

private JButton jcomp1;

private JPanel contentPane;

public MyPanel2(JPanel panel)

{

contentPane = panel;

setOpaque(true);

setBackground(Color.GREEN.darker().darker());

//construct components

jcomp1 = new JButton ("Back");

jcomp1.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

CardLayout cardLayout = (CardLayout) contentPane.getLayout();

cardLayout.next(contentPane);

}

});

add(jcomp1);

}

@Override

public Dimension getPreferredSize()

{

return (new Dimension(500, 500));

}

}

最新编辑

在CardLayout中显示您选择的JPanel

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class CardLayoutExample

{

private JPanel contentPane;

private MyPanel panel1;

private MyPanel2 panel2;

private MyPanel2 panel3;

private JComboBox choiceBox;

private String[] choices = {

"Panel 1",

"Panel 2",

"Panel 3"

};

private void displayGUI()

{

JFrame frame = new JFrame("Card Layout Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel contentPane = new JPanel();

contentPane.setBorder(

BorderFactory.createEmptyBorder(5, 5, 5, 5));

contentPane.setLayout(new CardLayout());

choiceBox = new JComboBox(choices);

panel1 = new MyPanel(contentPane, this);

panel2 = new MyPanel2(contentPane

, Color.GREEN.darker().darker(), this);

panel3 = new MyPanel2(contentPane

, Color.DARK_GRAY, this);

contentPane.add(panel1, "Panel 1");

contentPane.add(panel2, "Panel 2");

contentPane.add(panel3, "Panel 3");

frame.getContentPane().add(choiceBox, BorderLayout.PAGE_START);

frame.getContentPane().add(contentPane, BorderLayout.CENTER);

frame.pack();

frame.setLocationByPlatform(true);

frame.setVisible(true);

}

public JComboBox getChoiceBox()

{

return choiceBox;

}

public static void main(String... args)

{

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

new CardLayoutExample().displayGUI();

}

});

}

}

class MyPanel extends JPanel

{

private JButton jcomp4;

private JPanel contentPane;

private JComboBox choiceBox;

public MyPanel(JPanel panel, CardLayoutExample cle)

{

choiceBox = cle.getChoiceBox();

contentPane = panel;

setOpaque(true);

setBackground(Color.RED.darker().darker());

//construct components

jcomp4 = new JButton ("Show New Panel");

jcomp4.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String changeToPanel = (String) choiceBox.getSelectedItem();

CardLayout cardLayout = (CardLayout) contentPane.getLayout();

cardLayout.show(contentPane, changeToPanel);

}

});

add(jcomp4);

}

@Override

public Dimension getPreferredSize()

{

return (new Dimension(500, 500));

}

}

class MyPanel2 extends JPanel

{

private JButton jcomp1;

private JPanel contentPane;

private Color backgroundColour;

private JComboBox choiceBox;

public MyPanel2(JPanel panel, Color c, CardLayoutExample cle)

{

contentPane = panel;

backgroundColour = c;

choiceBox = cle.getChoiceBox();

setOpaque(true);

setBackground(backgroundColour);

//construct components

jcomp1 = new JButton ("Show New Panel");

jcomp1.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String changeToPanel = (String) choiceBox.getSelectedItem();

CardLayout cardLayout = (CardLayout) contentPane.getLayout();

cardLayout.show(contentPane, changeToPanel);

}

});

add(jcomp1);

}

@Override

public Dimension getPreferredSize()

{

return (new Dimension(500, 500));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值