Java使用GridLayout的问题

发布于 2024-11-07 09:09:43 字数 1343 浏览 0 评论 0原文

我想要一个 3x3 网格布局,结构如下:

 _ _ _ _ _ _ _ _ _ _ _ _
|        Label         |
| Button Button Button |
|       TextArea       |
|_ _ _ _ _ _ _ _ _ _ _ |

但布局已损坏。

    public static void main(String[] args){
    JFrame frame = new JFrame("title");
    frame.setSize(400,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GridLayout layout = new GridLayout(3,3);

    JPanel pan0 = new JPanel();
    JPanel pan1 = new JPanel();
    JPanel pan2 = new JPanel();

    pan0.setLayout(layout);
    GridBagConstraints c = new GridBagConstraints();

    JLabel title = new JLabel("title");
    title.setFont(new Font("Serif", Font.BOLD, 40));
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    pan0.add(title, c);

    JButton b1 = new JButton("button1");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b1, c);

    JButton b2 = new JButton("button2");
    c.gridx = 1;
    c.gridy = 2;
    c.gridwidth = 1;
    pan0.add(b2, c);

    JButton b3 = new JButton("button3");
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 1;
    pan0.add(b3, c);    

    TextArea text1 = new TextArea(15,40);
    c.gridx = 2;
    c.gridy = 0;
    c.gridwidth = 3;
    pan0.add(text1, c);

    frame.add(pan0);
    frame.pack();

  }
}

I want to have a 3x3 grid layout structured like this:

 _ _ _ _ _ _ _ _ _ _ _ _
|        Label         |
| Button Button Button |
|       TextArea       |
|_ _ _ _ _ _ _ _ _ _ _ |

But the layout is broken.

    public static void main(String[] args){
    JFrame frame = new JFrame("title");
    frame.setSize(400,500);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GridLayout layout = new GridLayout(3,3);

    JPanel pan0 = new JPanel();
    JPanel pan1 = new JPanel();
    JPanel pan2 = new JPanel();

    pan0.setLayout(layout);
    GridBagConstraints c = new GridBagConstraints();

    JLabel title = new JLabel("title");
    title.setFont(new Font("Serif", Font.BOLD, 40));
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    pan0.add(title, c);

    JButton b1 = new JButton("button1");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b1, c);

    JButton b2 = new JButton("button2");
    c.gridx = 1;
    c.gridy = 2;
    c.gridwidth = 1;
    pan0.add(b2, c);

    JButton b3 = new JButton("button3");
    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 1;
    pan0.add(b3, c);    

    TextArea text1 = new TextArea(15,40);
    c.gridx = 2;
    c.gridy = 0;
    c.gridwidth = 3;
    pan0.add(text1, c);

    frame.add(pan0);
    frame.pack();

  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

假情假意假温柔 2024-11-14 09:09:43

您正在将 GridBagConstraintGridLayout 一起使用,

而不是使用位于同一包中的 GridBagLayout

you are using a GridBagConstraint with a GridLayout

instead use a GridBagLayoutit's in the same pakage

陪我终i 2024-11-14 09:09:43

为了添加棘轮的答案,你还有一些网格位置不正确。试试这个

    GridBagLayout layout = new GridBagLayout();

    JPanel pan0 = new JPanel();
    JPanel pan1 = new JPanel();
    JPanel pan2 = new JPanel();

    pan0.setLayout(layout);
    GridBagConstraints c = new GridBagConstraints();

    JLabel title = new JLabel("title");
    title.setFont(new Font("Serif", Font.BOLD, 40));
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.CENTER;
    pan0.add(title, c);

    JButton b1 = new JButton("button1");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b1, c);

    JButton b2 = new JButton("button2");
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b2, c);

    JButton b3 = new JButton("button3");
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b3, c);    

    TextArea text1 = new TextArea(15,40);
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.CENTER;
    pan0.add(text1, c);

To add to ratchet's answer, you also have some grid positions incorrect. Try this

    GridBagLayout layout = new GridBagLayout();

    JPanel pan0 = new JPanel();
    JPanel pan1 = new JPanel();
    JPanel pan2 = new JPanel();

    pan0.setLayout(layout);
    GridBagConstraints c = new GridBagConstraints();

    JLabel title = new JLabel("title");
    title.setFont(new Font("Serif", Font.BOLD, 40));
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.CENTER;
    pan0.add(title, c);

    JButton b1 = new JButton("button1");
    c.gridx = 0;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b1, c);

    JButton b2 = new JButton("button2");
    c.gridx = 1;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b2, c);

    JButton b3 = new JButton("button3");
    c.gridx = 2;
    c.gridy = 1;
    c.gridwidth = 1;
    pan0.add(b3, c);    

    TextArea text1 = new TextArea(15,40);
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 3;
    c.fill = GridBagConstraints.CENTER;
    pan0.add(text1, c);
夏末 2024-11-14 09:09:43

您将 GridLayout 与 GridBagLayout 混淆了,但我自己会使用 BoxLayout 进行垂直定位,然后可能使用 GridLayout 作为按钮。例如,

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class Foo003 {
   private static final int BTN_COUNT = 3;
   private static final int VERT_GAP = 10;
   private static final int EB_GAP = 5;
   private static final float TITLE_SIZE = 36f;
   private static final String TITLE_TEXT = "This is my Title";

   private static void createAndShowUI() {

      JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
      titleLabel.setFont(titleLabel.getFont().deriveFont(TITLE_SIZE));
      JPanel titlePanel = new JPanel();
      titlePanel.add(titleLabel);

      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      for (int i = 0; i < BTN_COUNT; i++) {
         JButton btn = new JButton("Button " + (i + 1));
         buttonPanel.add(btn);
      }

      JTextArea textArea = new JTextArea(20, 30);

      JPanel mainPanel = new JPanel();
      mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
      mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
      mainPanel.add(titlePanel);
      mainPanel.add(Box.createVerticalStrut(VERT_GAP));
      mainPanel.add(buttonPanel);
      mainPanel.add(Box.createVerticalStrut(VERT_GAP));
      mainPanel.add(new JScrollPane(textArea));

      JFrame frame = new JFrame("Foo003");
      frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

You're confusing GridLayout with GridBagLayout, but I'd use a BoxLayout for the vertical positioning myself, and then perhaps a GridLayout for the buttons. For e.g.,

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;

public class Foo003 {
   private static final int BTN_COUNT = 3;
   private static final int VERT_GAP = 10;
   private static final int EB_GAP = 5;
   private static final float TITLE_SIZE = 36f;
   private static final String TITLE_TEXT = "This is my Title";

   private static void createAndShowUI() {

      JLabel titleLabel = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
      titleLabel.setFont(titleLabel.getFont().deriveFont(TITLE_SIZE));
      JPanel titlePanel = new JPanel();
      titlePanel.add(titleLabel);

      JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      for (int i = 0; i < BTN_COUNT; i++) {
         JButton btn = new JButton("Button " + (i + 1));
         buttonPanel.add(btn);
      }

      JTextArea textArea = new JTextArea(20, 30);

      JPanel mainPanel = new JPanel();
      mainPanel.setBorder(BorderFactory.createEmptyBorder(EB_GAP, EB_GAP, EB_GAP, EB_GAP));
      mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
      mainPanel.add(titlePanel);
      mainPanel.add(Box.createVerticalStrut(VERT_GAP));
      mainPanel.add(buttonPanel);
      mainPanel.add(Box.createVerticalStrut(VERT_GAP));
      mainPanel.add(new JScrollPane(textArea));

      JFrame frame = new JFrame("Foo003");
      frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文