Swing组件:简简单单实现菜单栏窗体切换

了解更多swing

最近,因为种种原因,使我不得不经常性的用到swing组件,这次做课设又要用到swing组件来写界面,本来想写个网页版的?但自生条件不允许呀!!!打算这个暑假把java学习视频看完,加油!!!

回到这次总结:
我们在用到swing组件开发界面时,用到菜单栏时,我们心中想,我们要怎样设计,才能实现窗体的切换,注意在这里我们只出现一个窗体,于是我们可能会出现这样一种想法,通过隐藏一个窗体,然后在打开一个窗体,的方法来实现,窗体的切换,但这样做,有一个问题,这样确实可以实现窗体的切换,但是这个切换的过程中会出现窗体闪烁这种现象,通俗而言就是肉眼很容易看出是通过隐藏这种方法来实现窗体切换的,那我们要怎样才能实现,窗体不闪烁,但还是能进行窗体的切换

重点来了!!!
接下来,我来讲讲我的,窗体切换的实现过程,大致思路是:我们只创建一个 frame (JFrame)窗体,然后创造多个 panel(JPanel),我们将菜单栏定义在 frame上,其他地方通过,frame.remove(panel) 和 frame.add(panel),来实现窗体的切换,在我的方法中实际就是 panel的切换,从而实现 窗体内容的改变,下面我们通过代码,进一步了解这种方法的实现过程。

代码如下:

首页:

package 窗体切换;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class MainFrame {

	//创造全局变量,并且私有
	private static JFrame frame = new JFrame();
	
	private static JPanel panel = null;
	
	public MainFrame() {
		
		frame = new JFrame("切换界面");
		frame.setBounds(100, 100, 1116, 790);
		frame.getContentPane().setLayout(null);
		
		//使窗体不能放大
		frame.setResizable(false);
		
		//使窗体在屏幕中间出现
		frame.setResizable(false);
		
		// 菜单栏
		// 新建一个菜单条
		JMenuBar jb = new JMenuBar();
		jb.setBounds(0, 0, 1116, 30);
		frame.getContentPane().add(jb);

		
		// 新建一个菜单选项
		JMenu jmenu1 = new JMenu("界面");
		jb.add(jmenu1);
				
		// 新建一个菜单项
		JMenuItem jm1 = new JMenuItem("界面一");

		jmenu1.add(jm1);
				
		JMenuItem jm2 = new JMenuItem("界面二");
		jmenu1.add(jm2);
		
		
		//初始  panel
		panel = new JPanel();
		
		frame.add(panel);
		
		panel.setBounds(-1, 31, 1103, 718);
		panel.setLayout(null);
		 
		JLabel label = new JLabel("首页");
		label.setFont(new Font("宋体", Font.PLAIN, 99));
		label.setBounds(356, 108, 326, 297);
		panel.add(label);
		
		
		
		//关键地方来了,下面就是窗体的切换过程了。
		
		//打开界面一
		jm1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				frame.remove(panel);
				panel = FirstPanel.firstView();
				
				//在这里,我们为什么要先frame.remove(panel),这是因为,如果我们不是remove,
				//而是add()话,会出现,有一些组件重合,那么就会导致我们一些功能,无法使用,所以我们要先remve掉frame当前的panel。
				
				//为什么要?panel = FirstPanel.firstView();
				//这是因为,frame添加的panel已经改变了,如果我们不重新赋值panel的话,那我们,想要再次切换时,将会remove以前的panel
				//对frame现在添加的panel,没有任何影响,所以要重新赋值panel
			}
		});
		
		//打开界面二
		jm2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				frame.remove(panel);
				panel = SecondPanel.secondView();
				//原因同上。
			}
		});
		
		
		//使窗体可见
		frame.setVisible(true);		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
	
	public static JFrame returnJFrame() {
		
		return frame;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		new MainFrame();
	}

}

界面一:

package 窗体切换;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FirstPanel {

	private static JPanel panel = null;
	private static JFrame frame = null;
	
	public static JPanel firstView(){
		
		//导入frame
		frame = MainFrame.returnJFrame();
		
		panel = new JPanel();
		
		//添加新的panel
		frame.add(panel);
		
		panel.setBounds(-1, 31, 1103, 718);
		panel.setLayout(null);
		 
		JLabel label = new JLabel("界面一");
		label.setFont(new Font("宋体", Font.PLAIN, 99));
		label.setBounds(356, 108, 326, 297);
		panel.add(label);
		
		return panel;
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

界面二:

package 窗体切换;

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SecondPanel {
	private static JPanel panel = null;
	private static JFrame frame = null;
	
	public static JPanel secondView() {
		
		//导入frame
		frame = MainFrame.returnJFrame();
		
		panel = new JPanel();
		
		//添加新的panel
		frame.add(panel);
		
		panel.setBounds(-1, 31, 1103, 718);
		panel.setLayout(null);
		 
		JLabel label = new JLabel("界面二");
		label.setFont(new Font("宋体", Font.PLAIN, 99));
		label.setBounds(356, 108, 326, 297);
		panel.add(label);
		
		return panel;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

效果如下:
在这里插入图片描述
最后在说一片:该方法一定要保证只有一个frame,有多个panel,通过不断的removeadd来实现窗体的切换。

如果看了我的讲解,和源码,还是有些地方不明白,可以在评论区留言。

  • 17
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
要利用Swing组件完成菜单功能实现,可以按照以下步骤进行: 1. 创建一个JFrame对象来作为应用程序的主窗口。 ``` JFrame frame = new JFrame("菜单功能"); ``` 2. 创建一个JMenuBar对象来作为菜单栏。 ``` JMenuBar menuBar = new JMenuBar(); ``` 3. 创建一个JMenu对象来作为菜单栏中的一个菜单项。 ``` JMenu menu = new JMenu("文件"); ``` 4. 创建JMenuItem对象作为菜单项,并将其添加到JMenu对象中。 ``` JMenuItem menuItem1 = new JMenuItem("新建"); JMenuItem menuItem2 = new JMenuItem("打开"); JMenuItem menuItem3 = new JMenuItem("保存"); menu.add(menuItem1); menu.add(menuItem2); menu.add(menuItem3); ``` 5. 将JMenu对象添加到JMenuBar对象中。 ``` menuBar.add(menu); ``` 6. 将JMenuBar对象添加到JFrame对象中。 ``` frame.setJMenuBar(menuBar); ``` 完整代码如下: ``` import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class MenuDemo { public static void main(String[] args) { JFrame frame = new JFrame("菜单功能"); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("文件"); JMenuItem menuItem1 = new JMenuItem("新建"); JMenuItem menuItem2 = new JMenuItem("打开"); JMenuItem menuItem3 = new JMenuItem("保存"); menu.add(menuItem1); menu.add(menuItem2); menu.add(menuItem3); menuBar.add(menu); frame.setJMenuBar(menuBar); frame.setSize(300, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } ``` 运行代码后,应用程序的主窗口上将会出现一个名为“文件”的菜单项,点击菜单项将会弹出三个菜单子项:“新建”、“打开”和“保存”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值