代码之家  ›  专栏  ›  技术社区  ›  Oscar F

JPanel内的GridBagLayout JLabels和JTextAreas组织

  •  1
  • Oscar F  · 技术社区  · 10 年前

    我试图在一个面板中制作4列。

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

    它几乎是一个输入数据的面板。标签将类似于“速度”,然后你在旁边的文本区域中键入一个数字。不过,我对网格显示有问题。标题很大,所以看起来像这样。使用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 回复  |  直到 10 年前
        1
  •  2
  •   Infinite Recursion Van Phong Pham    10 年前

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

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

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

    相关文件:

        2
  •  1
  •   frankfg    10 年前

    如果我做对了,您需要的是设置标题标签的全宽,请尝试以下操作:

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