我花了一段时间从我的大型程序创建sscce,我希望它足够小!ScrollPane扩展组件时添加
我有一个JSplitPane顶部的表,下面是一个JPanel。
底部面板包含较小的JPanel或“条目”。随着条目数量的增加,底部的SplitPane将占据顶部窗格的空间。
Dimension dim = getPreferredSize();
setPreferredSize(dim);
在第一类中,取消注释此代码解决了这个问题,但我不知道为什么。如果有人能帮忙,我想了解如何更好地调整调整大小? (我也得到了处理文本区域水平扩展,这是一个原因一些约束可能看起来有点古怪)
这里的底部面板:
package example;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.border.Border;
public class BottomPanel extends JPanel{
private JLabel summary = new JLabel();
private EntryPanel entryPanel = new EntryPanel();
public BottomPanel() {
//Dimension dim = getPreferredSize();
//setPreferredSize(dim);
setLayout(new GridBagLayout());
entryPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
setComponents();
}
private void setComponents() {
Border test = BorderFactory.createLineBorder(Color.BLUE);
JScrollPane jsp = new JScrollPane(entryPanel);
JPanel dummy1 = new JPanel();
JPanel dummy2 = new JPanel();
JPanel dummy3 = new JPanel();
dummy1.setBorder(test);
dummy2.setBorder(test);
dummy3.setBorder(test);
int row = 0;
add(dummy1, new GBC(0,row).setWeight(0,0));
// new row
row++;
add(summary, new GBC(1,row).setAnchor(GBC.LINE_START));
// new row
row++;
add(jsp, new GBC(1,row).setWeight(50,70).setFill(GBC.BOTH));
// new row
row++;
add(dummy2, new GBC(2,row).setWeight(0,0));
}
private class EntryPanel extends JPanel{
private List<JPanel> entries = new ArrayList<>();
private int row = 0;
private EntryPanel(){
setLayout(new GridBagLayout());
for(int x = 0; x < 20; x++)
createEntryItem("TEST", "test text");
}
private void createEntryItem(String s, String text){
Border test = BorderFactory.createLineBorder(Color.GREEN);
JPanel entryItem = new JPanel();
entryItem.setBorder(test);
entryItem.setLayout(new GridBagLayout());
JLabel title = new JLabel(s);
JTextArea details = new JTextArea(text);
entryItem.add(title, new GBC(0,0).setAnchor(GBC.LINE_START));
entryItem.add(details, new GBC(0,1).setAnchor(GBC.LINE_START));
entryItem.add(new JPanel(), new GBC(1,0).setWeight(100,0));
entries.add(entryItem);
displayEntry(entryItem);
}
private void displayEntry(JPanel entry){
JPanel dummy = new JPanel();
add(entry, new GBC(0,row).setAnchor(GBC.LINE_START).setFill(GBC.HORIZONTAL));
add(dummy, new GBC(1,row).setWeight(100, 0));
row++;
}
}
}
和含框架和面板:
package example;
import java.awt.*;
import javax.swing.*;
public class Example extends JFrame{
private GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
private int width = (int)((gd.getDisplayMode().getWidth())*0.5);
private int height = (int)((gd.getDisplayMode().getHeight())*0.75);
private JPanel mainPanel = new JPanel();
private JSplitPane sp;
private JTable table = new JTable();
private BottomPanel bp = new BottomPanel();
public Example() {
setSize(width,height);
setLayout(new BorderLayout());
sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT,new JScrollPane(table), bp);
add(sp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Example ex = new Example();
}
});
}
}
和GridBagHelper:
package example;
import java.awt.GridBagConstraints;
public class GBC extends GridBagConstraints{
public GBC(int gridx, int gridy){
this.gridx = gridx;
this.gridy = gridy;
}
public GBC setAnchor(int anchor){
this.anchor = anchor;
return this;
}
public GBC setFill(int fill){
this.fill = fill;
return this;
}
public GBC setWeight(double weightx, double weighty){
this.weightx = weightx;
this.weighty = weighty;
return this;
}
}
任何帮助或建议将非常感激 - 我是自学成才,正在努力争取公平的一点!
感谢
调用'setPreferredSize'将阻止组件计算它自己的首选大小 – MadProgrammer
您的期望是什么? – MadProgrammer
我认为我通过调用getPreferredSize()来检索它的首选大小的组件计算。设置这个值似乎是多余的 - 但毫无疑问,我误解了发生了什么 – NickW