无法在GridLayout

时间:2015-09-15 17:17:50

标签: java button grid-layout

我正在尝试使用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;
   }
}

Here is the output of the code

2 个答案:

答案 0 :(得分:0)

我相信设置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);
}

答案 1 :(得分:0)

如果要控制组件的大小,请不要使用布局。做这样的事情:

 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;