练习一、【jsplitpane】分割面板,一次性把两个组件显示在一个分割面板之中。JSplitPane用于划分两个(也只有两个) Components。
import java.awt.Color;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class JSplitPaneTest extends JFrame implements ActionListener {
JPanel p,p1,p2;
JButton b;
JSplitPane sp;
int ore=0;
public JSplitPaneTest() {
p=new JPanel();
p1=new JPanel();
p2=new JPanel();
b=new JButton("改变");
p1.setBackground(Color.green);
p2.setBackground(Color.RED);
sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,p1,p2);
//参数分别是:分割线的位置(水平分割还是垂直分割);要放的组件1;要放的组件2
p.add(b);
this.add("Center", sp);
this.add("South", p);
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new JSplitPaneTest();
}
}
练习二、点击按钮,改变分割线的位置
package org.zhaiyujia.pkg1;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class JSplitPaneTest extends JFrame implements ActionListener {
JPanel p,p1,p2;
JButton b;
JSplitPane sp;
int ore=0;
public JSplitPaneTest() {
p=new JPanel();
p1=new JPanel();
p2=new JPanel();
b=new JButton("改变");
p1.setBackground(Color.green);
p2.setBackground(Color.RED);
sp=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,p1,p2);
//参数分别是:分割线的位置(水平分割还是垂直分割);要放的组件1;要放的组件2.
//放容器组件有优势(可以在它上面继续放其他非容器组件),也可以直接放非容器组件
p.add(b);
this.add("Center", sp);
this.add("South", p);
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.addActionListener(this);
}
public static void main(String[] args) {
new JSplitPaneTest();
}
@Override
public void actionPerformed(ActionEvent e) {
if(ore==0) {
sp.setOrientation(JSplitPane.VERTICAL_SPLIT);
ore=1;
}else {
sp.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
ore=0;
}
}
}
练习三、【setOneTouchExpandable(boolean newValue) 】设置 oneTouchExpandable属性的值,它们必须为 true为 JSplitPane提供一个UI小部件在分隔符上以快速扩展/折叠分频器。