jSplitPane 具体场景使用

本文介绍如何使用Java Swing中的JSplitPane组件来实现动态切换左右面板的功能。通过监听左侧按钮点击事件,移除并更换右侧面板内容,达到响应式布局的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用了一个JSplitPane,左边是导航面板,有按钮,右边是空的面板,击左边的导航按钮,右边就显示响应的面板窗口。
效果实现的具体方法是:(1)点击左侧按监听事件启动
				     (2)监听方法调用写好的改变面板显示的方法
				     (3)简单方法实例如下:
					public void first(){
						 	  JPanel first = new JPanel();
							first.setBackground(Color.black);
							jsp.remove(1);
							repaint();
							jsp.setRightComponent(first);
							validate();
					}           
说明:首先要删除掉JSplitPane面板中右侧的面板,调用repaint()方法后,再将要展示的面板添加到JSplitPane的右侧,再调用validate()方法。
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
/**
* @author wangsheng
*/
public class JSplitPaneTest extends JFrame{
public JRadioButton button1;
public JRadioButton button2;
public JRadioButton button3;
public ButtonGroup group;
public JSplitPane jsp;
public JPanel rightPanel;
public JSplitPaneTest() {
initUI();
initAction();
}
public JPanel leftPanel(){
JPanel leftPanel = new JPanel();
button1 = new JRadioButton("修改",true);
button1.setBounds(1, 0, 50, 20);
button2 = new JRadioButton("添加");
button2.setBounds(1, 30, 50, 20);
button3 = new JRadioButton("删除");
button3.setBounds(1, 60, 50, 20);
group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
leftPanel.add(button1);
leftPanel.add(button2);
leftPanel.add(button3);
return leftPanel;
}
public void first(){
JPanel first = new JPanel();
first.setBackground(Color.black);
jsp.remove(1);
repaint();
jsp.setRightComponent(first);
validate();
}
public void second(){
JPanel second = new JPanel();
second.setBackground(Color.blue);
jsp.remove(1);
repaint();
jsp.setRightComponent(second);
validate();
}
public void third(){
JPanel third = new JPanel();
third.setBackground(Color.green);
jsp.remove(1);
repaint();
jsp.setRightComponent(third);
validate();
}
public void initUI(){
rightPanel = new JPanel();
rightPanel.setBackground(Color.black);
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel(),rightPanel);
this.add(jsp);
this.setTitle("拆分窗口");
this.setBounds(300, 200, 800, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void initAction(){
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
first();
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
second();
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
third();
}
});
}
public static void main(String[] args) {
new JSplitPaneTest();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值