GridBagLayout JLabels& JPAnel中的JTextAreas组织

时间:2014-09-03 19:35:12

标签: java layout grid jlabel jtextarea

我试图在面板中制作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);
    }
}

2 个答案:

答案 0 :(得分:2)

将屏幕划分为多个部分,并指定每个组件的宽度(网格宽度),以及gridx和gridy,以便相应地放置它们。

我写的样本的输出如下:

enter image description here

代码:

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);
    }

}

相关文档:

答案 1 :(得分:1)

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

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);