1
Programmazione 2 - Marco Ronchetti
Fondamenti di Java
Fac.Scienze – Università di Trento
Introduzione alla
costruzione di GUI
(graphic user interface)
Parte 2
2
Programmazione 2 - Marco Ronchetti
Seconda applicazione: calcolatrice
Fac.Scienze – Università di Trento
App
ActionListener
Model
View
ViewImplementation
Controller
JFrame
1
1
3
Programmazione 2 - Marco Ronchetti
Seconda applicazione: calcolatrice
Fac.Scienze – Università di Trento
App
ActionListener
Model
View
Controller
Controlla l’aspetto
dell’applicazione
ViewImplementation
4
JFrame
Programmazione 2 - Marco Ronchetti
Seconda applicazione: calcolatrice
Fac.Scienze – Università di Trento
App
ActionListener
Model
View
Controller
Controlla i dati
ViewImplementation
JFrame
2
2
5
Programmazione 2 - Marco Ronchetti
Seconda applicazione: calcolatrice
Fac.Scienze – Università di Trento
App
ActionListener
Model
View
Controller
Implementa la
Business logic
ViewImplementation
6
JFrame
Programmazione 2 - Marco Ronchetti
Seconda applicazione: calcolatrice
Fac.Scienze – Università di Trento
App
ActionListener
Model
View
Controller
NumberButton
ViewImplementation
OperationButton
JFrame
3
3
7
Programmazione 2 - Marco Ronchetti
class App
Fac.Scienze – Università di Trento
package it.unitn.science.programmazione.calculator;
import javax.swing.*;
import java.awt.*;
public class App
{
public static void main(String[] a) {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (Exception e) {
System.out.println ("Couldn't load Motif L&F " + e);
}
new App ();
}
8
Programmazione 2 - Marco Ronchetti
class App
Fac.Scienze – Università di Trento
public App()
{
Model m=new Model();
Controller c=new Controller();
View v=new ViewImplementation(c);
c.setModel(m);
c.setView(v);
c.initCalc();
v.show();
}
}
4
4
9
Programmazione 2 - Marco Ronchetti
class Model
Fac.Scienze – Università di Trento
package it.unitn.science.programmazione.calculator;
public class Model
{
double memoria1=0;
double memoria2=0;
public void cleanAll(){
memoria1=0;
memoria2=0;
}
public double getMemoria1() {return memoria1;}
public void setMemoria1(double x) {memoria1=x;}
public double getMemoria2() {return memoria2;}
public void setMemoria2(double x) {memoria2=x;}
}
10
Programmazione 2 - Marco Ronchetti
class NumberButton
Fac.Scienze – Università di Trento
package it.unitn.science.programmazione.calculator;
import java.awt.event.ActionListener;
import javax.swing.JButton;
class NumberButton extends JButton
{
static final String COMMAND="ADD_NUMBER";
public NumberButton(String s,ActionListener c)
{
super(s);
this.addActionListener(c);
this.setActionCommand(COMMAND);
}
}
5
5
11
Programmazione 2 - Marco Ronchetti
class OperationButton
Fac.Scienze – Università di Trento
package it.unitn.science.programmazione.calculator;
import java.awt.event.ActionListener;
import javax.swing.JButton;
class OperationButton extends JButton
{
static final String COMMAND="PERFORM_OPERATION";
static final int ENTER=0;
static final int ADDITION=1;
static final int SUBTRACTION=2;
static final int MULTIPLICATION=3;
static final int DIVISION=4;
static final int CLEAR=5;
static final int CLEARALL=6;
12
Programmazione 2 - Marco Ronchetti
class OperationButton
Fac.Scienze – Università di Trento
public OperationButton(String s,ActionListener c, int operation)
{
super(s);
this.addActionListener(c);
this.setActionCommand(COMMAND);
this.operationType=operation;
}
int getOperationType() {return operationType;}
}
6
6
13
Programmazione 2 - Marco Ronchetti
class View
Fac.Scienze – Università di Trento
package it.unitn.science.programmazione.calculator;
public abstract class View
{
Controller c=null;
public void setController(Controller c) {this.c=c;}
public abstract void setDisplayValue(String s);
public abstract String getDisplayValue();
public abstract void show();
}
14
Programmazione 2 - Marco Ronchetti
class ViewImplementation
Fac.Scienze – Università di Trento
package it.unitn.science.programmazione.calculator;
import javax.swing.*; import java.awt.*;
public class ViewImplementation extends View {
JFrame frame;
JPanel contentPane=new JPanel();
JPanel numberPanel=new JPanel();
JPanel operationPanel=new JPanel();
GridLayout numberPanelGridLayout=new GridLayout();
JTextField displayField=new JTextField("0");
public ViewImplementation(Controller c) {
setController(c);
setupGraphicEnvironment();
setupGraphics();
}
public void show() { frame.setVisible(true); }
7
7
15
Programmazione 2 - Marco Ronchetti
class ViewImplementation
Fac.Scienze – Università di Trento
public void setDisplayValue(String s) {displayField.setText(s);}
public String getDisplayValue() {return displayField.getText();}
private void setupGraphicEnvironment() {
frame = new JFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
frameSize.height = screenSize.height;
if (frameSize.width > screenSize.width)
frameSize.width = screenSize.width;
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
16
Programmazione 2 - Marco Ronchetti
class ViewImplementation
Fac.Scienze – Università di Trento
private void setupGraphics() {
frame.setSize(new Dimension(310, 320));
//
contentPane=(JPanel)frame.getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.YELLOW);
contentPane.add(displayField);
contentPane.add(operationPanel);
contentPane.add(numberPanel);
//
displayField.setBounds(new Rectangle(25, 15, 245, 30));
displayField.setEditable(false);
8
8
17
Programmazione 2 - Marco Ronchetti
class ViewImplementation
Fac.Scienze – Università di Trento
numberPanel.setBounds(new Rectangle(25, 60, 140, 160));
numberPanelGridLayout.setColumns(3);
numberPanelGridLayout.setRows(4);
numberPanel.setLayout(numberPanelGridLayout);
for (int i=0 ; i<10;i++)
numberPanel.add(new NumberButton(""+i,c));
numberPanel.add(new NumberButton(".",c));
operationPanel.setBounds(new Rectangle(220, 60, 70, 220));
operationPanel.add(new OperationButton("+“,c,
OperationButton.ADDITION));
operationPanel.add(new OperationButton("-",c,
OperationButton.SUBTRACTION));
operationPanel.add(new OperationButton("*",c,
OperationButton.MULTIPLICATION));
18
Programmazione 2 - Marco Ronchetti
Fac.Scienze – Università di Trento
class ViewImplementation
operationPanel.add(new OperationButton("/",c,
OperationButton.DIVISION));
operationPanel.add(new OperationButton("ENTER",c,
OperationButton.ENTER));
operationPanel.add(new OperationButton("C",c,
OperationButton.CLEARALL));
operationPanel.add(new OperationButton("CE",c,
OperationButton.CLEAR));
}
}
9
9
19
Programmazione 2 - Marco Ronchetti
class Controller
Fac.Scienze – Università di Trento
package it.unitn.science.programmazione.calculator;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Toolkit;
public class Controller implements ActionListener {
Model m=null;
View v=null;
boolean refreshDisplay=false;
public void setView(View v) {this.v=v;}
public void setModel(Model m) {this.m=m;}
void initCalc() {
m.cleanAll();
v.setDisplayValue("0");
}
20
Programmazione 2 - Marco Ronchetti
class Controller
Fac.Scienze – Università di Trento
public void actionPerformed(ActionEvent e) {
String command=(String)e.getActionCommand();
if(command.equals(NumberButton.COMMAND)) {
String s=((JButton)e.getSource()).getText();
appendString(s);
}
else if (command.equals(OperationButton.COMMAND)) {
executeOperation((OperationButton)e.getSource());
}
}
10
10
21
Programmazione 2 - Marco Ronchetti
Fac.Scienze – Università di Trento
class Controller
void appendString(String s) {
if (refreshDisplay) {
double val=Double.parseDouble(v.getDisplayValue());
v.setDisplayValue(s);
refreshDisplay=false;
} else {
String currentString=v.getDisplayValue();
String candidateString=currentString+s;
if (candidateString.length()>1)
if (!candidateString.startsWith("0."))
if (candidateString.charAt(0)=='0')
candidateString=candidateString.substring(1);
try {
double val=Double.parseDouble(candidateString);
v.setDisplayValue(candidateString);
} catch (NumberFormatException e) {Toolkit.getDefaultToolkit().beep();}
}
}
22
Programmazione 2 - Marco Ronchetti
class Controller
Fac.Scienze – Università di Trento
void executeOperation(OperationButton ob) {
double opDis=Double.parseDouble(v.getDisplayValue());
m.setMemoria1(opDis);
switch (ob.getOperationType()) {
case OperationButton.CLEAR:
m.setMemoria1(0);
break;
case OperationButton.CLEARALL:
initCalc();
break;
case OperationButton.ADDITION:
m.setMemoria1(m.getMemoria2()+m.getMemoria1());
break;
case OperationButton.SUBTRACTION:
m.setMemoria1(m.getMemoria2()-m.getMemoria1());
break;
11
11
23
Programmazione 2 - Marco Ronchetti
class Controller
Fac.Scienze – Università di Trento
case OperationButton.MULTIPLICATION:
m.setMemoria1(m.getMemoria2()*m.getMemoria1());
break;
case OperationButton.DIVISION:
m.setMemoria1(m.getMemoria2()/m.getMemoria1());
break;
case OperationButton.ENTER:
m.setMemoria2(m.getMemoria1());
}
v.setDisplayValue(""+m.getMemoria1());
refreshDisplay=true;
}
}
24
Programmazione 2 - Marco Ronchetti
Model – View – Controller Architecture
Fac.Scienze – Università di Trento
View
Pagine
Web
Dinamiche
Ad-hoc
clients
Controller
Model
Classi
Che
Implementano
Business
Logic
DB
Three-tier architecture
12
12