erkk84
05-02-2008, 20:41
ciao a tutti chiedo aiuto per l'uso dei metodi paint e repaint nell'implementazione di una semplice calcolatrice java; in particolare vorrei sapere come implementare la funzione che scrive il risultato di un'operazione non in una jtextarea di turno ma deve essere "disegnato" il numero in un JPanel (o comunque in un componente); magari per essere più chiaro posto il codice fatto fin qui:
PANNELLO PRINCIPALE:
public class Calc extends JPanel{
JPanel sopra = new JPanel();
JPanel sotto = new JPanel();
JPanel sinistra = new JPanel();
JPanel destra = new JPanel();
JButton somma = new JButton("+");
JButton sott = new JButton("-");
JButton molt = new JButton("*");
JButton div = new JButton("/");
//JLabel labelStato = new JLabel("ABILITATO");
JLabel labelX = new JLabel("X =");
JLabel labelY = new JLabel("Y =");
JTextArea area = new JTextArea();
JTextArea areaX = new JTextArea("00");
JTextArea areaY = new JTextArea("00");
public Calc() {
this.setLayout(new BorderLayout());
this.add(sopra,BorderLayout.NORTH);
sopra.setLayout(new BorderLayout());
this.add(sotto,BorderLayout.CENTER);
sotto.setLayout(new BorderLayout());
sopra.add(sinistra,BorderLayout.WEST);
sinistra.setLayout(new GridLayout());
sopra.add(destra,BorderLayout.CENTER);
destra.setLayout(new BorderLayout());
sotto.setBackground(Color.GREEN);
sinistra.add(somma);
sinistra.add(sott);
sinistra.add(molt);
sinistra.add(div);
Operazioni azione = new Operazioni(sotto);
somma.addActionListener(azione);
destra.repaint();
somma.setVisible(true);
sott.setVisible(true);
molt.setVisible(true);
div.setVisible(true);
//labelStato.setVisible(true);
JPanel panelX = new JPanel();
destra.add(panelX,BorderLayout.NORTH);
panelX.setLayout(new FlowLayout());
panelX.add(labelX);
panelX.add(areaX);
labelX.setVisible(true);
areaX.setVisible(true);
JPanel panelY = new JPanel();
destra.add(panelY,BorderLayout.SOUTH);
panelY.setLayout(new FlowLayout());
panelY.add(labelY);
panelY.add(areaY);
labelY.setVisible(true);
areaY.setVisible(true);
}
}
FRAME DI PARTENZA:
public class MioFrame extends JFrame{
private void jbInit() throws Exception {
this.setContentPane(new Calc());
this.setSize(new Dimension(450,450));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public MioFrame() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
MioFrame miojframe = new MioFrame();
}
}
CLASSE CHE (DOVREBBE) DISEGNA (Per intanto provo con delle semplci stringhe):
public class Operazioni extends JPanel implements ActionListener {
public JPanel panel;
public Operazioni(JPanel panel) {
this.panel=panel;
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
//g.drawString("Ciao", 200, 200);
g.drawRect(200,200,200,200);
}
public void actionPerformed(ActionEvent arg0) {
panel.validate();
panel.repaint();
}
}
sostanzialemnete non succede nulla e non riesco a capire il perchè...
PANNELLO PRINCIPALE:
public class Calc extends JPanel{
JPanel sopra = new JPanel();
JPanel sotto = new JPanel();
JPanel sinistra = new JPanel();
JPanel destra = new JPanel();
JButton somma = new JButton("+");
JButton sott = new JButton("-");
JButton molt = new JButton("*");
JButton div = new JButton("/");
//JLabel labelStato = new JLabel("ABILITATO");
JLabel labelX = new JLabel("X =");
JLabel labelY = new JLabel("Y =");
JTextArea area = new JTextArea();
JTextArea areaX = new JTextArea("00");
JTextArea areaY = new JTextArea("00");
public Calc() {
this.setLayout(new BorderLayout());
this.add(sopra,BorderLayout.NORTH);
sopra.setLayout(new BorderLayout());
this.add(sotto,BorderLayout.CENTER);
sotto.setLayout(new BorderLayout());
sopra.add(sinistra,BorderLayout.WEST);
sinistra.setLayout(new GridLayout());
sopra.add(destra,BorderLayout.CENTER);
destra.setLayout(new BorderLayout());
sotto.setBackground(Color.GREEN);
sinistra.add(somma);
sinistra.add(sott);
sinistra.add(molt);
sinistra.add(div);
Operazioni azione = new Operazioni(sotto);
somma.addActionListener(azione);
destra.repaint();
somma.setVisible(true);
sott.setVisible(true);
molt.setVisible(true);
div.setVisible(true);
//labelStato.setVisible(true);
JPanel panelX = new JPanel();
destra.add(panelX,BorderLayout.NORTH);
panelX.setLayout(new FlowLayout());
panelX.add(labelX);
panelX.add(areaX);
labelX.setVisible(true);
areaX.setVisible(true);
JPanel panelY = new JPanel();
destra.add(panelY,BorderLayout.SOUTH);
panelY.setLayout(new FlowLayout());
panelY.add(labelY);
panelY.add(areaY);
labelY.setVisible(true);
areaY.setVisible(true);
}
}
FRAME DI PARTENZA:
public class MioFrame extends JFrame{
private void jbInit() throws Exception {
this.setContentPane(new Calc());
this.setSize(new Dimension(450,450));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public MioFrame() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
MioFrame miojframe = new MioFrame();
}
}
CLASSE CHE (DOVREBBE) DISEGNA (Per intanto provo con delle semplci stringhe):
public class Operazioni extends JPanel implements ActionListener {
public JPanel panel;
public Operazioni(JPanel panel) {
this.panel=panel;
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
//g.drawString("Ciao", 200, 200);
g.drawRect(200,200,200,200);
}
public void actionPerformed(ActionEvent arg0) {
panel.validate();
panel.repaint();
}
}
sostanzialemnete non succede nulla e non riesco a capire il perchè...