Swing组件JSplitPane的用法

2015-01-02 18:12:03|?次阅读|上传:wustguangh【已有?条评论】发表评论

关键词:Java, 界面设计|来源:唯设编程网

3. 实例代码:

下面是一个完整使用JSplitPane的测试代码:

package JSplitPane;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Container;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;

public class JSplitPaneDemo {
	public JSplitPaneDemo() {
		//1.测试标签
		JLabel greenLabel = new JLabel("TopLeft", JLabel.CENTER);
		greenLabel.setBackground(Color.GREEN);
		greenLabel.setOpaque(true);
		
		JLabel pinkLabel = new JLabel("TopRight", JLabel.CENTER);
		pinkLabel.setBackground(Color.PINK);
		pinkLabel.setOpaque(true);
		
		JLabel yellowLabel = new JLabel("Bottom", JLabel.CENTER);
		yellowLabel.setBackground(Color.YELLOW);
		yellowLabel.setOpaque(true);
		
		//2.JSplitPane的用法
		// 將label1和label2加入到splitPane1中
		// 并設置splitPane1為水平分割(左右分割),splitPane1具有Continuous layout的功能
		JSplitPane splitPaneTop = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				false, greenLabel, pinkLabel);
		// 設置splitPane1水平分割線的位置
		splitPaneTop.setDividerLocation(0.3);
		splitPaneTop.setOneTouchExpandable(true);// 設置splitPane是否可以展開或收起
		splitPaneTop.setDividerSize(10);// 設置分割線寬度的大小
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true,
				splitPaneTop, yellowLabel);
		splitPane.setDividerLocation(35);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerSize(5);
		
		//3.测试对话框
		JFrame frame = new JFrame("JSplitPaneDemo");
		Container contentPane = frame.getContentPane();
		contentPane.add(splitPane);		
		frame.setSize(250, 200);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	public static void main(String[] args) {
		new JSplitPaneDemo();
	}
}

这段代码最终实现的效果如下:

Swing组件JSplitPane的用法

<12>
发表评论0条 】
网友评论(共?条评论)..
Swing组件JSplitPane的用法