Java Swing:JSplitPane 在启动时拆分两个组件的大小相等
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20437097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing : JSplitPane split two component equals size at start up
提问by hqt
I'm using JSplitPane
includes two JScrollPane
at each side. I don't know how to make them at equals size at start up. Here is my main code:
我使用的是每边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);
I have used splitPane.setDividerLocation(getWidth() / 2);
but still no effect.
我用过splitPane.setDividerLocation(getWidth() / 2);
但是还是没有效果。
Please tel me how to fix this.
请告诉我如何解决这个问题。
For more detail. Here is my full code:
欲知更多详情。这是我的完整代码:
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);
}
}
Thanks :)
谢谢 :)
采纳答案by Paul Samsotha
I got it right for you. I add this;
我为你准备好了。我加上这个;
contentPane.add(splitPane, BorderLayout.CENTER);
splitPane.setResizeWeight(0.5); <------- here :)
And I got rid of the setDviderLocation()
at the bottom
我摆脱setDviderLocation()
了底部的
Inititally sets the resize wieght property. values are 0.0 to 1.0, a double value percentage to split the pane. There's a whole lot to exaplain about preferred sizes and such that I read about in the JSplitPane tutorial, so you can check it out for yourself.
最初设置调整大小 wight 属性。值为 0.0 到 1.0,这是用于拆分窗格的双倍值百分比。有很多关于首选尺寸的解释,我在JSplitPane 教程中读到过,所以你可以自己检查一下。
回答by Masudul
Initially getWidth()
size is 0
. Add splitPane.setDividerLocation(getWidth()/2);
after setvisible(true)
. Try,
最初getWidth()
大小是0
. splitPane.setDividerLocation(getWidth()/2);
之后添加 setvisible(true)
。尝试,
// put two JScrollPane into SplitPane
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPanelLeft, scrollPanelRight);
splitPane.setOneTouchExpandable(true);
// still no effect
add(splitPane, BorderLayout.CENTER);
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
setVisible(true);// JFrame setVisible
splitPane.setDividerLocation(getWidth()/2); //Use setDividerLocation here.
回答by camickr
It really depends on the exact behaviour you want for the split pane.
这实际上取决于您想要的拆分窗格的确切行为。
You can use:
您可以使用:
splitPane.setResizeWeight(0.5f);
when you create the split pane. This affects how the space is allocated to each component when the split pane is resized. So at start up it will be 50/50. As the split pane increased in size the extra space will also be split 50/50;
创建拆分窗格时。这会影响拆分窗格调整大小时如何将空间分配给每个组件。所以在启动时它将是 50/50。随着拆分窗格大小的增加,额外的空间也将按 50/50 拆分;
splitPane.setDividerLocation(.5f);
This will only give an initial split of 50/50. As the split pane size is increased, the extra space will all go to the last component. Also, note that this method must be invoked AFTER the frame has been packed or made visible. You can wrap this statement in a SwingUtilities.invokeLater() to make sure the code is added to the end of the EDT.
这只会给出 50/50 的初始分割。随着拆分窗格大小的增加,额外的空间将全部用于最后一个组件。另请注意,必须在帧被打包或使其可见后调用此方法。您可以将此语句包装在 SwingUtilities.invokeLater() 中,以确保将代码添加到 EDT 的末尾。