Ich bin dabei einen simplen Entwurf vom Design eines Taschenrechners zu entwerfen. Ich habe mein Programm in 2 Klassen unterteilt, eine zu den Tasten und die andre zum Display, diese ist auch die Hauptklasse. Mein Problem ist jetzt, dass ich das nicht richtig zusammengefügt bekomme. Das Projekt soll aber auf jeden Fall in mehrere Klassen unterteilt bleiben.......
Die Hauptklasse:
Und hier die andere Klasse:
Die Hauptklasse:
Code:
import java.awt.*;
import javax.swing.*;
public class Display extends JPanel{
JTextField textField;
//Breite und Höhe des Displays
int breite;
int höhe;
//aktuelle Zahl in der Anzeige
int anzeige;
//Zustand der Eingabe, abgeschlossen?
boolean eingabe;
JFrame tmpFrame = new JFrame("Display");
JPanel tmpPanel = new JPanel();
public Display(int höhe, int breite){
super();
init();
tmpFrame.setSize(breite, höhe);
tmpFrame.setDefaultCloseOperation(tmpFrame.EXIT_ON_CLOSE);
Tasten t = new Tasten(this);
tmpFrame.setLayout( new BorderLayout(5,5));
Panel tmpPanel = new Panel();
tmpPanel.add(textField);
tmpFrame.add(tmpPanel, BorderLayout.NORTH);
tmpFrame.add(tmpPanelEast, BorderLayout.EAST);
tmpFrame.add(tmpPanelWest, BorderLayout.WEST);
tmpFrame.setVisible(true);
anzeige = 0;
eingabe = true;
}
/*public void enterZahl(int zahl){
//Abfrage der Situation, Eingabe fortsetzen?
if(eingabe == true){
anzeige = anzeige + zahl;
}
tmpPanel.repaint();
}*/
public void reset(){
anzeige = 0;
eingabe = false;
}
//Textfeld einrichten
public void init(){
textField = new JTextField();
textField.setColumns(20);
this.add(textField);
}
public JTextField getTextField() {
return textField;
}
public static void main(String[]args){
Display tmpDisplay = new Display(300,500);
}
}
Und hier die andere Klasse:
Code:
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.*;
public class Tasten extends JPanel implements ActionListener{
Display display;
public Tasten(Display aDisplay){
super();
display = aDisplay;
init();
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
display.getTextField().setText(command);
}
public void init() {
JPanel tmpPanelEast = new JPanel();
tmpPanelEast.setLayout(new GridLayout(4,3,5,5));
//Deklaration&Initalisierung der Tasten 1-9
for(int i=9; i >= 1; i--){
JButton tmpButton = new JButton(""+i);
tmpButton.addActionListener(this);
tmpButton.setBackground(Color.gray);
this.add(tmpButton);
}
// 0
JButton tmpButton0 = new JButton("0");
tmpButton0.addActionListener(this);
tmpButton0.setBackground(Color.gray);
this.add(tmpButton0);
// .
JButton tmpButton5 = new JButton(".");
tmpButton5.addActionListener(this);
tmpButton5.setBackground(Color.gray);
this.add(tmpButton5);
JPanel tmpPanelWest = new JPanel();
tmpPanelWest.setLayout(new GridLayout(4,2,5,5));
// DEL
JButton tmpButton6 = new JButton("DEL");
tmpButton6.addActionListener(this);
tmpButton6.setBackground(Color.gray);
this.add(tmpButton6);
//Clear
JButton tmpButtonClear = new JButton("AC");
tmpButton6.addActionListener(this);
tmpButton6.setBackground(Color.gray);
this.add(tmpButtonClear);
// *
JButton tmpButton4 = new JButton("*");
tmpButton4.addActionListener(this);
tmpButton4.setBackground(Color.gray);
this.add(tmpButton4);
// /
JButton tmpButton3 = new JButton("/");
tmpButton3.addActionListener(this);
tmpButton3.setBackground(Color.gray);
this.add(tmpButton3);
// +
JButton tmpButton1 = new JButton("+");
tmpButton1.addActionListener(this);
tmpButton1.setBackground(Color.gray);
this.add(tmpButton1);
// -
JButton tmpButton2 = new JButton("-");
tmpButton2.addActionListener(this);
tmpButton2.setBackground(Color.gray);
this.add(tmpButton2);
// =
JButton tmpButton7 = new JButton("=");
tmpButton7.addActionListener(this);
tmpButton7.setBackground(Color.gray);
this.add(tmpButton7);
}
}