-1

宿題は、私たちが本当に基本的な計算機を作った以前のラボを拡張したものです。いくつかのボタンpow、sqrt、pct、およびlogを追加することになっています。

BasicCalculator.javaファイルから拡張しましたが、新しいクラスで追加したボタンは追加されていませんが、setBackgroundの色は完全に正常に機能しています。では、なぜ同じ方法で追加されたボタンが追加されないのですか?

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

public class Calculator extends BasicCalculator
{
// Attributes
protected JButton pow, sqr, pct, log;
// Constructors
public Calculator()
{
    super();
}
// Methods
public void createModifiedUserInterface()
{
    try {
        UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() );
    } 
    catch (Exception e) {
        e.printStackTrace();
    }

    createUserInterface();
    pow = new JButton("^");
    sqr = new JButton("sqr");
    pct = new JButton("pct");
    log = new JButton("log");

    keyPad.add(pow);
    keyPad.add(sqr);
    keyPad.add(log);
    keyPad.add(pct);

    displayBox.setHorizontalAlignment(displayBox.RIGHT);
    displayBox.setBackground(Color.YELLOW);

    clr.setBackground(Color.RED);
    com.setBackground(Color.GREEN);        
    b0.setBackground(Color.YELLOW);
    b1.setBackground(Color.YELLOW);
    b2.setBackground(Color.YELLOW);
    b3.setBackground(Color.YELLOW);
    b4.setBackground(Color.YELLOW);
    b5.setBackground(Color.YELLOW);
    b6.setBackground(Color.YELLOW);
    b7.setBackground(Color.YELLOW);
    b8.setBackground(Color.YELLOW);
    b9.setBackground(Color.YELLOW);
    bd.setBackground(Color.YELLOW);
    add.setBackground(Color.ORANGE);
    sub.setBackground(Color.ORANGE);
    mul.setBackground(Color.ORANGE);
    div.setBackground(Color.ORANGE);
    mod.setBackground(Color.ORANGE);
    pow.setBackground(Color.ORANGE);
    sqr.setBackground(Color.PINK);
    log.setBackground(Color.PINK);
    pct.setBackground(Color.PINK);

    pow.addActionListener(this);
    sqr.addActionListener(this);
    log.addActionListener(this);
    pct.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
    displayBox.setBackground(Color.YELLOW);
    super.actionPerformed(e);
    if (e.getSource() == pow)
    {
        operator = '^';
        operand1 = new Double(displayBox.getText()).doubleValue();
        displayBox.setText("");
    }
    if (e.getSource() == sqr)
    {
        operand1 = new Double(displayBox.getText()).doubleValue();
        displayBox.setBackground(Color.GREEN);
        displayBox.setText(new Double(Math.sqrt(operand1)).toString());
    }
    if (e.getSource() == log)
    {
        operand1 = new Double(displayBox.getText()).doubleValue();
        displayBox.setBackground(Color.GREEN);
        displayBox.setText(new Double(Math.log(operand1)).toString());
    }
    if (e.getSource() == pct)
    {
        operand1 = new Double(displayBox.getText()).doubleValue();
        displayBox.setBackground(Color.GREEN);            
        displayBox.setText(new Double(operand1 * 100).toString());
    }
}

public void calculate()
{
    displayBox.setBackground(Color.GREEN);
    super. calculate();

    if (operator == '^')
    {
        displayBox.setText(new Double(Math.pow(operand1, operand2)).toString());
    }
}

public static void main(String[] args) 
{
    Calculator mine = new Calculator();
    mine.createModifiedUserInterface(); 
}

}

これは、かなり不格好ですが、「BasicCalculator.java」ファイルにあるコードです。

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

public class BasicCalculator implements ActionListener
{
//attributes
protected double operand1, operand2;
protected char operator;
protected JFrame calWin;
protected JTextField displayBox;
protected JPanel keyPad;
protected JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, dot;
protected JButton add, sub, mul, div, mod, com, clr;

//constructors
public BasicCalculator() 
{
    clean();
}

//methods
public void clean()
{
    operand1 = 0;
    operand2 = 0;
    operator = ' ';

}

public void createUserInterface()
{
    calWin = new JFrame("Calculator");
    displayBox = new JTextField(20);
    keyPad = new JPanel();
    b0 = new JButton("0");
    b1 = new JButton("1");
    b2 = new JButton("2");
    b3 = new JButton("3");
    b4 = new JButton("4");
    b5 = new JButton("5");
    b6 = new JButton("6");
    b7 = new JButton("7");
    b8 = new JButton("8");
    b9 = new JButton("9");
    dot = new JButton(".");
    add = new JButton("+");
    sub = new JButton("-");
    mul = new JButton("*");
    div = new JButton("/");
    mod = new JButton("%");
    com = new JButton("=");
    clr = new JButton("C");

    keyPad.add(displayBox);
    keyPad.add(b0);
    keyPad.add(b1);
    keyPad.add(b2);
    keyPad.add(b3);
    keyPad.add(b4);
    keyPad.add(b5);
    keyPad.add(b6);
    keyPad.add(b7);
    keyPad.add(b8);
    keyPad.add(b9);
    keyPad.add(dot);
    keyPad.add(add);
    keyPad.add(sub);
    keyPad.add(mul);
    keyPad.add(div);
    keyPad.add(mod);
    keyPad.add(com);
    keyPad.add(clr);
    calWin.getContentPane().add(keyPad);
    calWin.setSize(300, 300);
    calWin.setVisible(true);
    calWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    b0.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);
    dot.addActionListener(this);
    add.addActionListener(this);
    sub.addActionListener(this);
    mul.addActionListener(this);
    div.addActionListener(this);
    mod.addActionListener(this);
    com.addActionListener(this);
    clr.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
    if(e.getSource() == b0)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "0");
    }
    if(e.getSource() == b1)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "1");
    }
    if(e.getSource() == b2)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "2");
    }
    if(e.getSource() == b3)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "3");
    }
    if(e.getSource() == b4)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "4");
    }
    if(e.getSource() == b5)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "5");
    }
    if(e.getSource() == b6)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "6");
    }
    if(e.getSource() == b7)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "7");
    }
    if(e.getSource() == b8)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "8");
    }
    if(e.getSource() == b9)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + "9");
    }
    if(e.getSource() == dot)
    {
        if(displayBox.getText().contains("+") && operator == '+' || 
           displayBox.getText().contains("-") && operator == '-' || 
           displayBox.getText().contains("/") && operator == '/' || 
           displayBox.getText().contains("*") && operator == '*' || 
           displayBox.getText().contains("%") && operator == '%')
        {
            displayBox.setText("");
        }
        displayBox.setText(displayBox.getText() + ".");
    }
    if(e.getSource() == add)
    {
        operator = '+';
        operand1 = new Double(displayBox.getText()).doubleValue();
        displayBox.setText("+ ");
    }
            if(e.getSource() == sub)
    {
        if(displayBox.getText().trim().equals("") || 
           operator == '+' ||
           operator == '-' ||
           operator == '/' ||
           operator == '*' ||
           operator == '%')
        {
            displayBox.setText("-");
        }
        else
        {
            operator = '-';
            operand1 = new Double(displayBox.getText()).doubleValue();
            displayBox.setText("- ");
        }
    }
    if(e.getSource() == div)
    {
        operator = '/';
        operand1 = new Double(displayBox.getText()).doubleValue();
        displayBox.setText("/ ");
    }
    if(e.getSource() == mul)
    {
        operator = '*';
        operand1 = new Double(displayBox.getText()).doubleValue();
        displayBox.setText("* ");
    }
    if(e.getSource() == mod)
    {
        operator = '%';
        operand1 = new Double(displayBox.getText()).doubleValue();
        displayBox.setText("% ");
    }
    if(e.getSource() == clr)
    {
        displayBox.setText("");
        clean();
    }
    if(e.getSource() == com)
    {
        operand2 = new Double(displayBox.getText()).doubleValue();
        calculate();
    }
}

public void calculate()
{
    switch(operator)
    {
        case '+': displayBox.setText(new Double(operand1 + operand2).toString()); break;
        case '-': displayBox.setText(new Double(operand1 - operand2).toString()); break;    
        case '*': displayBox.setText(new Double(operand1 * operand2).toString()); break;  
        case '/': displayBox.setText(new Double(operand1 / operand2).toString()); break;   
        case '%': displayBox.setText(new Double(operand1 % operand2).toString()); break;    
    }
}

}
4

1 に答える 1

3

あなたのニーズを満たすことができるレイアウトマネージャーを使用してくださいGridLayout...

ここに画像の説明を入力してください

BasicCalculatorクラスで、keyPadを使用するように変更しGridLayout、ウィンドウをパックします。

keyPad = new JPanel(new GridLayout(0, 3));
//...
calWin.getContentPane().add(keyPad);
于 2012-12-10T03:44:36.117 に答える