Swing的JSplitPane的垂直拆分示例

说明

SplitPane类允许在两个面板之间进行可移动拆分。

代码示例

package com.yiidian;

import javax.swing.*;
import java.awt.*;

public class SwingTester {
   public static void main(String[] args) {
      createWindow();
   }

   private static void createWindow() {    
      JFrame frame = new JFrame("一点教程网:Swing Tester");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      createUI(frame);
      frame.setSize(560, 200);      
      frame.setLocationRelativeTo(null);  
      frame.setVisible(true);
   }

   private static void createUI(final JFrame frame){  
      JPanel buttonPanel = new JPanel(new FlowLayout());
      buttonPanel.add(new JButton("OK"));
      buttonPanel.add(new JButton("Cancel"));    
      JPanel textBoxPanel = new JPanel(new FlowLayout());
      textBoxPanel.add(new JLabel("Name:"));
      textBoxPanel.add(new JTextField(20));
      JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, buttonPanel, textBoxPanel);
      splitPane.setOneTouchExpandable(true);
      splitPane.setDividerLocation(50);
      frame.getContentPane().add(splitPane, BorderLayout.CENTER);      
   }
}

执行效果如下:

热门文章

优秀文章