java上下分页窗口流动布局

上下分页要用到

JSplitPane jSplitPane =new JSplitPane();//设定为拆分布局

效果图:

 

 

show me code:

 

 

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

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;

public class Mainui extends JFrame{
    JSplitPane jSplitPane =new JSplitPane();//设定为拆分布局
    private static final long serialVersionUID = 1L;
    JPanel jp1;                    //上面板1
    JTextPane jtp1;                   //下面板1
    JButton bt1;                  //选择项目文件按钮
    JButton bt2;                //开始转换按钮
    JComboBox<String> cmb1;       //创建下拉菜单
    JComboBox<String> cmb2;        //创建下拉菜鸟

    public Mainui(){
    
        this.setTitle("欢迎使用编码转换软件");
        this.setBounds(100, 100, 600, 500);
        
        jp1 = new JPanel();        
        jtp1=new JTextPane();
        bt1 = new JButton("打开项目");
        bt2 = new JButton("开始转换");
        cmb1=new JComboBox<String>();    //创建JComboBox
        cmb2=new JComboBox<String>();    //创建JComboBox
        
        cmb1.addItem("文本类型");    //向下拉列表1中添加项
        cmb1.addItem("所有文件");
        cmb1.addItem(".java");
        cmb1.addItem(".txt");
        cmb1.addItem(".c");
        cmb1.addItem(".cpp");
        cmb1.addItem(".py");
        
        cmb2.addItem("转换编码");    //向下拉列表2中添加项
        cmb2.addItem("UTF-8");
        cmb2.addItem("GB2312");
        cmb2.addItem("Unicode");
        
        jp1.add(bt1);                //将按钮,下拉菜单添加入jPanel容器
        jp1.add(cmb1);
        jp1.add(cmb2);
        jp1.add(bt2);
    

        jSplitPane.setContinuousLayout(true);//操作箭头,重绘图形
        jSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);//垂直方向
        jSplitPane.setTopComponent(jp1);//上下布局中添加组件 ,面板1(上)
        jSplitPane.setBottomComponent(jtp1);//上下布局中添加组件 ,面板1(下)
        
        setContentPane(jSplitPane);//将布局添加入窗口
        
        setVisible(true);//窗口设置可见
        
        jSplitPane.addComponentListener(new ComponentAdapter() {//拖动窗口监听 
             public void componentResized(ComponentEvent e) {  
                 if(jSplitPane.getWidth()<=210){
                     jSplitPane.setDividerLocation(140);//设置条高度
                 }
                 else if(jSplitPane.getWidth()<420){
                     jSplitPane.setDividerLocation(70);//设置条高度 
                 }else{
                     jSplitPane.setDividerLocation(40);//设置条高度 
                 }
             }  
        }); 
    }

    public static void main(String a[]){
        new Mainui();
    }

}

 

posted @ 2019-01-05 16:13  一文搞懂  阅读(1683)  评论(0编辑  收藏  举报