【问题标题】:How can I implement Flowlayout in java to make it so that my GridLayout can have a different sized button?如何在 java 中实现 Flowlayout 以使我的 GridLayout 可以具有不同大小的按钮?
【发布时间】:2016-11-07 06:53:15
【问题描述】:

我使用 GridLayout 方法在 java 中编写了一个计算器。我没有考虑这样一个事实,即无法设置不同大小的按钮。因此,我试图找到一种解决方法,因为我无法弄清楚如何在我的代码中实现 GridBagLayout。

目前我的代码结果如下所示: My current JFrame Design

但是,我试图让它看起来像这样: How I want the design to look

   public static void main(String[] args){

   JFrame frame = new JFrame("Calculator");
   Container content = frame.getContentPane();
   ActionListener AL = new Calculator();
   content.setLayout((new BorderLayout()));

   JPanel panel1 = new JPanel(new BorderLayout());
   panel1.add(new JLabel("CSC 20 Lab 08", JLabel.CENTER), BorderLayout.NORTH);

   text = new JTextField("0.");
   text.addActionListener(AL);

   panel1.add( text, BorderLayout.SOUTH);
   text.setHorizontalAlignment(JTextField.RIGHT);
   content.add(panel1, BorderLayout.NORTH);
   JPanel panel2 = new JPanel(new GridLayout(4, 5));

   for (int i=1; i < 8; i = i + 3){

       ...adding numerical button in panel2...

       ...adding more buttons in panel2...
   }

   JPanel panel2b = new JPanel(new GridLayout(1, 2));

   clearButton = new JButton("C");
   panel2b.add(clearButton);
   gridbag.setConstraints(clearButton, c);

  ...add "clear" functionality...

   panel2.add(panel2b);

   ...adding more buttons in panel2...

   content.add(panel2, BorderLayout.CENTER); 

   JPanel panel3 = new JPanel(new GridLayout(1, 3));
   equalButton = new JButton("=");
   panel3.add(equalButton);
   equalButton.addActionListener(AL);

   content.add(panel3, BorderLayout.SOUTH);

   ... more code

我怎样才能最轻松地实现它来更改“C”按钮的大小?

【问题讨论】:

  • 改用GridBagLayout。它允许一个单元格跨越多行或多列。 “我无法弄清楚如何在我的代码中实现 GridBagLayout。” 好吧.. 最好准确描述问题所在,因为它是正确的布局。
  • 这是一个使用 GridBagLayout 作为计算器类型的示例,您可以将其作为起点:stackoverflow.com/a/39235219/2180785

标签: java swing layout-manager grid-layout flowlayout


【解决方案1】:

这是您的案例的代码 sn-p。说明以 cmets 为单位。

// Creating a panel with Grid**Bag**Layout
final JPanel pane = new JPanel(new GridBagLayout());

// Setup constraints to future use
final GridBagConstraints c = new GridBagConstraints();

// Let columns to expand horizontally,
// while keeping the same width
c.weightx = 1;

// The same for rows
c.weighty = 1;

// Let the buttons to occupy entire cells
c.fill = GridBagConstraints.BOTH;

c.gridy = 0; // Starting the first row
pane.add(new JButton("1"), c);
pane.add(new JButton("2"), c);
pane.add(new JButton("3"), c);
pane.add(new JButton("+"), c);

c.gridy++; // Switching to next row
pane.add(new JButton("4"), c);
pane.add(new JButton("5"), c);
pane.add(new JButton("6"), c);
pane.add(new JButton("-"), c);

c.gridy++;
pane.add(new JButton("7"), c);
pane.add(new JButton("8"), c);
pane.add(new JButton("9"), c);
pane.add(new JButton("*"), c);

c.gridy++;
// Let the "C" button have double width
c.gridwidth = 2; 
pane.add(new JButton("C"), c);
// Resets to default width for the following buttons
c.gridwidth = 1;
pane.add(new JButton("0"), c);
pane.add(new JButton("/"), c);

c.gridy++;
// Making "=" button have quadruple width
c.gridwidth = 4;
pane.add(new JButton("="), c);

更多信息请参见 Java 教程的 How to Use GridBagLayout 部分。

【讨论】:

    【解决方案2】:

    我建议不要使用GridLayout,因为它过于简单而且确实 对于任何实际布局都没有用处。 GridBagLayout 已过时、设计不良、过于复杂且不可移植。 如今,我们只能在GroupLayoutMigLayoutFormLayout 之间进行选择。

    我使用MigLayout 创建了您的布局。是第三方布局管理器,需要下载对应的JAR。

    package com.zetcode;
    
    import java.awt.EventQueue;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import net.miginfocom.swing.MigLayout;
    
    public class MigLayoutCalculatorEx extends JFrame {
    
        public MigLayoutCalculatorEx() {
    
            initUI();
        }
    
        private void initUI() {
    
            setLayout(new MigLayout("fill, gap 1lp"));
    
            add(new JButton("1"), "grow");
            add(new JButton("2"), "grow");
            add(new JButton("3"), "grow");
            add(new JButton("+"), "grow, wrap");
            add(new JButton("4"), "grow");
            add(new JButton("5"), "grow");
            add(new JButton("6"), "grow");
            add(new JButton("-"), "grow, wrap");
            add(new JButton("7"), "grow");
            add(new JButton("8"), "grow");
            add(new JButton("9"), "grow");
            add(new JButton("*"), "grow, wrap");
            add(new JButton("C"), "grow, spanx 2");
            add(new JButton("0"), "grow");
            add(new JButton("/"), "grow, wrap");
            add(new JButton("="), "grow, spanx");
    
            pack();
    
            setTitle("MigLayout example");
            setLocationRelativeTo(null);        
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
    
            EventQueue.invokeLater(() -> {
                MigLayoutCalculatorEx ex = new MigLayoutCalculatorEx();
                ex.setVisible(true);
            });
        }
    } 
    

    MigLayout 是一个基于网格的布局管理器,因此创建请求的布局非常容易。

    这是截图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多