【发布时间】:2017-04-30 21:24:11
【问题描述】:
我尝试了很多布局管理器,但没有一个可以解决我的问题:
- 我希望滚动窗格中的项目保持其大小(首选或最小),而不是调整大小(缩小)以适合视口面板。因为如果它是一个 JTextArea,并且如果文本区域有空白区域并且比视口大,它会缩小它,因此不会显示空白文本区域。我希望显示空白文本区域以解决外观问题。
- 我正在使用 BoxLayout 一个接一个地堆叠项目,在我看来,对于文本区域,setMinimum 方法失败了。
- 如果文本区域有空格,则 ScrollPane 的滚动条不会出现,而只会在没有空格的情况下出现。
有什么办法吗?
JScrollPane materialPane = new FScrollPane();
this.materialPaneView = new TPanel();
this.materialPaneView.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/2)));
this.materialPaneView.setLayout(new BoxLayout(this.materialPaneView, BoxLayout.Y_AXIS));
materialPane.setViewportView(materialPaneView);
materialPane.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/2)));
for(Material mat: this.unit.getMaterial()){
this.addMaterial(mat);
}
centerPanel.add(sectionPane);
centerPanel.add(exercisePane);
centerPanel.add(materialPane);
this.add(upperPanel, BorderLayout.NORTH);
this.add(centerPanel, BorderLayout.CENTER);
public void addMaterial(Material mat){
JTextField matName = new JTextField(30);
JPanel fieldButtonPanel = new TPanel();
fieldButtonPanel.setLayout(new GridLayout(1,2));
JPanel fieldPanel = new TPanel(new FlowLayout(FlowLayout.LEFT));
JPanel deleteMatButtonPanel = new TPanel(new FlowLayout(FlowLayout.RIGHT));
matName.setText(mat.getName());
matName.setMaximumSize(new Dimension(FFont.def.getSize()*20, 30));
fieldPanel.add(matName);
JButton deleteMat = new JButton("Delete Material");
deleteMatButtonPanel.add(deleteMat);
fieldButtonPanel.add(fieldPanel);
fieldButtonPanel.add(deleteMatButtonPanel);
fieldButtonPanel.setAlignmentX(LEFT_ALIGNMENT);
JTextArea matText = new FTextArea(mat.getDesc(), (int)(WIDTH*0.95), (int)(HEIGHT/3.4));
matText.setMinimumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/3.5)));
/*matText.setMaximumSize(new Dimension((int)(WIDTH*0.95), (int)(HEIGHT/3.4)));*/
matText.setText(mat.getDesc());
matText.setAlignmentX(LEFT_ALIGNMENT);
this.materialPaneView.add(fieldButtonPanel);
this.materialPaneView.add(matText);
matName.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
mat.setName(matName.getText());
}
});
HEIGHT 和 WIDTH 是常量,TPanel FScrollPane 是我预定义的透明面板。 BoxLayout 面板是 scrollPane 的视口,它仍然会调整文本区域的大小。
【问题讨论】:
-
请发布您的minimal reproducible example 代码,并可能显示您观察到的行为与期望行为的图像。您当然也知道,您可以通过调用采用两个整数的构造函数来设置 JTextArea 所需的可见列和行数,对吧?
-
是的,我确实设置了。立即发布我的代码
-
请重新阅读minimal reproducible example链接。
标签: java swing jscrollpane