在Swing GUI中定位组件 [英] Positioning components in Swing GUIs

查看:166
本文介绍了在Swing GUI中定位组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对定位组件以及文本字段和文本区域(Java Swing)的一些问题有一些疑问。非常感谢任何帮助。



现在我正在尝试将两个文本字段放在一起,每个文本字段上面都有一个不同的标签,用于描述该文本字段的作用。为实现这一点,我将它们放在 GridLayout(2,2)中。


  1. 这是最好的方法吗?这是我知道直接在另一个组件上标签的唯一方法。有没有更好的办法?如果一个按钮上方只有一个标签怎么办?通过 GridLayout(2,1)来定位它是否明智?我有视觉障碍,所以我不认为定位按钮只是它们的像素位置是一个选项,除非有一种简单的方法将组件的相对像素数放置到另一个组件。


  2. 这引出了我的下一个问题。
    使用与上面相同的UI但在其下居中的另一个组件(按钮)的最佳方法是什么。本质上,UI应该由两个命名文本字段组成,其下有一个计算按钮。我这样做的方法是将上面的组件放在一个面板中,然后将这个加上计算按钮添加到一个带有 GridLayout(2,1)的周围面板。问题是按钮变得和它上面的面板一样大(我假设)。如何调整此选项仍然可以在文本字段/标签面板下完美对齐按钮?与文本区域上方的标签类似。标签应该很小,但文本区域的空间要大。


  3. (文本字段):
    再次参考上面的UI,如果用户在第一个文本字段中键入了多个字符,那么这些字母是否会覆盖右侧的文本字段?如果是这样我该如何防止这种情况?


  4. 如果我将文字附加到文本区域并且文本已经满了,它会自动允许用户滚动吗?如果不是什么是使文本区域可滚动的简单方法?


  5. 现在我没有设置文本区域的大小。它是否随着我添加文字而增长?它是否有字符数的默认大小?



解决方案

那里许多布局管理者可能能够为您提供所需的东西。




  • 值得一读。


    I have some questions on positioning components and some questions on text fields and text areas (Java Swing). Any help is greatly appreciated.

    Right now I am trying to have two text fields beside each other with a different label above each describing what that text field does. To achieve this I have placed them in a GridLayout(2, 2).

    1. Is this the best way? It is the only way I know to have a label directly over another component. Is there a better way? What about if there is just one label above one button. Is it sensible to position this through a GridLayout(2, 1)? I am visually impaired so I do not think positioning buttons just by their pixel position is an option unless there is a simple way to place components at a relative number of pixels to another component.

    2. That leads me to my next question. What is the best way to have the same UI as above but with another component (button) centered under it. Essentially the UI should compose of two Named text fields with a calculate button under. The way I did this is by putting the above components in a panel, and adding that plus the calculate button to a surrounding panel with a GridLayout(2, 1). The problem is that the button becomes as big as the panel above it (I'm assuming). How can I adjust this and still have the button perfectly aligned under the panel of text fields/labels? Similarly with labels above text areas. The label should be small but have a larger space for the text area under.

    3. (text field): Again referring to the UI above, if the user types many characters into the first text field, will the letters go over the text field on the right? If so how can I prevent this?

    4. If I append text to a text area and it is already full, will it automatically allow the user to scroll? If not what is a simple way to make the text area scrollable?

    5. Right now I am not setting a size of the text area. Does it just grow as I add text? Does it have a default size as in number of characters?

    解决方案

    There are a number of layout managers that might be capable of providing you with what you need.

    For, GridBagLayout would be my choice (I'm biased, as I've been using this layout manager for the past 12 years ;))

    public class TestLayout17 {
    
        public static void main(String[] args) {
            new TestLayout17();
        }
    
        public TestLayout17() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.insets = new Insets(2, 2, 2, 2);
    
                add(new JLabel("Label 1"), gbc);
                gbc.gridx++;
                add(new JLabel("Label 2"), gbc);
    
                gbc.gridx = 0;
                gbc.gridy++;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                add(new JTextField(10), gbc);
                gbc.gridx++;
                add(new JTextField(10), gbc);
    
                gbc.gridx = 0;
                gbc.gridy++;
                gbc.fill = GridBagConstraints.NONE;
                gbc.gridwidth = 2;
                add(new JButton("Click"), gbc);
    
            }
    
        }
    
    }
    

    I also agree with Eng.Fouad's suggestion of using compound containers to make your life easier in the long run

    You might find Laying Out Components Within a Container a worth while read.

    这篇关于在Swing GUI中定位组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆