[英]Expanding a node of a JTree minimizes GridBagLayout
我創建了一個簡單的Swing應用程序,該應用程序當前由GridBagLayout內部的JToolBar,JTree和RSyntaxTextArea組成 。
JTree當前只有一個頂級節點,而只有一個子節點。 具有JToolBar,JTree和RSyntaxTextArea的完整UI
擴展JTree的頂部節點時,整個GridBagLayout種類為“最小化”:
我已經用谷歌搜索了這個現象,但是由於控制台中沒有錯誤消息或其他內容,我現在有點無助。
我正在使用以下代碼創建UI:
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);
}
});
}
}
擴展JTree的頂部節點時,整個GridBagLayout種類為“最小化”:
如果沒有足夠的空間來顯示整個組件,則GridBagLayout將縮小到組件的最小尺寸。
Swing應用程序當前由GridBagLayout內部的JToolBar,JTree和RSyntaxTextArea組成。
我只使用框架的默認BorderLayout
:
add(toolbar, BorderLayout.PAGE_START);
add(treeScrollPane, BorderLayout.CENTER);
add(textAreaScrollPane, BorderLayout.PAGE_END);
注意如何將JTree添加到JScrollPane。 現在,在需要時將為樹顯示滾動條。
如果您確實要使用GridBagLayout
閱讀Swing教程中有關如何使用GridBagLayout的部分, 以獲取有關如何使用各種約束的說明。 您可能想從“ weightx / y”約束開始,該約束控制隨着幀大小的改變哪些組件獲得空間。 另外,請查看“填充”約束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.