Aplikacja: prosty kalkulator który dodaje odejmuje mnoży dzieli itp.
Jeśli się klika w odpowiednie buttony wszytko działa jak trzeba. Chciałbym żeby działało wtedy kiedy naciskam klawisze cyferek na klawiaturze. Problem polega na tym, że nie potrafię wywołać metody ze słuchacza ActionListener w odpowiedniej metodzie KeyListener. Proszę o pomoc.
Pozdrawiam i z góry dziękuję za pomoc.
Piotr M.
Mój kod:
Code: Zaznacz cały
package kalkulator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
CalcFrame frame = new CalcFrame();
frame.initComponets();
}
}
/*
* Tworzy ramke kalkulatora i jego panel
* ustawia właściwości ramki
*/
class CalcFrame extends JFrame
{
CalcFrame()
{
this.setTitle("Kalkulator");
this.setBounds(100, 100, 200, 200);
CalcPanel panel = new CalcPanel();
this.add(panel);
this.pack();
}
public void initComponets()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
/*
* Dodaje do panelu w ramce wszystkie
* komponenty jednoczesnie obsługując zdarzenia
*/
class CalcPanel extends JPanel
{
CalcPanel()
{
this.setLayout(new BorderLayout());
wynik = 0;
lastCommand = "=";
start = true;
// Tworzy wyswetlacz i ustawia go na górze ramki
display = new JButton("0");
display.setEnabled(false);
this.add(display, BorderLayout.NORTH);
/*
* Inicjuje klasy dla sluchaczy
* ponizej sa definicje tych klass
*/
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
ActionListener delete = new DeleteAction();
/*
*
*/
panel = new JPanel();
panel.setLayout(new GridLayout(4, 5, 6, 6));
addButton ("7", insert);
addButton ("8", insert);
addButton ("9", insert);
addButton ("/", command);
addButton ("<-", delete);
addButton ("4", insert);
addButton ("5", insert);
addButton ("6", insert);
addButton ("*", command);
addButton ("C", delete);
addButton ("1", insert);
addButton ("2", insert);
addButton ("3", insert);
addButton ("-", command);
addButton ("sqt", command);
addButton ("0", insert);
addButton (".", insert);
addButton ("=", command);
addButton ("+", command);
addButton ("^x", command);
add(panel, BorderLayout.CENTER);
}
/*
* Metoda dodajaca przyciski i obsluge zdarzenia
*/
private void addButton (String label, ActionListener lisner)
{
JButton buton = new JButton(label);
buton.addActionListener(lisner);
buton.addKeyListener(new KeyAdapter()
{
@Override
public void keyTyped(KeyEvent e)
{
if (e.getKeyChar() == 1)
{
}
}
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_1)
{
// ??
}
if (e.getKeyCode() == KeyEvent.VK_2)
{
// ??
}
}
@Override
public void keyReleased(KeyEvent e)
{
// ??
}
});
panel.add(buton);
}
/*
* Klasa obslugujaca zdarzenia imput, command i delete
* Imput zdarzenie dopisania liczby do wyswietlania
* Command zdarzenie wykonania dzialania matematycznego
*/
private class InsertAction implements ActionListener
{
// Może do istniejącego lisnera da sie dopisac zeby
// słuchał rownież zdarzenia z KeyListener?
public void actionPerformed(ActionEvent e)
{
String imput = e.getActionCommand();
if (start)
{
display.setText("");
start = false;
}
display.setText(display.getText() + imput);
}
}
private class DeleteAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String imput = e.getActionCommand();
String p = "";
if (imput.equals("<-"))
{
imput = display.getText();
display.setText("");
for (int i = 0; i <= imput.length()-2; i++)
{
display.setText(display.getText() + imput.charAt(i));
}
}
if (imput.equals("C"))
{
display.setText("0");
}
}
}
private class CommandAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if (start)
{
if (command.equals("-"))
{
display.setText(command);
start = false;
}
else lastCommand = command;
}
else
{
calculate(Double.parseDouble(display.getText()), command);
lastCommand = command;
start = true;
}
}
}
/*
* Metoda wykonujaca polecenia matematyczne
*/
public void calculate(double x, String command)
{
String com = command;
if (lastCommand.equals("+"))
{
wynik += x; System.out.println(wynik + " " + lastCommand + " " + x);
};
if (lastCommand.equals("-"))
{
wynik -= x; System.out.println(wynik + " " + lastCommand + " " + x);
};
if (lastCommand.equals("*"))
{
wynik *= x; System.out.println(wynik + " " + lastCommand + " " + x);
};
if (lastCommand.equals("/"))
{
wynik /= x; System.out.println(wynik + " " + lastCommand + " " + x);
};
if ((lastCommand.equals("^x")) && (com.equals("=")))
{
wynik = Math.pow(wynik, x);
System.out.println(wynik + " " + lastCommand + " " + x);
}
if (lastCommand.equals("="))
{
wynik = x;
System.out.println(wynik + " " + lastCommand);
};
display.setText("" + wynik);
}
private JButton display;
private JPanel panel;
private double wynik;
private String lastCommand;
private boolean start;
}