我已经创建了一个简单的Swing应用程序,它目前由一个JToolBar,一个JTree和一个RSyntaxTextArea组成,全都在GridBagLayout内。扩展JTree的节点最小化GridBagLayout
JTree目前只有一个顶级节点,并且只有一个子节点。 与JToolBar中,JTree的和RSyntaxTextArea完整的UI
当扩大JTree的顶部节点,整个的GridBagLayout一种 “最小化” 的:
我GOOGLE了这个phenominum,但由于控制台中没有错误信息或其他信息,我现在很无助。
我用下面的代码来创建用户界面:
RSyntaxTextArea textArea = new RSyntaxTextArea(50, 150);
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
textArea.setCodeFoldingEnabled(true);
RTextScrollPane sp = new RTextScrollPane(textArea);
cp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
cp.add(createToolbar(), c);
c.gridx = 0;
c.gridy = 1;
c.ipadx = 90;
c.fill = GridBagConstraints.BOTH;
cp.add(createTree(), c);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
cp.add(sp, c);
...
private JToolBar createToolbar() {
JToolBar tb = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton ob = new JButton(new ImageIcon("..."));
tb.add(ob);
tb.setFloatable(false);
tb.setRollover(true);
return tb;
}
...
private JTree createTree() {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Projects");
JTree tree = new JTree(top);
DefaultMutableTreeNode test = new DefaultMutableTreeNode("I'm a test!");
top.add(test);
return tree;
}
更新:一个最小的代码示例来编译你的系统上测试目的:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Tester extends JFrame {
public Tester() {
initializeComponent();
}
private void initializeComponent() {
JPanel cp = new JPanel(new BorderLayout());
JTextArea textArea = new JTextArea(50, 150);
JScrollPane sp = new JScrollPane(textArea);
cp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
cp.add(createToolbar(), c);
c.gridx = 0;
c.gridy = 1;
c.ipadx = 90;
c.fill = GridBagConstraints.BOTH;
cp.add(createTree(), c);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
cp.add(sp, c);
setContentPane(cp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
private JTree createTree() {
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Projects");
JTree tree = new JTree(top);
DefaultMutableTreeNode test = new DefaultMutableTreeNode("I'm a test!");
top.add(test);
return tree;
}
private JToolBar createToolbar() {
JToolBar tb = new JToolBar("Toolbar", JToolBar.HORIZONTAL);
JButton ob = new JButton("Button");
tb.add(ob);
tb.setFloatable(false);
tb.setRollover(true);
return tb;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Tester().setVisible(true);
}
});
}
}
请考虑在您的问题中创建并发布一个有效的[mcve] ***小***程序,我们可以编译,运行,并为我们演示您的问题。 –
@HovercraftFullOfEels当然,更新了我的问题。 –
(1-)@AlexanderLeithner,您发布的代码不是'MCVE',因为我们无法访问自定义类。无论如何,你已经有了你的答案。有时间做一些阅读。 – camickr