如何让我的CE按钮占据整个最后一行?

时间:2016-12-13 17:36:28

标签: java jframe jpanel jbutton

我想要的只是让我的整个最后一行成为一个按钮。实际上,这是我的计算器程序的一部分。我创建了另一个三角计算面板。当我删除sin,cos,tan按钮时,计算器看起来不是很好,最后一行只有一个按钮而行的其余部分都是空白的。因此,在删除sin,cos,tan按钮后,我试图通过.setsize()增加CE按钮的大小,但是徒劳无功。我希望有人可以帮助我。

public final class Cal implements ActionListener, KeyListener {

Font font = new Font("SansSerif", Font.BOLD, 32);
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(6, 4, 4, 4));
JTextField field = new JTextField("0.0");
JButton[][] keys = new JButton[6][5];

final int plus = 1;
final int minus = 2;
final int times = 3;
final int divide = 4;
final int sin = 5;
final int cos = 6;
final int ce = 7;
final int perc = 8;
int op;
double ans = 0;
double accum = 0;
boolean newNumb = true;
String calc = "";

//Assign method 
public void assign() {
    frame.setTitle("Scientific Calculator");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLayout(new BorderLayout());
    frame.add(panel, BorderLayout.CENTER);

    field.setHorizontalAlignment(JTextField.RIGHT);
    field.setEditable(true);
    field.setForeground(Color.red);
    field.setBackground(Color.white);
    field.setToolTipText("Input Box");
    field.setFont(font);

    frame.add(field, BorderLayout.NORTH);
    frame.setVisible(true);
    frame.setSize(430, 430);
    frame.addKeyListener(this);
    // field.addKeyListener(this); //renders field uneditable
    field.requestFocus();
    panel.setBackground(Color.black);
}

[enter image description here][1]//buttons Method    
public void buttons() {
    String action;

    int i, j;
    // create the keys
    keys = new JButton[6][5];
    keys[0][0] = new JButton("7");
    keys[0][1] = new JButton("8");
    keys[0][2] = new JButton("9");
    keys[0][3] = new JButton("+");
    keys[1][0] = new JButton("4");
    keys[1][1] = new JButton("5");
    keys[1][2] = new JButton("6");
    keys[1][3] = new JButton("-");
    keys[2][0] = new JButton("1");
    keys[2][1] = new JButton("2");
    keys[2][2] = new JButton("3");
    keys[2][3] = new JButton("x");
    keys[3][0] = new JButton("0");
    keys[3][1] = new JButton(".");
    keys[3][2] = new JButton("=");
    keys[3][3] = new JButton("/");
    keys[4][0] = new JButton("x³");
    keys[4][1] = new JButton("x²");
    keys[4][2] = new JButton("%");
    keys[4][3] = new JButton("√");
    keys[5][0] = new JButton("sin");
    keys[5][1] = new JButton("tan");
    keys[5][2] = new JButton("cos");
    keys[5][3] = new JButton("CE");
    //i = rows j =colums
    for (i = 0; i < 6; i++) {
        for (j = 0; j < 4; j++) {
            keys[i][j].setFont(font);
            action = (new Integer(i)).toString() + (new Integer(j)).toString();
            keys[i][j].setActionCommand(action);
            keys[i][j].addActionListener(this);
            keys[i][j].setBackground(Color.white);
            keys[i][j].setForeground(Color.red);

            panel.add(keys[i][j]);
        }
    }//end of For loops
}

0 个答案:

没有答案