【问题标题】:GridBagLayout JLabels & JTextAreas organization within JPanelJPanel 中的 GridBagLayout JLabels 和 JTextAreas 组织
【发布时间】:2014-10-28 10:16:48
【问题描述】:

我正在尝试在一个面板中创建 4 列。

                    JLabel(Title)  
     JLabel                            JLabel
JLabel    JTextArea               JLabel    JTextArea
...
...
...
                      JButton 

它几乎是一个输入数据的面板。标签将类似于“速度”,然后您在旁边的文本区域中输入一个数字。不过,我遇到了 gridbaglayout 的问题。标题很大,所以看起来像这样。使用 GridBagLayout ,标题位于 (1,0) 但看起来它太大了,当我放置 JLabel1 (0,1) 和 JLabel2 (2,0) 时,它们的间距太大了,因为标题似乎已经占用了一大块。

                       JLabel(Title..............)     

               JLabel1                            JLabel2
               ...
               ...
               ...
                               JButton

我希望它更像

            JLabel(Title..........................................)     

                     JLabel                         JLabel
                JLabel  JTextArea              JLabel  JTextArea
            ...
            ...
            ...
                                   JButton

如果你想运行它的代码:

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Example {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                new Example();
            }
        });
    }

    JFrame go = new JFrame("Example");
    JPanel panel = new JPanel();
    JButton Button = new JButton("Button");
    GridBagLayout Grid = new GridBagLayout();
    JLabel Title = new JLabel("LARGEE TITLEE");
    JLabel Label1 = new JLabel("Label 1");
    JLabel Label2 = new JLabel("Label 2");

    public Example() {
    panel.setLayout(Grid);

    GridBagConstraints c = new GridBagConstraints();
    Title.setFont(new Font("Serif", Font.BOLD, 60));

    c.insets = new Insets(10,10,10,10);
    c.gridy = 0; c.gridx = 1;
    panel.add(Title, c);
    c.gridy = 1; c.gridx = 0;
    panel.add(Label1 , c);
    c.gridx = 2;
    panel.add(Label2, c);
    c.gridy = 2; c.gridx = 1;
    panel.add(Button, c);
    go.add(panel);
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(750, 750);
    go.setVisible(true);
    }
}

【问题讨论】:

  • 为了尽快获得更好的帮助,请发布Minimal Complete Example 来证明问题。很多时候,仅仅创建示例就会暴露问题。
  • @splunebob 我很确定我找到了问题所在。我只是不知道如何解决它。 :)

标签: java layout grid jlabel jtextarea


【解决方案1】:

将屏幕分成几部分,并指定每个组件的宽度(gridwidth),以及gridx和gridy,这样它们就会相应地放置。

我编写的示例输出如下所示:

代码:

public class Example extends JPanel {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }

    JFrame go = new JFrame("Example");
    JPanel panel = new JPanel();
    GridBagLayout Grid = new GridBagLayout();
    JLabel Title = new JLabel("LARGE TITLE", SwingConstants.CENTER);
    JLabel Label1 = new JLabel("Label 1", SwingConstants.CENTER);
    JLabel Label2 = new JLabel("Label 2", SwingConstants.CENTER);


    JLabel anotherLabel1 = new JLabel("Another Label 1", SwingConstants.CENTER);
    JLabel anotherLabel2 = new JLabel("Another Label 2", SwingConstants.CENTER);

    JTextArea textArea1 = new JTextArea("TextArea 1");
    JTextArea textArea2 = new JTextArea("TextArea 2");

    public Example() {
        panel.setLayout(Grid);
        panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

        Title.setFont(new Font("Serif", Font.BOLD, 60));

        JButton button;
        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 40; //increase height of the title
        c.weightx = 0.5;
        c.gridwidth = 4;
        c.gridx = 0;
        c.gridy = 0;
        panel.add(Title, c);

        c.ipady = 0;
        c.gridwidth = 2;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 1;
        panel.add(Label1, c);

        c.weightx = 0.5;
        c.gridwidth = 2;
        c.gridx = 2;
        c.gridy = 1;
        panel.add(Label2, c);

        c.ipady = 0;
        c.gridwidth = 1;
        c.weightx = 0.25;
        c.gridx = 0;
        c.gridy = 2;
        panel.add(anotherLabel1, c);

        c.weightx = 0.25;
        c.gridx = 1;
        c.gridy = 2;
        panel.add(textArea1, c);

        c.weightx = 0.25;
        c.gridx = 2;
        c.gridy = 2;
        panel.add(anotherLabel2, c);

        c.weightx = 0.25;
        c.gridx = 3;
        c.gridy = 2;
        panel.add(textArea2, c);

        button = new JButton("JButton");
        c.ipady = 0;
        c.weighty = 1.0;
        c.insets = new Insets(10, 0, 0, 0); 
        c.gridx = 1; 
        c.gridwidth = 2; 
        c.gridy = 3;
        panel.add(button, c);

        go.add(panel);
        go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        go.pack();
        go.setSize(750, 300);
        go.setVisible(true);
    }

}

相关文档:

【讨论】:

  • 你为什么要panel.setLayout(Gird),然后在panel.setLayout(new GridBagLayout() )之后不久。第二个布局不会覆盖第一个吗?
【解决方案2】:

如果我做对了,您需要将标题标签设置为全宽,试试这个:

c.fill = GridBagConstraints.HORIZONTAL; 
c.gridwidth = 4; //where 4 is the numbers of columns
c.gridx=0; // set position at (0,0) because now is full width
panel.add(title, c);

【讨论】:

  • 我试过了,但现在无论我的标签在什么 x 值上,它们都聚集在最左边,就在标题开头的正下方。
猜你喜欢
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多