java jframe 切换

编程入门 行业动态 更新时间:2024-10-15 04:18:34

<a href=https://www.elefans.com/category/jswz/34/1770091.html style=java jframe 切换"/>

java jframe 切换

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

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));

}

}

更多推荐

java jframe 切换

本文发布于:2023-06-29 08:35:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/942186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:java   jframe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!