【问题标题】:java - swing - components not listening to boundsjava - swing - 组件不听边界
【发布时间】:2014-10-17 11:13:45
【问题描述】:

我正在尝试创建一个框架,当我添加一些组件时,它们不会听我给它们的尺寸或位置 - 每当我调整框架的大小时,组件就会粘在一起,彼此靠在一起。另外,我有一个可滚动的文本区域,它获取写入其中的文本的长度和宽度。另外,如果我不调整框架的大小,组件就不会显示。

我的代码:

public static void main(String[] args){
    new Main();
}
private void loadLabel(){
    label.setBounds(0,0,269,20);
    //Setting the icon, not relevant to the code.
    panel.add(label);
}
private void loadInput(){
    input.setBounds(0,20,300,60);
    JScrollPane scroll = new JScrollPane (input);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setVisible(true);
    scroll.setBounds(50,20,300,60);
    panel.add(scroll);
}

private JPanel panel = new JPanel();
private JLabel label = new JLabel();
private JTextArea input = new JTextArea("Enter message                                                         ");
public Main() {
    super("Frame");
    setLocationRelativeTo(null);
    setSize(300, 400);
    setContentPane(panel);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    loadLabel();
    loadInput();

}

提前致谢!

【问题讨论】:

  • 如果你使用布局,你的问题会自己解决
  • 您的主要和唯一问题是您使用setBounds。相反,学习如何使用 LayoutManager,您的所有问题都会立即消失。一劳永逸地,知道你永远不应该使用setBounds/setSize/setLocation。如果您想这样做,只需使用适当的LayoutManager
  • 一些信息给LayoutManager
  • 1) 为了尽快获得更好的帮助,请发布MCVE(最小、完整、可验证的示例)。 2) 提供 ASCII 艺术或简单的图形,说明 GUI 应如何以默认大小显示,并且(如果可调整大小)具有额外的宽度/高度。 ..
  • .. 3) Swing GUI 可能必须在不同的平台上工作,使用不同的 PLAF,在不同的屏幕尺寸和分辨率上使用不同的字体大小默认设置。因此,它们不利于组件的精确放置。而是使用布局管理器,或 combinations of layout managers 以及 layout padding and borders 用于空白。

标签: java swing components layout-manager


【解决方案1】:

这样写

加载标签();
加载输入();

setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

加载内容然后使其可见

【讨论】:

    【解决方案2】:

    您不应该使用.setBounds(,,,) 安排您的组件,而是安排您的 使用布局 (http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) 的组件。 另外,您还没有将标签设置为文本或图标,因此很难正确查看这些组件。在这里,我使用BoxLayout 垂直管理您的组件并将它们放在框架的EAST 一侧,方法是将setContentPane(panel); 替换为getContentPane().add(panel,BorderLayout.EAST);,以帮助我们正确查看您的组件。

    import java.awt.*;
    import javax.swing.*; 
    
    public class Main extends JFrame {
       public static void main(String[] args){
        new Main();
    }
    private void loadLabel(){
        label.setBounds(0,0,269,20);
        //Setting the icon, not relevant to the code.
        panel.add(label);
    }
    private void loadInput(){
        input.setBounds(0,20,300,60);
        JScrollPane scroll = new JScrollPane (input);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setVisible(true);
        scroll.setBounds(50,20,300,60);
        panel.add(scroll);
    }
    
    private JPanel panel = new JPanel();
    private JLabel label = new JLabel("Your Label");
    private JTextArea input = new JTextArea("Enter message                                                         ");
    public Main() {
        super("Frame");
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    
        setLocationRelativeTo(null);
        setSize(300, 400);
        getContentPane().add(panel,BorderLayout.EAST);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loadLabel();
        loadInput();
    
    }
     }
    

    【讨论】:

      猜你喜欢
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多