Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GridBagLayout JLabels&;JPanel中的JTextAreas组织_Java_Layout_Grid_Jlabel_Jtextarea - Fatal编程技术网

Java GridBagLayout JLabels&;JPanel中的JTextAreas组织

Java GridBagLayout JLabels&;JPanel中的JTextAreas组织,java,layout,grid,jlabel,jtextarea,Java,Layout,Grid,Jlabel,Jtextarea,我试图在一个面板中创建4列 JLabel(Title) JLabel JLabel JLabel JTextArea JLabel JTextArea ... ... ... JButton 它几乎是一个输入数据的面板。标签类似于“速度”,然后在旁边的文本区域中键入一个数字。不过我对gridbaglay

我试图在一个面板中创建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);
    }
}

如果我没有弄错,您需要设置标题标签的全宽,请尝试以下操作:

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

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

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

代码: 相关文件:

为了更快地获得更好的帮助,请发布一篇演示问题的文章。很多时候,只要创建一个例子,就会发现问题所在。@splungebob我很确定我已经找到了问题所在。我只是不知道如何修复它。:)我试过了,但现在不管标签上的x值是什么,它们都会聚集在最左边,标题的正下方。为什么要
panel.setLayout(Gird)
,然后在
panel.setLayout(new gridbagloayout())
之后,第二个布局会覆盖第一个布局吗?
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);
    }

}