簡體   English   中英

無法在GridLayout中設置按鈕的大小

[英]Unable to set the size of my buttons in GridLayout

我正在嘗試使用Java中的GridLayout構建示例應用程序。 但是我無法調整按鈕的大小。 請幫我做。

代碼中有四個網格布局。

我已經使用了setSize(width,height)函數以及setPreferredSize函數。 但是我無法設置按鈕的大小。

這是代碼

import java.awt.*;

import javax.swing.*;
public class Details2 {

    static JButton btn1,btn2;
   public static void main(String[] a) {
      JFrame myFrame = new JFrame("Medical History Form");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      myFrame.setSize(700,700);
      Container myPane = myFrame.getContentPane();
      myPane.setLayout(new GridLayout(2,1));
      myPane.add(getFieldPanel());
      myPane.add(getButtonPanel());
      myPane.setPreferredSize(new Dimension(100,100));
      myFrame.setVisible(true);
   }

   private static JPanel getFieldPanel() {
      JPanel p = new JPanel(new GridLayout(4,2));
      p.setBorder(BorderFactory.createTitledBorder("Details"));
      p.add(new JLabel("Please check in the here"));

      p.add(new JCheckBox("Nothing till now",false));
      p.add(getPanel());

      return p;
   }

   private static JPanel getButtonPanel() {
      GridLayout g =new GridLayout(1,2);
      JPanel p = new JPanel(g);
      btn1 = new JButton("Submit");
      btn2 = new JButton("Reset");

      p.add(btn1).setPreferredSize(new Dimension(100,100));
      p.add(btn2).setPreferredSize(new Dimension(100,100));
      p.setPreferredSize(new Dimension(100,100));
      return p;
   }

   private static JPanel getPanel() {
      JPanel p = new JPanel(new GridLayout(5,2));
      p.add(new JCheckBox("A",false));
      p.add(new JCheckBox("B",false));
      p.add(new JCheckBox("C",false));
      p.add(new JCheckBox("D",false));
      p.add(new JCheckBox("E",false));
      p.add(new JCheckBox("F",false));
      p.add(new JCheckBox("G",false));
      p.add(new JCheckBox("E",false));
      p.add(new JCheckBox("H",false));
      p.add(new JCheckBox("I",false));
      return p;
   }
}

這是代碼的輸出

我相信設置GridLayout(2,2)將覆蓋對面板所做的尺寸更改。 更精確地說,使用GridBagConStraints; 您可以參考以下代碼來理解它。

private JTextField field1 = new JTextField();
private JButton addBtn = new JButton("Save: ");

public void addComponents(Container pane) {
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
// Components
c.gridwidth = 1;
c.weightx = .01;
c.weighty = .2;
c.gridx = 0;
c.gridy = 1;
pane.add(field1, c);

    c.gridwidth = 1;
c.weightx = .01;
c.weighty = .2;
c.gridx = 0;
c.gridy = 1;
pane.add(addBtn, c);
}

public MainView()  {
    //Create and set up the window.
    JFrame frame = new JFrame("NAME");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Set up the content pane.
    addComponents(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setSize(400, 125);
    frame.setLocation(400, 300);
}

如果要控制組件的大小,請不要使用布局。 做這樣的事情:

 private static JPanel getButtonPanel() {

  JPanel p = new JPanel();
  p.setLayout(null);

  btn1 = new JButton("Submit");
  btn2 = new JButton("Reset");
  btn1.setBounds(x, y, width, height);
  btn2.setBounds(x, y, width, height);
  p.add(btn1);
  p.add(btn2);
  return p;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM