use*_*119 3 java swing jscrollpane
我有一个Java Swing GUI,它包含一些包含JTextAreas的JScrollPanes,它们会填充数据,以便显示滚动条.一旦滚动条出现,当我将窗口移动到第二个监视器时,JScrollPane变得非常小,以至于我再也看不到它包含的文本了.
例:
这是我的主监视器上的窗口,其中JScrollPane填充了文本:

这是一个演示问题的小示例程序:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
public Main() {
Test theGui = new Test();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
add(theGui);
pack();
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}
}
class Test extends JPanel {
private static final int PADDING = 3;
private JLabel label;
private JTextArea textarea;
private JScrollPane scrollpane;
private GridBagConstraints gbc;
public Test() {
super(new GridBagLayout());
label = new JLabel("Fill this text box with text so the scrollbars appear, then move the window to 2nd monitor:");
textarea = new JTextArea(10, 40);
scrollpane = new JScrollPane(textarea);
gbc = new GridBagConstraints();
gbc.gridheight = 1;
gbc.insets = new Insets(PADDING, PADDING, PADDING, PADDING);
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 4;
gbc.gridx = 0;
gbc.gridy = 0;
add(label, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(scrollpane, gbc);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,当JScrollPane填充文本时,它移动到另一个监视器时变得非常小.如果其中没有文字,它似乎保持正确的大小.
我的问题是,当我将窗口移动到第二台显示器时,如何防止JScrollPane变得非常小?
GridBagLayoutminimumSize当组件的可用空间小于组件时,将恢复为components 属性preferredSize.
您可以通过使用替代此GridBagConstraints,weightx/ weighty属性,例如.
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
add(scrollpane, gbc);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
258 次 |
| 最近记录: |