我想让一个组件占用Container的maximumAvailableHeight。例如,在我下面粘贴的代码中,我将根框架设置为800,600。我想设置只有那个帧的高度/宽度(我不想尝试像素化它的孩子)。如果我运行它,我会看到一个严重对齐的UI。
首先,我想要一个面板(在根框架内)占据框架的100%高度(在这种情况下,800px减去绘制框架标题所需的小空间)。
其次,在面板内我有一个树和文本区域。我希望他们两个都采取100%的高度,让树占30%,textArea采取70%的宽度(如果树扩展到10级,那么我可以使用ScrollPane)。
了解这在HTML中最容易实现。只说高度= 100%,宽度为30%等我们就完成了。有人知道这是否可以在Swing中完成? (我可以通过设置像素高度和布局管理器来实现这一目标,但我正在寻找设置百分比高度和宽度的最干净的解决方案。)
package com.ekanathk.logger.gui;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TestFrame extends JFrame {
public TestFrame() {
super("Top Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTree env = getEnvironmentTree();
env.expandRow(0);
panel.add(new JScrollPane(env));
panel.add(new JTextArea("Some contents"));
getContentPane().add(panel);
setSize(800, 600);
SwingUtil.centerComponentOnScreen(this);
setVisible(true);
}
private JTree getEnvironmentTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
JTree tree = new JTree(root);
DefaultMutableTreeNode one = new DefaultMutableTreeNode("One");
root.add(one);
one.add(new DefaultMutableTreeNode("under one.1"));
one.add(new DefaultMutableTreeNode("under one.2"));
root.add(new DefaultMutableTreeNode("two"));
root.add(new DefaultMutableTreeNode("three"));
return tree;
}
public static void main(String[] args) {
new TestFrame();
}
}
答案 0 :(得分:3)
这使用GridBagLayout来做你想要的事情。您可以使用组件的setMinimumSize和setPreferedSize。
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
public TestFrame() {
super("Top Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
JTree env = getEnvironmentTree();
env.expandRow(0);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx=.7;
c.weighty=1;
c.gridx=0;
c.gridy=0;
c.gridheight=1;
c.gridwidth=7;
panel.add(new JScrollPane(env),c);
c.weightx=.3;
c.gridx=7;
c.gridy=0;
c.gridheight=1;
c.gridwidth=GridBagConstraints.REMAINDER;
panel.add(new JTextArea("Some contents"),c);
add(panel);
setSize(800, 600);
setLocationRelativeTo(null);
setVisible(true);
}
private JTree getEnvironmentTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
JTree tree = new JTree(root);
DefaultMutableTreeNode one = new DefaultMutableTreeNode("One");
root.add(one);
one.add(new DefaultMutableTreeNode("under one.1"));
one.add(new DefaultMutableTreeNode("under one.2"));
root.add(new DefaultMutableTreeNode("two"));
root.add(new DefaultMutableTreeNode("three"));
return tree;
}
public static void main(String[] args) {
new TestFrame();
}
}
答案 1 :(得分:1)
有许多layout managers可以实现这一目标 - GridLayout(1x1网格),GridBagLayout,也许是BorderLayout。
答案 2 :(得分:1)
这是使用SpringLayout的另一种方法。我无法使Spring.width和Spring.height工作,所以我使用了更详细的方式
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.tree.DefaultMutableTreeNode;
public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
public TestFrame() {
super("Top Frame");
setDefaultCloseOperation(EXIT_ON_CLOSE);
SpringLayout layout = new SpringLayout();
JTree env = getEnvironmentTree();
env.expandRow(0);
JPanel contentPane = new JPanel(layout);
contentPane.setLayout(layout);
JScrollPane treePane = new JScrollPane(env);
JTextArea area = new JTextArea("Some contents");
contentPane.add(treePane);
contentPane.add(area);
setLayout(new BorderLayout());
add(contentPane);
setSize(800, 600);
SpringLayout.Constraints cons;
cons = layout.getConstraints(treePane);
cons.setX(Spring.constant(0));
cons.setY(Spring.constant(0));
cons.setWidth(Spring.scale(layout.getConstraint(SpringLayout.EAST, contentPane), .7f));
cons.setHeight(layout.getConstraint(SpringLayout.SOUTH, contentPane));
cons = layout.getConstraints(area);
cons.setX(layout.getConstraint(SpringLayout.EAST, treePane));
cons.setY(Spring.constant(0));
cons.setWidth(Spring.scale(layout.getConstraint(SpringLayout.EAST, contentPane), .3f));
cons.setHeight(layout.getConstraint(SpringLayout.SOUTH, contentPane));
setPreferredSize(getSize());
setLocationRelativeTo(null);
pack();
setVisible(true);
}
private JTree getEnvironmentTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
JTree tree = new JTree(root);
DefaultMutableTreeNode one = new DefaultMutableTreeNode("One");
root.add(one);
one.add(new DefaultMutableTreeNode("under one.1"));
one.add(new DefaultMutableTreeNode("under one.2"));
root.add(new DefaultMutableTreeNode("two"));
root.add(new DefaultMutableTreeNode("three"));
return tree;
}
public static void main(String[] args) {
new TestFrame();
}
}