按钮在我的Swing计算器中显得很奇怪

时间:2017-04-28 10:50:44

标签: java swing resize layout-manager gridbaglayout

我正在使用Swing构建计算器程序。我正在编写多个类来构建计算器,其中一个包含一个窗格,我在其中放置所有按钮。然后按钮会触发进入屏幕窗格的属性更改,以对屏幕进行更改。

但是,我遇到了问题。按钮不会以统一的方式显示。有些按钮比其他按钮大,看起来像是在“压缩”其他按钮。

以下是我运行应用程序时计算器的显示方式:

Calculator link

以下是按钮窗格的代码示例:

package com.nacher.calc.ui;

import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

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

public class PanelNumbers extends JPanel implements ActionListener, PropertyChangeListener {

/**
 * 
 */
private static final long serialVersionUID = -8338113083603013816L;

private GUIConstraints cons = new GUIConstraints(new int[][] {
        // la linea de enteros: {#, #}, {#, #}, {#, #}, {#,#,#,#}
        // {gridx, gridy}, {gridwidth, gridheight}, {fill, anchor}, {top, left, bottom, right}(insets)
        { 0, 0 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnBck 0
        { 1, 0 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnCE 1
        { 2, 0 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnC 2
        { 3, 0 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnSign  3
        { 4, 0 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 2 }, // btnSqrt 4
        { 0, 1 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnSeven 5
        { 1, 1 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnEight 6
        { 2, 1 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnNine 7
        { 3, 1 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnDiv 8
        { 4, 1 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 2 }, // btnPercent 9
        { 0, 2 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnFour 10
        { 1, 2 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnFive 11
        { 2, 2 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnSix 12
        { 3, 2 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnMult 13
        { 4, 2 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 2 }, // btnFrac 14
        { 0, 3 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnOne 15
        { 1, 3 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnTwo 16
        { 2, 3 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnThree 17
        { 3, 3 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnMinus 18
        { 4, 3 }, { 1, 2 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 2 }, // btnEquals 19
        { 0, 4 }, { 2, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnZero 20
        { 2, 4 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnComma 21
        { 3, 4 }, { 1, 1 }, { GUIConstraints.BOTH, GUIConstraints.CENT }, { 1, 1, 1, 1 }, // btnPlus 22
},
        // La linea de decimales: {#,#}
        // {weightx, weighty}
        new double[][] { { 0.2, 0.2 }, // btnBck
                { 0.1, 0.1 }, // btnCE
                { 0.1, 0.1 }, // btnC
                { 0.1, 0.1 }, // btnSign
                { 0.1, 0.1 }, // btnSqrt
                { 0.2, 0.2 }, // btnSeven
                { 0.1, 0.1 }, // btnEight
                { 0.1, 0.1 }, // btnNine
                { 0.1, 0.1 }, // btnDiv
                { 0.1, 0.1 }, // btnPercent
                { 0.2, 0.2 }, // btnFour
                { 0.1, 0.1 }, // btnFive
                { 0.1, 0.1 }, // btnSix
                { 0.1, 0.1 }, // btnMult
                { 0.1, 0.1 }, // btnFrac
                { 0.2, 0.2 }, // btnOne
                { 0.1, 0.1 }, // btnTwo
                { 0.1, 0.1 }, // btnThree
                { 0.1, 0.1 }, // btnMinus
                { 0.1, 0.1 }, // btnEquals
                { 0.2, 0.2 }, // btnZero
                { 0.1, 0.1 }, // btnComma
                { 0.1, 0.1 }, // btnPlus
        });
private ConstraintsGroup cg = new ConstraintsGroup(cons.getgMain(), cons.getwMain());

// Row One
JButton btnBck;
JButton btnCE;
JButton btnC;
JButton btnSign;
JButton btnSqrt;

// Row Two
JButton btnSeven;
JButton btnEight;
JButton btnNine;
JButton btnDiv;
JButton btnPercent;

// Row Three
JButton btnFour;
JButton btnFive;
JButton btnSix;
JButton btnMult;
JButton btnFrac;

// Row Four
JButton btnOne;
JButton btnTwo;
JButton btnThree;
JButton btnMinus;
JButton btnEquals;

// Row Five
JButton btnZero;
JButton btnComma;
JButton btnPlus;

Font buttonFont = new Font ("Times New Roman", Font.PLAIN, 11);

public PanelNumbers() {
    this.setLayout(new GridBagLayout());

    this.buildComponents();
    this.situateComponents();
}

private void buildComponents() {
    // Row One
    btnBck = new JButton("\u232B");
    //new ImageIcon(PanelNumbers.class.getResource(ImageConstants.BCK_ICON))
    btnBck.setIconTextGap(1);
    btnBck.setActionCommand(Constants.ACTION_COMMAND_BACK);
    btnBck.addActionListener(this);
    btnCE = new JButton("CE");
    btnCE.setFont(buttonFont);
    btnC = new JButton("C");
    btnC.setFont(buttonFont);
    btnSign = new JButton("\u00B1");
    btnSign.setFont(buttonFont);
    btnSqrt = new JButton("\u221A");
    btnSqrt.setFont(buttonFont);

    // Row Two
    btnSeven = new JButton("7");
    btnSeven.setFont(buttonFont);
    btnSeven.setActionCommand(Constants.ACTION_COMMAND_SEVEN);
    btnSeven.addActionListener(this);
    btnEight = new JButton("8");
    btnEight.setFont(buttonFont);
    btnEight.setActionCommand(Constants.ACTION_COMMAND_EIGHT);
    btnEight.addActionListener(this);
    btnNine = new JButton("9");
    btnNine.setFont(buttonFont);
    btnNine.setActionCommand(Constants.ACTION_COMMAND_NINE);
    btnNine.addActionListener(this);
    btnDiv = new JButton("/");
    btnDiv.setFont(buttonFont);
    btnPercent = new JButton("%");
    btnPercent.setFont(buttonFont);

    // Row Three
    btnFour = new JButton("4");
    btnFour.setFont(buttonFont);
    btnFour.setActionCommand(Constants.ACTION_COMMAND_FOUR);
    btnFour.addActionListener(this);
    btnFive = new JButton("5");
    btnFive.setFont(buttonFont);
    btnFive.setActionCommand(Constants.ACTION_COMMAND_FIVE);
    btnFive.addActionListener(this);
    btnSix = new JButton("6");
    btnSix.setFont(buttonFont);
    btnSix.setActionCommand(Constants.ACTION_COMMAND_SIX);
    btnSix.addActionListener(this);
    btnMult = new JButton("*");
    btnMult.setFont(buttonFont);
    btnFrac = new JButton("1/x");
    btnFrac.setFont(buttonFont);

    // Row Four
    btnOne = new JButton("1");
    btnOne.setFont(buttonFont);
    btnOne.setActionCommand(Constants.ACTION_COMMAND_ONE);
    btnOne.addActionListener(this);
    btnTwo = new JButton("2");
    btnTwo.setFont(buttonFont);
    btnTwo.setActionCommand(Constants.ACTION_COMMAND_TWO);
    btnTwo.addActionListener(this);
    btnThree = new JButton("3");
    btnThree.setFont(buttonFont);
    btnThree.setActionCommand(Constants.ACTION_COMMAND_THREE);
    btnThree.addActionListener(this);
    btnMinus = new JButton("-");
    btnMinus.setFont(buttonFont);
    btnEquals = new JButton("=");
    btnEquals.setFont(buttonFont);

    // Row Five
    btnZero = new JButton("0");
    btnZero.setFont(buttonFont);
    btnZero.setActionCommand(Constants.ACTION_COMMAND_ZERO);
    btnZero.addActionListener(this);
    btnComma = new JButton(",");
    btnComma.setFont(buttonFont);
    btnPlus = new JButton("+");
    btnPlus.setFont(buttonFont);
}

private void situateComponents() {
    // Row One
    this.add(btnBck, cg.getConstraints(0));
    this.add(btnCE, cg.getConstraints(1));
    this.add(btnC, cg.getConstraints(2));
    this.add(btnSign, cg.getConstraints(3));
    this.add(btnSqrt, cg.getConstraints(4));

    // Row Two
    this.add(btnSeven, cg.getConstraints(5));
    this.add(btnEight, cg.getConstraints(6));
    this.add(btnNine, cg.getConstraints(7));
    this.add(btnDiv, cg.getConstraints(8));
    this.add(btnPercent, cg.getConstraints(9));

    // Row Three
    this.add(btnFour, cg.getConstraints(10));
    this.add(btnFive, cg.getConstraints(11));
    this.add(btnSix, cg.getConstraints(12));
    this.add(btnMult, cg.getConstraints(13));
    this.add(btnFrac, cg.getConstraints(14));

    // Row Four
    this.add(btnOne, cg.getConstraints(15));
    this.add(btnTwo, cg.getConstraints(16));
    this.add(btnThree, cg.getConstraints(17));
    this.add(btnMinus, cg.getConstraints(18));
    this.add(btnEquals, cg.getConstraints(19));

    // Row Five
    this.add(btnZero, cg.getConstraints(20));
    this.add(btnComma, cg.getConstraints(21));
    this.add(btnPlus, cg.getConstraints(22));
}

@Override
public void propertyChange(PropertyChangeEvent evt) {
    // TODO Auto-generated method stub

}

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_ONE)) {
        String v = "1";
        this.firePropertyChange(Constants.ACTION_COMMAND_ONE, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_TWO)) {
        String v = "2";
        this.firePropertyChange(Constants.ACTION_COMMAND_TWO, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_THREE)) {
        String v = "3";
        this.firePropertyChange(Constants.ACTION_COMMAND_THREE, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_FOUR)) {
        String v = "4";
        this.firePropertyChange(Constants.ACTION_COMMAND_FOUR, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_FIVE)) {
        String v = "5";
        this.firePropertyChange(Constants.ACTION_COMMAND_FIVE, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_SIX)) {
        String v = "6";
        this.firePropertyChange(Constants.ACTION_COMMAND_SIX, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_SEVEN)) {
        String v = "7";
        this.firePropertyChange(Constants.ACTION_COMMAND_SEVEN, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_EIGHT)) {
        String v = "8";
        this.firePropertyChange(Constants.ACTION_COMMAND_EIGHT, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_NINE)) {
        String v = "9";
        this.firePropertyChange(Constants.ACTION_COMMAND_NINE, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_ZERO)) {
        String v = "0";
        this.firePropertyChange(Constants.ACTION_COMMAND_ZERO, null, v);
    } else if(e.getActionCommand().equalsIgnoreCase(Constants.ACTION_COMMAND_BACK)) {
        this.firePropertyChange(Constants.ACTION_COMMAND_BACK, false, true);
    }
}

计算器正在进行中,但我想在做其他任何事情之前完成按钮。谁能告诉我,如果我做错了什么?

0 个答案:

没有答案