将GroupLayout更改为GridBagLayout



我正在创建一个简单的计算器GUI,我一直在使用netbeans IDE GUI构建器。我一直试图使GroupLayout代码的意义,不幸的是,我只是没有得到它。我想也许通过改变到GridBagLayout,它会更清楚,我将在一个更好的位置来理解代码。

下面是我的代码。谁能帮我把它改成GridBagLayout ?

public class CITCalculatorGUI extends javax.swing.JFrame {
/* Creates new frame CITCalculatorGUI */
public CITCalculatorGUI() {
    initComponents();
}
/* This method is called from within the constructor to
 * initialize the frame. 
 */
private void initComponents() {
    tempTextField = new javax.swing.JTextField();
    sevenButton = new javax.swing.JButton();
    eightButton = new javax.swing.JButton();
    nineButton = new javax.swing.JButton();
    fourButton = new javax.swing.JButton();
    fiveButton = new javax.swing.JButton();
    sixButton = new javax.swing.JButton();
    oneButton = new javax.swing.JButton();
    twoButton = new javax.swing.JButton();
    threeButton = new javax.swing.JButton();
    zeroButton = new javax.swing.JButton();
    positiveNegativeToggle = new javax.swing.JButton();
    decimalPoint = new javax.swing.JButton();
    base16A = new javax.swing.JButton();
    base16B = new javax.swing.JButton();
    base16C = new javax.swing.JButton();
    base16D = new javax.swing.JButton();
    base16E = new javax.swing.JButton();
    base16F = new javax.swing.JButton();
    plusButton = new javax.swing.JButton();
    minusButton = new javax.swing.JButton();
    divisionButton = new javax.swing.JButton();
    multiplication = new javax.swing.JButton();
    andButton = new javax.swing.JButton();
    orButton = new javax.swing.JButton();
    xorButton = new javax.swing.JButton();
    norButton = new javax.swing.JButton();
    equalsButton = new javax.swing.JButton();
    nandButton = new javax.swing.JButton();
    makLogo = new javax.swing.JLabel();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("CIT Calculator");
    sevenButton.setBackground(new java.awt.Color(0, 0, 255));
    sevenButton.setForeground(new java.awt.Color(153, 153, 153));
    sevenButton.setText("7");
    eightButton.setBackground(new java.awt.Color(0, 0, 255));
    eightButton.setForeground(new java.awt.Color(153, 153, 153));
    eightButton.setText("8");
    nineButton.setBackground(new java.awt.Color(0, 0, 255));
    nineButton.setForeground(new java.awt.Color(153, 153, 153));
    nineButton.setText("9");
    fourButton.setBackground(new java.awt.Color(0, 0, 255));
    fourButton.setForeground(new java.awt.Color(153, 153, 153));
    fourButton.setText("4");
    fiveButton.setBackground(new java.awt.Color(0, 0, 255));
    fiveButton.setForeground(new java.awt.Color(153, 153, 153));
    fiveButton.setText("5");
    sixButton.setBackground(new java.awt.Color(0, 0, 255));
    sixButton.setForeground(new java.awt.Color(153, 153, 153));
    sixButton.setText("6");
    oneButton.setBackground(new java.awt.Color(0, 0, 255));
    oneButton.setForeground(new java.awt.Color(153, 153, 153));
    oneButton.setText("1");
    twoButton.setBackground(new java.awt.Color(0, 0, 255));
    twoButton.setForeground(new java.awt.Color(153, 153, 153));
    twoButton.setText("2");
    threeButton.setBackground(new java.awt.Color(0, 0, 255));
    threeButton.setForeground(new java.awt.Color(153, 153, 153));
    threeButton.setText("3");
    zeroButton.setBackground(new java.awt.Color(0, 0, 255));
    zeroButton.setForeground(new java.awt.Color(153, 153, 153));
    zeroButton.setText("0");
    positiveNegativeToggle.setBackground(new java.awt.Color(0, 0, 255));
    positiveNegativeToggle.setForeground(new java.awt.Color(153, 153, 153));
    positiveNegativeToggle.setText("+/-");
    decimalPoint.setBackground(new java.awt.Color(0, 0, 255));
    decimalPoint.setForeground(new java.awt.Color(153, 153, 153));
    decimalPoint.setText(".");
    base16A.setBackground(new java.awt.Color(0, 0, 255));
    base16A.setForeground(new java.awt.Color(153, 153, 153));
    base16A.setText("A");
    base16B.setBackground(new java.awt.Color(0, 0, 255));
    base16B.setForeground(new java.awt.Color(153, 153, 153));
    base16B.setText("B");
    base16C.setBackground(new java.awt.Color(0, 0, 255));
    base16C.setForeground(new java.awt.Color(153, 153, 153));
    base16C.setText("C");
    base16D.setBackground(new java.awt.Color(0, 0, 255));
    base16D.setForeground(new java.awt.Color(153, 153, 153));
    base16D.setText("D");
    base16E.setBackground(new java.awt.Color(0, 0, 255));
    base16E.setForeground(new java.awt.Color(153, 153, 153));
    base16E.setText("E");
    base16F.setBackground(new java.awt.Color(0, 0, 255));
    base16F.setForeground(new java.awt.Color(153, 153, 153));
    base16F.setText("F");
    plusButton.setBackground(new java.awt.Color(51, 255, 0));
    plusButton.setForeground(new java.awt.Color(153, 153, 153));
    plusButton.setText("+");
    minusButton.setBackground(new java.awt.Color(51, 255, 0));
    minusButton.setForeground(new java.awt.Color(153, 153, 153));
    minusButton.setText("-");
    divisionButton.setBackground(new java.awt.Color(51, 255, 0));
    divisionButton.setForeground(new java.awt.Color(153, 153, 153));
    divisionButton.setText("/");
    multiplication.setBackground(new java.awt.Color(51, 255, 0));
    multiplication.setForeground(new java.awt.Color(153, 153, 153));
    multiplication.setText("*");
    andButton.setBackground(new java.awt.Color(51, 255, 0));
    andButton.setForeground(new java.awt.Color(153, 153, 153));
    andButton.setText("AND");
    orButton.setBackground(new java.awt.Color(51, 255, 0));
    orButton.setForeground(new java.awt.Color(153, 153, 153));
    orButton.setText("OR");
    xorButton.setBackground(new java.awt.Color(51, 255, 0));
    xorButton.setForeground(new java.awt.Color(153, 153, 153));
    xorButton.setText("XOR");
    norButton.setBackground(new java.awt.Color(51, 255, 0));
    norButton.setForeground(new java.awt.Color(153, 153, 153));
    norButton.setText("NOR");
    equalsButton.setBackground(new java.awt.Color(51, 255, 0));
    equalsButton.setForeground(new java.awt.Color(153, 153, 153));
    equalsButton.setText("=");
    nandButton.setBackground(new java.awt.Color(51, 255, 0));
    nandButton.setForeground(new java.awt.Color(153, 153, 153));
    nandButton.setText("NAND");
    makLogo.setIcon(new javax.swing.ImageIcon("C:\test\mak-logo.jpg")); 
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);//sets the layout manager
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(tempTextField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(base16D, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                .addComponent(base16A, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                .addComponent(zeroButton, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                .addComponent(oneButton, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                .addComponent(fourButton, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE))
                            .addComponent(sevenButton, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(base16E, 0, 0, Short.MAX_VALUE)
                            .addComponent(base16B, 0, 0, Short.MAX_VALUE)
                            .addComponent(positiveNegativeToggle, 0, 0, Short.MAX_VALUE)
                            .addComponent(twoButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(fiveButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(eightButton, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(base16F, 0, 0, Short.MAX_VALUE)
                            .addComponent(base16C, 0, 0, Short.MAX_VALUE)
                            .addComponent(decimalPoint, 0, 0, Short.MAX_VALUE)
                            .addComponent(threeButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(sixButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(nineButton, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(multiplication, 0, 0, Short.MAX_VALUE)
                                        .addComponent(xorButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(plusButton, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(norButton, 0, 0, Short.MAX_VALUE)
                                    .addComponent(andButton, 0, 0, Short.MAX_VALUE)
                                    .addComponent(minusButton, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)))
                            .addComponent(nandButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(equalsButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(orButton, 0, 0, Short.MAX_VALUE)
                            .addComponent(divisionButton, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE))))
                .addComponent(makLogo))
            .addContainerGap(23, Short.MAX_VALUE))
    );
    layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {base16A, base16B, base16C, base16D, base16E, base16F, decimalPoint, eightButton, fiveButton, fourButton, nineButton, oneButton, positiveNegativeToggle, sevenButton, sixButton, threeButton, twoButton, zeroButton});
    layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {andButton, divisionButton, equalsButton, minusButton, multiplication, nandButton, norButton, orButton, plusButton, xorButton});
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(tempTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(eightButton)
                        .addComponent(sevenButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(fourButton)
                        .addComponent(fiveButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(oneButton)
                        .addComponent(twoButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(zeroButton)
                        .addComponent(positiveNegativeToggle))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(base16A)
                        .addComponent(base16B)))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nineButton)
                        .addComponent(minusButton)
                        .addComponent(divisionButton)
                        .addComponent(plusButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(sixButton)
                        .addComponent(multiplication)
                        .addComponent(andButton)
                        .addComponent(orButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(threeButton)
                        .addComponent(xorButton)
                        .addComponent(norButton)
                        .addComponent(equalsButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(decimalPoint)
                        .addComponent(nandButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(base16C)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(base16D)
                .addComponent(base16E)
                .addComponent(base16F))
            .addGap(28, 28, 28)
            .addComponent(makLogo)
            .addContainerGap(28, Short.MAX_VALUE))
    );
    pack();//pack method ensures the frame is at its preferred size
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new CITCalculatorGUI().setVisible(true);
        }
    });
}// this code ensures thread safety.
// Variables declaration
private javax.swing.JButton andButton;
private javax.swing.JButton base16A;
private javax.swing.JButton base16B;
private javax.swing.JButton base16C;
private javax.swing.JButton base16D;
private javax.swing.JButton base16E;
private javax.swing.JButton base16F;
private javax.swing.JButton decimalPoint;
private javax.swing.JButton divisionButton;
private javax.swing.JButton eightButton;
private javax.swing.JButton equalsButton;
private javax.swing.JButton fiveButton;
private javax.swing.JButton fourButton;
private javax.swing.JLabel makLogo;
private javax.swing.JButton minusButton;
private javax.swing.JButton multiplication;
private javax.swing.JButton nandButton;
private javax.swing.JButton nineButton;
private javax.swing.JButton norButton;
private javax.swing.JButton oneButton;
private javax.swing.JButton orButton;
private javax.swing.JButton plusButton;
private javax.swing.JButton positiveNegativeToggle;
private javax.swing.JButton sevenButton;
private javax.swing.JButton sixButton;
private javax.swing.JTextField tempTextField;
private javax.swing.JButton threeButton;
private javax.swing.JButton twoButton;
private javax.swing.JButton xorButton;
private javax.swing.JButton zeroButton;
// End of variables declaration
}

看这里:java swing中的计算器。它在面板中使用GridLayout,可能会对你有所帮助。

这是一个与GroupLayout几乎相同布局的代码片段,但只有10行代码:

DesignGridLayout layout = new DesignGridLayout(getContentPane());
layout.row().grid().add(tempTextField);
layout.row().grid().add(sevenButton, eightButton, nineButton).grid().add(plusButton, minusButton, divisionButton);
layout.row().grid().add(fourButton, fiveButton, sixButton).grid().add(multiplication, andButton, orButton);
layout.row().grid().add(oneButton, twoButton, threeButton).grid().add(xorButton, norButton, equalsButton);
layout.row().grid().add(zeroButton, positiveNegativeToggle, decimalPoint).grid().empty().add(nandButton).empty();
layout.row().grid(1).add(base16A, base16B, base16C);
layout.row().grid(1).add(base16D, base16E, base16F);
layout.emptyRow();
layout.row().left().add(makLogo);

它使用DesignGridLayout使布局易于编码和维护。与基于GroupLayout的示例的唯一视觉区别是,当您放大框架时,所有按钮都会变大(根据我自己的品味,这并不一定是坏的)。

请注意,我花了大约5-10分钟来写上面的10行。

编辑:添加了额外的2行代码来添加底部的标志

相关内容

  • 没有找到相关文章

最新更新