close
1. 客製JSplitPanel之程式範例
public class JSplitPanelDemo01 extends JPanel {
JPanel panel01, panel02;
JSplitPane splitPane;
public JSplitPanelDemo01() {
this.setLayout(new BorderLayout());
UIManager.put("SplitPane.oneTouchButtonSize", 8); // 置頂 or 置底 按鈕 大小
UIManager.put("SplitPane.oneTouchButtonOffset", 10); // 置頂 or 置底 按鈕 離左邊的距離
UIManager.put("SplitPaneDivider.draggingColor", Color.PINK); // 拖曳 Divider 時的顏色
panel01 = new JPanel();
panel02 = new JPanel();
panel01.setPreferredSize(new Dimension(400, 200));
panel01.add(new JButton("第一個按鈕"));
panel01.setBackground(Color.RED);
panel02.setPreferredSize(new Dimension(400, 140));
panel02.setBackground(Color.GREEN);
splitPane = new JSplitPane() {
@Override public void updateUI() {
super.updateUI();
BasicSplitPaneDivider divider = ((BasicSplitPaneUI) getUI()).getDivider();
/* 設置 Divider 邊框之顏色
* 同於 UIManager.put("SplitPaneDivider.border", BorderFactory.createLineBorder(Color.BLUE, 3));
* divider.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.BLUE));
*/
divider.setBorder(BorderFactory.createLineBorder(Color.BLUE, 3));
// 設置 Divider 背景顏色
divider.setBackground(Color.ORANGE);
}
};
// 設置 上下放置 之 內容
splitPane.setLeftComponent(panel01);
splitPane.setRightComponent(panel02);
// 顯示置頂 or 置底 按鈕,同於 UIManager.put("SplitPane.centerOneTouchButtons", true);
splitPane.setOneTouchExpandable(true);
// 設置 分割之方向 (JSplitPane.VERTICAL_SPLIT / JSplitPane.HORIZONTAL_SPLIT)
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
// 設置 Divider 之 寬度
splitPane.setDividerSize(20);
// 設置 置頂 or 置底 按鈕 背景顏色
BasicSplitPaneDivider divider = ((BasicSplitPaneUI) splitPane.getUI()).getDivider();
divider.setBackground(Color.ORANGE);
for (Component c: divider.getComponents()) {
if (c instanceof JButton) {
JButton b = (JButton) c;
b.setBackground(Color.ORANGE);
}
}
this.add(splitPane);
this.setPreferredSize(new Dimension(400, 240));
}
2. 主執行之程式範例
public static void createAndShowGUI() {
/*
// 寫法1:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}*/
// 寫法2:
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) { //Nimbus/Window/Windows
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JSplitPanelDemo01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JSplitPanelDemo01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JSplitPanelDemo01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JSplitPanelDemo01.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
JFrame frame = new JFrame("如何客製化JSplitPanel中Divider之風格");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new JSplitPanelDemo01());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
3. 執行結果
▲生成時之樣貌
▲按下置頂和置底之樣貌
▲拖曳Divider時與拖曳後之樣貌
[補充]
如果UI樣式選擇Nimbus或Window會造成此一寫法失敗,執行結果如下所示:
▲UI樣式選擇Nimbus之樣貌
▲UI樣式選擇Window之樣貌
參考網址:
http://ateraimemo.com/Swing/OneTouchButton.html
全站熱搜