JFrame 中的常用組件之一:JSplitPane–拆分窗格,就是被分爲兩部分的容器,左邊爲一個容器,右邊爲一個容器,可設置爲 左右分佈和上下分佈,HORIZONTAL_SPLIT和VERTICAL_SPLIT。兩邊分別放置組件。
下面是我自己寫的一個簡單的例子(ps,新手,代碼可能不太規範):
package socket;
import java.awt.*;
import javax.swing.*;
class Window extends JFrame
{
JPanel myPanel1 = new JPanel();//面板1
JPanel myPanel2 =new JPanel();//面板2
JButton button1 = new JButton("開始");//按鈕1
JButton button2 = new JButton("開始");//按鈕2
JButton button3 = new JButton("開始");//按鈕3
JSplitPane jSplitPane =new JSplitPane();//設定爲左右拆分佈局
public Window()
{
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init()
{
jSplitPane.setOneTouchExpandable(true);//讓分割線顯示出箭頭
jSplitPane.setContinuousLayout(true);//操作箭頭,重繪圖形
//jSplitPane.setPreferredSize(new Dimension (100,200));
jSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);//設置分割線方向
myPanel1.setSize(400, 400);
myPanel2.setSize(200, 400);
jSplitPane.setLeftComponent(myPanel1);//佈局中添加組件 ,面板1
jSplitPane.setRightComponent(myPanel2);//添加面板2
jSplitPane.setDividerSize(1);//設置分割線的寬度
//jSplitPane.setDividerLocation(100);//設置分割線位於中央
jSplitPane.setDividerLocation(400);//設定分割線的距離左邊的位置
setContentPane(jSplitPane);
//pack();
myPanel1.add(button1);
myPanel2.add(button3);
myPanel2.add(button2);
//add(myPanel1);
myPanel1.setBorder(BorderFactory.createLineBorder(Color.green));
//add(myPanel2);
myPanel2.setBorder(BorderFactory.createLineBorder(Color.red));
}
}
public class Test1
{
public static void main(String args[])
{
Window window = new Window();
window.setTitle(" 1.0.0---Damon");
window.setBounds(100, 100, 600, 400);
}
}
效果如下圖: