按比例分割窗口(JSplitPane)

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;
import javax.swing.JSplitPane;

public class Test extends JFrame{
    public static void main(String[] args) {
       new Test();
    }
    public Test(){
        JSplitPane jsp=new JSplitPane();

        this.add(jsp);

        //设置分割线不可移动
        jsp.setEnabled(false);
        //设置监听器,获取当前窗口的大小,按比例分配
        this.addComponentListener(new ComponentAdapter(){
            public void componentResized(ComponentEvent e) {
                jsp.setDividerLocation(0.67);
            }
        });

        this.setLocation(200,200);
        this.setSize(400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
} 
发布了88 篇原创文章 · 获赞 47 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/dai_ma_dong/article/details/103464611
今日推荐