Java Swing:JSplitPane在启动时拆分两个组件等于大小

hqt*_*hqt 9 java swing

我正在使用每侧JSplitPane包括两个JScrollPane.我不知道如何在启动时将它们设置为等于大小.这是我的主要代码:

 contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
            inputTextArea = new JTextArea();
    outputTextArea = new JTextArea();

    // put two TextArea to JScrollPane so text can be scrolled when too long
    JScrollPane scrollPanelLeft = new JScrollPane(inputTextArea);
    JScrollPane scrollPanelRight = new JScrollPane(outputTextArea);

    // put two JScrollPane into SplitPane 
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
            scrollPanelLeft, scrollPanelRight);
    splitPane.setOneTouchExpandable(true);
    splitPane.setDividerLocation(650);    // still no effect
    contentPane.add(splitPane, BorderLayout.CENTER);
Run Code Online (Sandbox Code Playgroud)

我用过splitPane.setDividerLocation(getWidth() / 2);但仍然没有效果.

请告诉我如何解决这个问题.

更多细节.这是我的完整代码:

package com.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

//import com.controller.Controller;

public class Main extends JFrame {

    private static final long serialVersionUID = 1L;

    private JPanel contentPane;
    public JTextArea inputTextArea;
    public JTextArea outputTextArea;
    private JButton inputBtn;
    private JButton outputBtn;
    private JButton sortBtn;
    public JRadioButton firstButton;
    public JRadioButton secondButton;
    public JRadioButton thirdButton;
    JSplitPane splitPane;

    //Controller controller;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Main() {
    //  controller = new Controller(this);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);  
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));

        /**
         * center
         * include two TextArea for display text
         */

        inputTextArea = new JTextArea();
        outputTextArea = new JTextArea();

        // put two TextArea to JScrollPane so text can be scrolled when too long
        JScrollPane scrollPanelLeft = new JScrollPane(inputTextArea);
        JScrollPane scrollPanelRight = new JScrollPane(outputTextArea);

        // put two JScrollPane into SplitPane 
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                scrollPanelLeft, scrollPanelRight);
        splitPane.setOneTouchExpandable(true);
        contentPane.add(splitPane, BorderLayout.CENTER);

        /**
         * Top
         * Include two button : SelectFile and WriteToFile
         * this layout includes some tricky thing to done work
         */

        // create new input button
        inputBtn = new JButton("Select File");
        // declare action. when user click. will call Controller.readFile() method 
        // (see this method for detail)
        inputBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            //  controller.readFile();
            }
        });

        // create new output button
        outputBtn = new JButton("Write To File");
        // declare action. when user click. will call Controller.writeFile() method 
        // (see this method for detail)
        outputBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            //  controller.writeFile();
            }
        });

        // put each button into seperate panel
        JPanel tmpPanel1 = new JPanel();
        tmpPanel1.add(inputBtn);
        JPanel tmpPanel2 = new JPanel();
        tmpPanel2.add(outputBtn);

        // finnally. put those two pane into TopPane
        // TopPane is GridLayout
        // by using this. we can sure that both two button always at center of screen like Demo
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(1, 2));
        topPanel.add(tmpPanel1);
        topPanel.add(tmpPanel2);
        contentPane.add(topPanel, BorderLayout.NORTH);

        /**
         * Bottom panel
         * Include all radionbutton and sortbutton
         */

        // Group the radio buttons.
        firstButton = new JRadioButton("Last Name");
        secondButton = new JRadioButton("Yards");
        thirdButton = new JRadioButton("Rating");
        // add those button into a group
        // so . ONLY ONE button at one time can be clicked
        ButtonGroup group = new ButtonGroup();
        group.add(firstButton);
        group.add(secondButton);
        group.add(thirdButton);

        // create sor button
        sortBtn = new JButton("Sort QB Stats");
        // add action for this button : will Call Controller.SortFile()
        sortBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            //  controller.sortFile();
            }
        });

        // add all to bottomPanel
        JPanel bottomPanel = new JPanel(new FlowLayout());
        bottomPanel.add(firstButton);
        bottomPanel.add(secondButton);
        bottomPanel.add(thirdButton);
        bottomPanel.add(sortBtn);
        contentPane.add(bottomPanel, BorderLayout.SOUTH);

        setContentPane(contentPane);
        setTitle("2013 College Quarterback Statistics");
        setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
        setVisible(true);
        System.out.println("getwidth: " + getWidth());
        splitPane.setDividerLocation(getWidth()/2);
    }

}
Run Code Online (Sandbox Code Playgroud)

谢谢 :)

Pau*_*tha 8

我找到了你的权利.我加上这个;

contentPane.add(splitPane, BorderLayout.CENTER);
splitPane.setResizeWeight(0.5);  <------- here :)
Run Code Online (Sandbox Code Playgroud)

我摆脱setDviderLocation()了底部

初始设置resize wieght属性.值为0.0到1.0,是拆分窗格的双倍值百分比.有很多关于首选大小的内容,以及我在JSplitPane教程中读到的内容,所以你可以自己检查一下.


cam*_*ckr 5

这实际上取决于拆分窗格所需的确切行为.

您可以使用:

splitPane.setResizeWeight(0.5f);
Run Code Online (Sandbox Code Playgroud)

在创建拆分窗格时.这会影响调整拆分窗格大小时为每个组件分配空间的方式.所以在启动时它将是50/50.随着分割窗格尺寸的增加,额外的空间也将分成50/50;

splitPane.setDividerLocation(.5f);
Run Code Online (Sandbox Code Playgroud)

这只会给出50/50的初始分割.随着拆分窗格大小的增加,额外的空间将全部转到最后一个组件.另请注意,必须在框架打包或可见后调用此方法.您可以将此语句包装在SwingUtilities.invokeLater()中,以确保将代码添加到EDT的末尾.