微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java,网格包布局如何将组件正确添加到其他组件

如何解决Java,网格包布局如何将组件正确添加到其他组件

我正在学习 JSwing,我发现了 GridBagLayout。

我正在尝试创建一个简单的计算器,我通过添加多个 JPanel 设置每个 preferredSize 来做到这一点,但是当我调整窗口框架的大小时,面板也不会调整大小。 然后我发现了 GridBagLayout。

但这就是我得到的:Wrong calculator with GridBagLayout

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

public class Calc extends JFrame {

private final int  WIDTH = 300;
private final int  HEIGHT = 450;

public Calc(){

    setSize(WIDTH,HEIGHT);

    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());




    mainPanel.add(createButtons(),BorderLayout.soUTH);


    add(mainPanel);


}

private  JPanel createButtons(){

    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();

    panel.setLayout(layout);


    GridBagConstraints g = new GridBagConstraints();

    g.gridx = 0;
    g.gridy = 0;

    for(int i = 0; i < 9; i++){

        panel.add(new JButton(""+i),g);
        g.gridx++;
        if(g.gridx == 3) {
            g.gridx = 0;
            g.gridy++;
        }

    }


    return panel;



}


public static void main(String... args){

    Calc calc = new Calc();
    calc.setVisible(true);

}

}

应该是这样的: Right calculator

我试过:

  • 设置锚点...但不起作用,
  • 创建多个 JPanel(一个带有 GridLayout)但不起作用

如果你不想偷代码,那也没关系..但是我应该从哪里开始呢?

编辑: 我弄清楚如何排列按钮......但我无法设置标题以填充所有 x 轴: 代码

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

public class ButtonPanel extends JPanel {

JPanel top;
JPanel left;
JPanel right;


private class CButton extends JButton{

    private Operation operation;

    public CButton(){

    }



}

public ButtonPanel(){

    initComponent();
    initLayout();
}

private void initLayout() {

    GridBagLayout layout = new GridBagLayout();
    this.setLayout(layout);

    layout.columnWeights = new double[] {3,1};
    layout.rowWeights = new double[] {1,1};

    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;

    c.weightx = 5;
    this.add(top,c);
    c.gridy++;
    c.weighty=1;
    this.add(left,c);
    c.gridx++;
    this.add(right,c);




}

private void initComponent() {

    top = new JPanel();

    top.setLayout(new GridLayout(1,3));

    for(int i = 0; i < 3; i++){
        top.add(new JButton("bbb"));
    }



    left = new JPanel();

    left.setLayout(new GridLayout(3,3));
    for(int i = 0; i < 9; i++){

        left.add(new JButton(""+i));

    }

    right = new JPanel();
    right.setLayout(new GridLayout(3,1));
    for(int i = 0; i < 3; i++){
        JButton btn = new JButton("aa");
        right.add(btn);

    }

}

public static void main(String[] args) {
    JFrame frame = new JFrame("test");
    frame.setLayout(new BorderLayout());
    frame.add(new ButtonPanel(),BorderLayout.soUTH);
    frame.setSize(300,450);
    frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

}

应该是:Image

解决方法

您可能可以在单个面板中完成所有操作,其中一些按钮跨越多列。

所以我给你一个不同的例子来使用单个 GridBagLayout 布局按钮,在这里你可以将你的按钮排列定义为一个值数组,检查它是否可以成为你项目的一个很好的起点。

package test;

import static test.Calculator.Buttons.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Calculator extends JPanel {
    //Here define all possible buttons with labels
    public enum Buttons {
        PERCENT("%"),CE("CE"),CLEAR("C"),ONE("1"),TWO("2"),THREE("3"),FOUR("4"),FIVE("5"),SIX("6"),SEVEN("7"),EIGHT("8"),NINE("9"),ZERO("0"),ADD("+"),SUB("-"),MULT("x"),DIV("/"),RESULT("="),DECPOINT(".");
        
        protected String text;
        
        Buttons(String txt) {
            this.text=txt;
        }
        
        public String getText() {
            return text;
        }
    };
    
    //This array contains your keypad layout,contiguous repeated elements will span across multiple columns (e.g. ZERO). 
    protected Buttons[][] keyPad = {
        {PERCENT,CE,CLEAR,DIV},{SEVEN,EIGHT,NINE,MULT},{FOUR,FIVE,SIX,ADD},{ONE,TWO,THREE,SUB},{ZERO,ZERO,DECPOINT,RESULT}
    };
    
    Map<JButton,Buttons> sourceMap=new HashMap<>();
    
    ActionListener padListener=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            onKeyPadPressed(sourceMap.get(e.getSource()));
        }
    };
    
    public Calculator() {
        setLayout(new GridBagLayout());
        GridBagConstraints c=new GridBagConstraints();
        c.weightx=1.0;
        c.weighty=1.0;
        c.fill=GridBagConstraints.BOTH;
        
        for (int y=0;y<keyPad.length;y++) {
            for (int x=0;x<keyPad[y].length;) {
                Buttons b=keyPad[y][x];
                if (b==null) {
                    continue;
                }
                JButton btn=new JButton(b.getText());
                c.gridx=x;
                c.gridy=y;

                c.gridwidth=0;
                while(x<keyPad[y].length&&keyPad[y][x]==b) {
                    c.gridwidth++;
                    x++;
                }
                add(btn,c);
                
                sourceMap.put(btn,b);
                btn.addActionListener(padListener);
            }
        }
        
    }
    
    //Callback method,whenever a button is clicked you get the associated enum value here
    protected void onKeyPadPressed(Buttons b) {
        System.out.println("Pressed "+b);
        switch (b) {
//          case ZERO:
//          .... here your logic
        }
    }
    
    public static void main(String[] args) {
        JFrame frame=new JFrame("Calculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Calculator());
        frame.pack();
        frame.setVisible(true);
    }
}

上面的代码产生了这个结果,但是添加/删除按钮和改变布局真的很容易。

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。