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 切换_java – 在JFrame中切换面板相关推荐
- java jframe教程_Java Swing JFrame窗口的实现
1. 概述 官方JavaDocsApi: javax.swing.JFrame JFrame,窗口.JFrame 是一个可以独立显示的组件,一个窗口通常包含有标题.图标.操作按钮(关闭.最小化.最大化 ...
- java SE 费用_Java SE 6中的垃圾回收器G1收费是虚惊一场
[51CTO快译]在Sun宣布Java SE 6 update 14版本中的垃圾回收器G1将收费之后,引起了Java社区相当大的反响.之后不久的6月5日,有一个细心的匿名读者发表了这样一个帖子: &q ...
- java 透明对话框_java实现jframe透明窗体示例
java实现jframe透明窗体示例 复制代码 代码如下: import javax.swing.JFrame; public class TansluFrame extends JFrame { p ...
- java幻灯片效果_Java实现多种幻灯片切换特效(附源码)
Java实现多种幻灯片切换特效(附源码)以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 功能说明 代码实现了多种幻灯片变 ...
- java界面分割_java将一个JFrame界面分割成多部分,每部分添加独立背景图片
现在,我要完成的目标是:创建了一个JFrame界面,我要分成两部分,操作时两部分完全独立,互不影响,已完成的效果如下: 可以看到,我做的左边是操作界面,右边是显示界面,左边的背景我没加,方法和右边的是 ...
- java 图片动画_java在窗口中添加图片做动画,怎么一闪一闪的?
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 视频来自:优酷 在java中添加图片做动画效果,怎么一闪一闪的运动啊?怎样才能让它不闪的连续运动呢? 下面是代码,代码项目project里有两个类,一个是 ...
- java jtextarea字体_java – 更改JTextArea中不同行的字体
我想在JTextArea中添加不同的字体行,但是最后一个字体似乎覆盖了另一个字体. 请帮忙- import java.awt.*; import javax.swing.*; @SuppressWar ...
- 各种存储分配算法java代码实现_Java实现操作系统中四种动态内存分配算法:BF+NF+WF+FF...
1 概述 本文是利用Java实现操作系统中的四种动态内存分配方式 ,分别是:BF NF WF FF 分两部分,第一部分是介绍四种分配方式的概念以及例子,第二部分是代码实现以及讲解. 2 四种分配方式 ...
- java jtextarea 滚动条_java 在JTextArea中显示 滚动条
我的代码为什么不显示垂直滚动条是那里错了么如果有错求高手修改importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava ...
最新文章
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(12)-系统日志和异常的处理②...
- https://www.exploit-db.com/能够成功下载的一个CVE编号
- System.Transactions深入了解
- STM32开发 -- YModem详解
- Android 入门之Intent应用
- lm358在哪个库里_库里8和aj35哪个好 库里8是安德玛的吗
- FreeSql (二十六)贪婪加载 Include、IncludeMany、Dto、ToList
- jquery学习文档
- java基于spingboot+vue的拼团旅游系统 elementui
- 基于B_spline 的非刚性形变
- 除了美团点评合并,国庆长假O2O还发生了什么?
- 游戏策划学习(二)---游戏策划与开发方法---常见的游戏类型
- python画最简单的折线图,推荐origin画图软件
- 【Python学习】导入类
- 近8成本科毕业生起薪过万?程序员收入这么高?
- 3000亿紫光集团破产重整,原因是啥?
- 使用RT-Thread Studio DIY 迷你桌面时钟(四)| OLED显示时钟和温湿度(cpp组件 + u8g2软件包)
- 想要软件外包,企业开发APP和小程序如何才能不吃亏?
- 钉钉小程序实现富文本编辑
- 解决 Source Insight中的parse too complex问题