JSplitPane中分隔条禁止拖动

转自:http://blog.csdn.net/sun1956/article/details/8237682


1、禁止分隔条:Java中分割面板是个不错的应用,可以将一个面板分隔成两个部分,一部分可以当作菜单,另一部分可以当作点击菜单项时的响应界面。

JSplitPane中的setDividerLocation(百分比);方法可以随意的设置自己想要的百分比。但是某些时候我们设置好百分比之后,想要禁止分隔条拖动。

可以采用setEnabled(false)禁止拖动。

2、如果想要让分隔条随着窗口大小变化,而保持比例,可以为该窗口添加监听器,在监听器里面重新设置百分比,这样分隔条就随着窗口大小变化而变化了。setDividerLocationpublic void setDividerLocation(double proportionalLocation) 这个方法一定要放在frame.setVisible(true);之后才有效

设置分隔条的位置为 JSplitPane 大小的一个百分比。

根据 setDividerLocation(int) 来实现此方法。此方法以分隔窗格的当前大小为基础迅速改变窗格的大小。如果分隔窗格没有正确地实现并且不显示在屏幕上,此方法将不产生任何影响

 

3、JSplitPane把面板分隔成两部分,但这两部分只能分别添加一个组件,如果想要添加多个组件,要设置一个新的JPanel对象添加进去,再向JPanel中添加组件,这样就可以添加多个组件了。

测试代码:

 

[java]  view plain copy
  1. import java.awt.event.ComponentAdapter;  
  2. import java.awt.event.ComponentEvent;  
  3.   
  4. import javax.swing.JButton;  
  5. import javax.swing.JFrame;  
  6. import javax.swing.JLabel;  
  7. import javax.swing.JPanel;  
  8. import javax.swing.JRadioButton;  
  9. import javax.swing.JSplitPane;  
  10.   
  11.   
  12. public class SplitPane extends JFrame{  
  13.   
  14.     /** 
  15.      * @author Sun 
  16.      */  
  17.     private static JSplitPane splitPane = new JSplitPane();  
  18.     private JLabel jbtTop = new JLabel("left:");  
  19.     private JLabel jbtBottom = new JLabel("right:");  
  20.     private JPanel panel1,panel2;  
  21.     private JRadioButton jrb1,jrb2;  
  22.     public SplitPane () {  
  23.         jrb1 = new JRadioButton("jrb1");  
  24.         jrb2 = new JRadioButton("jrb2");  
  25.         panel1 = new JPanel();  
  26.         panel2 = new JPanel();  
  27.         splitPane.setTopComponent(panel1);      //添加面板对象,这里添加组件的方法要注意,不是add  
  28.         panel1.add(jbtTop);                     //在面板对象中添加组件  
  29.         panel1.add(jrb1);  
  30.         splitPane.setBottomComponent(panel2);  
  31.         panel2.add(jbtBottom);  
  32.         panel2.add(jrb2);  
  33.         splitPane.setOneTouchExpandable(true);      //设置分割面板显示UI小部件,就是分隔条上的小三角  
  34.         add(splitPane);  
  35.         splitPane.setEnabled(false);            //设置分隔条禁止拖动  
  36.           
  37.     }  
  38.     public static void main(String[] args) {  
  39.         JFrame frame = new SplitPane();  
  40.         frame.setTitle("SplitPane");  
  41.         frame.setSize(300,200);  
  42.         frame.setLocationRelativeTo(null);  
  43.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  44.         frame.setVisible(true);  
  45.         //为frame窗口添加监听器,实现保持面板两部分的比例  
  46.         frame.addComponentListener(new ComponentAdapter(){  
  47.   
  48.             public void componentResized(ComponentEvent e) {  
  49.                     splitPane.setDividerLocation(0.4);}  
  50.         });  
  51.         //这里是面板第一次显示的时候,frame没有事件,所以要设置一次  
  52.         splitPane.setDividerLocation(0.4);    //设置分割比,一定要在frame.setVisible(true);之后才有效  
  53.     }  
  54.   
  55. }  

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值