【问题标题】:How to resize a JTextPane component binded in a JScrollPane如何调整绑定在 JScrollPane 中的 JTextPane 组件的大小
【发布时间】:2014-05-21 14:45:15
【问题描述】:

在带有 GridBagLayout 的 JPanel 中,我创建了与 JscrollPane 组件绑定的 JTextPane 组件列表。不幸的是,组件的大小似乎是固定的:当我减小 JFrame 大小时,我想强制组件动态地使其大小适合 JFrame。 例如,当主窗口大小减小到 20 像素时,我希望组件也将其大小更改为 20 像素。

这是我的代码:

import java.awt.*;
import javax.swing.*;

public class ScrollbarTest {

private JFrame frame;
private JPanel panel;
private JScrollPane [] scrollpane = new JScrollPane[4];
private JTextArea [] textPane = new JTextArea[4];
private JScrollPane scrollbar;


ScrollbarTest() {
    //initialize jtextarea
    for(int i=0;i<textPane.length;i++) {            
        textPane[i]=new JTextArea();            
        textPane[i].setText("xxxx xxxx xxxx xxxx xxxx xxxx xxxx | ");
        scrollpane[i] = new JScrollPane(textPane[i]); 
        scrollpane[i].setMinimumSize(new Dimension(100,100));
    }

    panel     = new JPanel();
    frame     = new JFrame();

    createList(panel);
    scrollbar = new JScrollPane(panel);

    frame.add(scrollbar);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(new Dimension(400,400));          
} //constructor

//method to easily add components to GridBags
static void addComponent(JPanel panel, 
                         GridBagLayout gbl, 
                         Component c,
                         int x, int y,
                         int width, int height,
                         double weightx, double weighty) {

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbl.setConstraints( c, gbc );
    panel.add( c ); 
}//addComponent

public void createList(JPanel panel) {
    //setting layout: "GridBagLayout"
    GridBagLayout gbl = new GridBagLayout();        
    panel.setLayout(gbl);                        

    //put created components to the gridbags of gridbaglayout
    //                                            x  y  w  h  wx wy
    addComponent( panel, gbl, scrollpane[0]     , 0, 1, 1, 1, 1 ,1  );
    addComponent( panel, gbl, scrollpane[1]     , 0, 2, 1, 1, 1 ,1  );
    addComponent( panel, gbl, scrollpane[2]     , 1, 1, 1, 1, 1 ,1  );
    addComponent( panel, gbl, scrollpane[3]     , 1, 2, 1, 1, 1 ,1  );
} //createList

public static void main (String[] args) {
    new ScrollbarTest();        
} //main

} //end of class

如何动态调整组件的大小?

非常感谢!

【问题讨论】:

    标签: java resize jscrollpane jtextpane


    【解决方案1】:

    使用GridBagLayout 时,您必须始终关注子组件的preferredSize。添加类似的东西

    textPane[i].setPreferredSize(new Dimension(10, 15));
    

    到你的构造函数中的for循环。

    另外,您将 AWT 与 Swing 混合在一起 - 使用更轻量级的 JComponent 而不是 Component

    【讨论】:

      【解决方案2】:

      将所有组件添加到您的 JFrame 后,您应该运行以下命令:

      frame.pack();
      

      我通常不会遇到这类问题,因为我通常使用 NetBeans 接口生成Aplets。在 NetBeans 中,您只需使用图形界面构建组件,它就会自动生成所有代码。 试试看! :-)

      【讨论】:

      • 感谢您的建议。首先,我想获得基础知识以了解事物的工作原理……但是,我希望在更改主窗口时动态更改组件大小。例如,当主窗口大小减小到 20 像素时,我希望组件也将其大小更改为 20 像素。
      • 我知道你的意思。我试着像你现在一样学习,但是当我开始做更复杂的事情时,我开始使用 NetBeans 变得如此困难。 NetBeans 并不完美,因为它生成的大部分代码都不需要,但它仍然非常有用。随着时间的推移,我开始通过查看 NetBeans 自动代码来了解其工作原理。
      • pack() 只是将框架的边界设置为计算出的内部窗格的首选边界。它应该如何帮助调整内部组件的大小?
      • pack() 不是我的问题的解决方案:正如 Yetti 先生所说,它不会调整内部组件的大小
      猜你喜欢
      • 1970-01-01
      • 2017-09-08
      • 2015-02-20
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 2016-04-04
      相关资源
      最近更新 更多