2.swing - unife · javax.swing javax.swing.plaf.basic javax.swing.text.html javax.accessibility...

21
!"#$%&# () (*(+,% -.%+! ( #., !"#$ !&'() *&++,+&- !"#$%&'"(()("*+,#$'"&$- -,"/0(+,% 1#*# 2(" 34 -*%3/224 )% %+,("$#&&( /,(+,( ( -("*%5% )% "(,( ( 34"4 #223%&#5%4+( 6789:;9 ./'&01 22 3&44+,(" 2565 "+& 66$55 7 68$85 9&+:";&01 2< 3&44+,(" 2565 "+& 66$55 7 68$85 ./'&01 6 =,+>" 2565 "+& 66$55 7 68$85 ?/#& ;& ;&>("'( )"'" '&; ;,4"+,@"+(" 0( A'3"+=,B:, C+,'0&$

Upload: others

Post on 17-Mar-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

!"#$%&#'()'(*(+,%''-.%+!'('#.,'

!"#$%!&'()%*&++,+&-%%

!"#$%&'"(()("*+,#$'"&$-.

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

6789:;9'

•  ./'&01%22%3&44+,("%2565 %% %% %"+&%66$55%7%68$85%•  9&+:";&01%2<%3&44+,("%2565%% %"+&%66$55%7%68$85%%•  ./'&01%6%=,+>"%2565 % %% % %"+&%66$55%7%68$85%

?/#&%;&%;&>("'(%)"'"%'&;%;,4"+,@"+("%0(%A'3"+=,B:,%C+,'0&$%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

<=>:?7;@'

•  DEAFCG%,+:H(@&#/+,%&%I&+,+:H(,%

•  J"=K"'&'B%K+(':(K,;(G%L*+,=&M%LN,'&;M%C+,KH(:)%•  C&)B"'&%0&I;(%&O&'B%•  P;@&+("+(%&)&=K(%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

1<A<'$:B;C<@:;'&6<DD7D'

•  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I+,_:(%:H&%:"=K+&'0&%@/#X"+,%(;%L!^%)"'"%

,%I+,'0(%;('&&%(%)&I/&'B%0/&G%

•  2<JH<>7'O<A<P<EKF''

–  (;%K+(="%K,:V,I&%I+,_:"%RL,O,%6$5S`%

–  ('0(K&'0&'@&%0,;;,%K(,#,3"+=,$$$%"%\/,)(a%

•  2<JH<>7'O<A<QPDE9;>F'

–  (;%'/"O"%K,:V,I&%I+,_:"%RL,O,%2b%O&+)("'&%K+&;(=(',+&%0,%L,O,%6$6$cS`%

–  ):+(#"%&))"%)@&))"%('%L,O,M%+&,;=&'@&%('0(K&'0&'@&%0,;;,%K(,#,3"+=,%

D:&'0(,="%K(d%('%0&#,I;("%'&;%K,:V,I&%)[('I$%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

1<A<'2<JH<>7F'-.%+!'•  A;%N,:V,I&%0(%D[('I%W%K"@&'@&M%e&))(4(;&%&%A99fFDU$%

javax.swing.undo javax.swing.table javax.swing.filechooser

javax.swing.tree javax.swing.plaf.synth javax.swing.event

javax.swing.text.rtf javax.swing.plaf.multi javax.swing.colorchooser

javax.swing.text.html.parser javax.swing.plaf.metal javax.swing.border

javax.swing.text.html javax.swing.plaf.basic javax.swing

javax.swing.text javax.swing.plaf javax.accessibility

•  Il numero dei package disponibili è alto, molti programmi usano un piccolo subset di package disponibili.

•  Per partire ne bastano 2: –  javax.swing –  javax.swing.event (non sempre richiesto).

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'#"&R%,(,,/"#'

•  D[('I%0&_'():&%/',%I&+,+:H(,%0(%:;,))(%:H&%3"+'():"'"%"I'(%BK"%0(%:"=K"'&'@&%I+,_:"%

–  _'&)@+&M%K,''&;;(M%3+,=&M%4"#"'(M%,+&&%0(%@&)@"M%:H&:V4"gM%;()@&%,%0():&),M%&@:%&@:%

•  N+"I+,==,>("'&%h&O&'@70+(O&'iG%

–  '"'%K(d%,;I"+(@=(%)B;&%('K/@j&;,4"+,>("'&j"/@K/@$$$%

–  $$$%=,%+&,>("'&%,I;(%&O&'B%:H&%;X/@&'@&M%('%="0"%('@&+,-O"M%I&'&+,%)/(%:"=K"'&'B%I+,_:(%

•  J"':&-%0(%&O&'@"%&%0(%,):";@,@"+&%0&I;(%&O&'B%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'!("#"&R%#'.,%I&+,+:H(,%0&;;&%:;,))(%I+,_:H&%0&;%k,O,G%

Component

Container

Window

JFrame JDialog

Frame Dialog

JComponent

JLabel

JList

JMenuBar

JPanel

JScrollBar

JTextComponent

JTextField JTextArea

AbstractButton Object

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'!("#"&R%#'.,%I&+,+:H(,%0&;;&%:;,))(%0(%I+,_:H&%0&;%k,O,G%

JMenuItem

JButton

JToggleButton

JRadioButton JCheckBox

AbstractButton

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'&4024+(+,%'2"%+&%2#3%'

•  &:;K<9;7=F%@/-%(%:"=K"'&'B%K+(':(K,;(%)"'"%:"'@&'(@"+($%

•  .9;C:EF%;&%_'&)@+&%)"'"%0&(%:,)(%K,+B:";,+(%0(%:"'@&'(@"+(M%:(%)"'"%*+,=&%&%_'&)@+&%0(%!(,;"I"$%

•  1$=<?7F%W%(;%:"=K"'&'@&%:H&%,))":(,="%I&'&+,;=&'@&%,;;,%_'&)@+,%K+(':(K,;&%0(%/'%K+"I+,==,%:"'%('@&+3,::(,%I+,_:,$%

•  1&:?S:;7;KF%\/&)@"%W%(;%I&'&+(:"%:"=K"'&'@&%I+,_:"%0(%)[('I$%RK"))(&0&%(;%=&@"0"%K&+%0()&I',+)(S$%

•  12<;76F%(%K,''&;;(%)"'"%\/&(%:"=K"'&'BM%:H&%H,''"%(;%:"=K(@"%0(%:"'@&'&+&%,;@+(%:"=K"'&'B%&%"+I,'(>>,+;(M%=,%K"))"'"%,':H&%&))&+&%/),B%K&+%=")@+,+&%(==,I('($%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'

•  In Swing non si possono aggiungere nuovi componenti direttamente al JFrame

•  Dentro a ogni JFrame c'è un Container, recuperabile col metodo getContentPane(): è a lui che vanno aggiunti i nuovi componenti

•  Tipicamente, si aggiunge un pannello (un JPanel o una nostra versione più specifica), tramite il metodo add()! –  sul pannello si può disegnare (forme, immagini...)! –  ...o aggiungere pulsanti, etichette, icone, etc

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7D7?S9:T'•  9")@+(,="%:"=&%)(%K/Y%I&'&+,+&%/',%_'&)@+,%K+(':(K,;&M%/B;(>>,'0"%(;%K,:V,I&%

k,O,g$)[('IG%

import javax.swing.*;

public class MyFrame1 { /* Costruttore della classe */ public MyFrame1() { //Istanzio il nuovo oggetto JFrame JFrame frame = new JFrame("Primo esmepio di Frame"); //Setto le dimensioni della finestra frame.setSize(400, 200); //Questo metodo rende la finestra NON ridimensionabile frame.setResizable(false); //Setto la posizione della finestra sullo schermo frame.setLocation(150, 500); //visualizzo la finestra frame.setVisible(true); } //Classe statica Main public static void main(String[] args) { MyFrame1 f = new MyFrame1(); }

}

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7D7?S9:T'

•  f0%&::"%\/,%(;%+()/;@,@"%0&;;X&)&:/>("'&G%

•  F"B,="%:H&%)&%)(%K+&=&%(;%K/;),'@&%0(%:H(/)/+,%0&;;X,KK;(:,>("'&%\/&)@,%+(=,'&%('%&)&:/>("'&$%A;%="BO"%0(%\/&)@"%:"=K"+@,=&'@"%W%:H&%'"'%,44(,="%(')&+(@"%;X&O&'@"%0(%:H(/)/+,$%

•  D&%O"I;(,="%3,+;"%K"))(,="%,II(/'I&+&%;,%+(I,G%–  frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7D7?S9:M'

•  F&;;X&)&=K("%K+&:&0&'@&%'"'%W%)@,@"%/),@,%;X&+&0(@,+(&@]%('3,-%'&;%:")@+/#"+&%'"(%,44(,="%:+&,@"%/'%"II&#"%JFrame$%

•  C&'&+,;=&'@&%W%K+&3&+(4(;&%&)@&'0&+&%;,%:;,))&%L*+,=&%K&+%I&'&+,+&%/'%:"=K"'&'@&%K&+)"',;(>>,@"%K&+%;,%'")@+,%,KK;(:,>("'&$%

•  T&0(,="%\/('0(%/'%&)&=K("%0(%:"=&%)(%K/Y%/),+&%;X&+&0(@,+(&@]%K&+%K+&;&O,+&%0,%L*+,=&%@/#&%;&%:,+,#&+()B:H&%:H&%:(%)&+O"'"%&%,II(/'I&+&%\/&;;&%:H&%O"I;(,="%(')&+(+&%'&;;,%'")@+,%,KK;(:,>("'&$%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7D7?S9:M'

•  l/&)@,%:;,))&%&)@&'0&%L*+,=&M%\/&)@"%)(I'(_:,%:H&%K+&;&O(,="%@/-%(%=&@"0(%&%;&%O,+(,4(;(%0,;;,%)/K&+:;,))&M%&%K"))(,="%+(/B;(>>,+;(%:"'%;,%'")@+,%:H&%)(%:H(,=,%9m*+,=&2$%

import javax.swing.*; public class MyFrame2 extends JFrame {

public MyFrame2(String titolo) { super(titolo); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(50, 50, 150, 500); setVisible(true); }

public MyFrame2() { super(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setBounds(250, 50, 150, 500); setVisible(true); }

public static void main(String[] args) { MyFrame2 finestra1 = new MyFrame2("Prova!"); MyFrame2 finestra2 = new MyFrame2(); }

}

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7D7?S9:M'

•  F&;;X&)&=K("M%K&+%/),+&%;X&+&0(@,+(&@]%W%)@,@"%'&:&)),+("%&)@&'0&+&%;,%:;,))&%JFrame%:"'%/',%'")@+,%:;,))&%K&+)"',;(>>,@,%@+,=(@&%;,%K,+";,%:H(,O&%extendsM%('%\/&)@"%="0"%&+&0(B,="%@/-%(%=&@"0(%&%(%:"=K"+@,=&'B%0&;;,%:;,))&%JFrame%'&;;,%'")@+,%:;,))&$%

•  .X&+&0(@,+(&@]%:(%K&+=&#&%,':H&%0(%:H(&0&+&%,(/@"%,;;,%)/K&+:;,))&%'&;;,%:+&,>("'&%0&(%:")@+/#"+(M%@+,=(@&%;,%K,+";,%:H(,O&%super$%l/&)@,%+(:H(,=,%(;%:")@+/#"+&%0&;;,%:;,))&%JFrame%=,%;X"II&#"%:H&%O(&'&%:+&,@"%W%0(%BK"%MyFrame2M%\/('0(%H,%;&%:,+,#&+()B:H&%0(%/'%JFrame%=,%:"=K+&'0&%,':H&%;&%K&+)"',;(>>,>("'(%:H&%,44(,="%3,#"$%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'

•  P',%L*+,=&%W%:"=K")@,%0,%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,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7'12<;76'•  N+&K,+(,="%/'%L*+,=&%:H&%K"))(&0,%,;%)/"%('@&+'"%/'%LN,'&;G%import javax.swing.*; import java.awt.*; public class MyFrame3 extends JFrame { public MyFrame3(String titolo) { super(titolo); this.setSize(400, 500); Container c = this.getContentPane(); MyPanel pannello = new MyPanel(); //JPanel pannello = new JPanel(); c.add(pannello); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setVisible(true); }

public static void main(String[] args) { MyFrame3 finestra = new MyFrame3("Finestra con Pannello!"); }

}

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7'12<;76'

•  A;%:"0(:&%:H&%,44(,="%=")@+,@"M%'"'%K/Y%&))&+&%&)&I/(@"%,':"+,%K"(:Ho%=,':,%;,%0&_'(>("'&%0&;;,%:;,))&%MyPanel$%D&%O"I;(,="%&)&I/(+&%:"=/'\/&%;X&)&=K("%K"))(,="%@"I;(&+&%(;%:"==&'@"%,;;,%+(I,%:"==&'@,@,%&%(')&+(+;"%('%\/&;;,%K+&:&0&'@&$%T&0+&="%/',%JFrame%:"'%/'%K,''&;;"%:"'%)3"'0"%I+(I("%R&),#,=&'@&%:"=&%'&;%K+(="%&)&=K("$S`%

•  N+"O(,="%"+,%,%&)@&'0&+&%;,%:;,))&%JPanel%&%,'0,+&%,%)"O+,):+(O&+&%(;%=&@"0"%:H&%O(&'&%/B;(>>,@"%K&+%(;%0()&I'"%0(%\/&)@"%:"=K"'&'@&G%% paintComponent(Graphics g);

•  .X"II&#"%Graphics%3,%K,+@&%0&;%K,:V,I&%java.awt%&0%W%=";@"%/B;&%K&+%(;%0()&I'"%0(%"II&-M%)"K+,#/#"%'&;;,%)/,%&)@&')("'&%Graphics2D$%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7'12<;76'

•  .,%)@+/#/+,%4,)&%0&;;,%:;,))&%),+]%;,%)&I/&'@&G%import javax.swing.JPanel; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; public class MyPanel extends JPanel {

public MyPanel() { super(); setBackground(Color.WHITE); } …

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7'12<;76'•  ZII(/'I(,="%;X(=K;&=&'@,>("'&%0&;%=&@"0"G%

''public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLUE); g.fillRect(5, 10, 100, 130); g.setColor(Color.ORANGE); g.fillRoundRect(110, 10, 280, 100, 20, 20); g.setColor(Color.RED); g.drawOval(50, 100, 150, 150); g.setColor(Color.GREEN); g.fillOval(150, 300, 150, 100); g.setColor(Color.BLACK); g.drawLine(50, 430, 300, 430); g.drawLine(30, 330, 270, 280); g.setColor(Color.WHITE); g.setFont(new Font("Garamond", Font.BOLD, 50)); g.drawString("Panel Test", 110, 75); g.setColor(Color.CYAN); int[] x = new int[]{380, 315, 210, 340}; int[] y = new int[]{200, 260, 220, 225}; g.drawPolyline(x, y, 4); } }

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7'12<;76'

•  A;%+()/;@,@"%0&;;X&)&:/>("'&%W%(;%)&I/&'@&G%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'1$=<?7'7'12<;76'

•  Z44(,="%O()@"%:"=&%K&+%0()&I',+&%)(,%)@,@"%/B;(>>,@"%;X"II&#"%Graphics$%l/&)@"%K"))(&0&%=";B%=&@"0(M%:H&%K"))"'"%&))&+&%)/00(O()(%('%0/&%I+,'0(%I+/KK(G%– 9&@"0(%0(%0()&I'"%&%+(&=K(=&'@"%:H&%)&+O"'"%K&+%0()&I',+&%R('%I&+I"%h@"%+&'0&+iS%0&;;&%3"+=&%4,)&M%0&;%@&)@"%&%0&;;&%(==,I('($%

– 9&@"0(%K&+%(;%)&-'I%0(%,#+(4/B%:H&%="0(_:,'"%:"=&%,OO&'I"'"%(;%0()&I'"%&%(;%+(&=K(=&'@"$%N&+%&)&=K("%(%=&@"0(%setFont%&%setColor$%

•  A'%+&,;@]%@/-%I;(%"II&-%Graphics%0&;;,%javax.swing%)"'"%"II&-%Graphics2DM%=";@"%K(d%,O,'>,@"%0(%GraphicsM%&%K"@&'@&$%T(&'&%=,'@&'/@"%Graphics%'&;;,%_+=,%0&(%=&@"0(%K&+%:"=K,B4(;(@]%@+,%O&+)("'($%%%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'!=<SU9JDM)'

•  F&;;,%:;,))&%)"'"%K+&)&'B%0&(%p&'0&+('I%q('@)M%"OO&+"%0&(%=&@"0(%K&+%)&#,+&%,;:/'(%K,+,=&@+(%:H&%)(%"::/K,'"%0(%=(I;("+,+&%;,%O()/,;(>>,>("'&M%0(%O&;":(>>,+;,%&%0(%:,=4(,+&%K&+%&)&=K("%(;%BK"%0(%+(&=K(=&'@"$%

•  N&+%&)&=K("%)(%K/Y%)&#,+&%;,%K+"K+(&@]%0(%Z'B,;(,)('I%0&;%@&)@"%:H&%K&+%0(=&')("'(%0(%3"'@%@+"KK"%I+,'0(%"%@+"KK"%K(::";&M%+()/;@,%(;;&II(4(;&$% public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; RenderingHints rh = new RenderingHints( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setRenderingHints(rh); }

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'!=<SU9JDM)'•  E’ possibile utilizzare le primitive del Graphics2D anche per fare

grafica CAG (Constructive Area Geometry) ovvero per ottenere forme complesse utilizzando forme più semplici combinate con logica booleana.

Unione Sottrazione

Intersezione Or esclusivo

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'!=<SU9JDM)'

•  ?+,=(@&%,;@+&%K+(=(BO&%0&_'(@&%0,;%L,O,M%W%K"))(4(;&%/B;(>>,+&%;,%I+,_:,%2!%K&+%&)&I/(+&%@+,)3"+=,>("'(%)/;;&%_I/+&%0()&I',@&%:"=&%+"@,>("'(M%@+,);,>("'(M%)B+,=&'B%0&;;&%_I/+&%&%,':H&%/B;(>>,+&%@+,)K,+&'>&%&%+(&=K(=&'B%,%I+,0(&'@&%"KK/+&%,%@&g@/+&$%

•  A;%+(&=K(=&'@"%,%I+,0(&'@&%/B;(>>,%2%:";"+(%0(%4,)&%,))":(,B%,%2%K/'B%)K,>(,;($%D3/=,%(;%K+(="%:";"+&%=&'@+&%)(%=/"O&%O&+)"%(;%)&:"'0"%K/'@"%'&;%0()&I'"%0&;;X(==,I('&$%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'•  *('"+,%)(%)"'"%/B;(>>,@&%,;:/'&%0&;;&%3/'>("',;(@]%0(%)[('IM%=,%;X('@&+,>("'&%:"'%

;X/@&'@&%'"'%W%)@,@,%K+&),%('%:"')(0&+,>("'&$%

•  UI'(%,KK;(>,>("'&%0&O&%K"@&+%('@&+,I(+&%:"'%;X/@&'@&M%&%K&+%3,+&%\/&)@"%0&O&%&))&+&%+&,-O,%)/%,;:/'(%:"=K"'&'BM%:H&%\/('0(%0&O"'"%&))&+&%('%I+,0"%0(%0&@&+=(',+&%(;%K+"K+("%)@,@"%&%;,':(,+&%"KK"+@/'(%)&I',;(%\/,'0"%\/&)@"%O(&'&%="0(_:,@"%0,;;X/@&'@&$%

•  UI'(%:"=K"'&'@&%I+,_:"M%\/,'0"%)(%"K&+,%)/%0(%&))"M%I&'&+,%/'%&O&'@"%:H&%0&):+(O&%:"),%W%,::,0/@"$%

•  ?(K(:,=&'@&M%"I'(%:"=K"'&'@&%K/Y%I&'&+,+&%=";B%BK(%0(O&+)(%0(%&O&'BM%('%+&;,>("'&%,%:(Y%:H&%)@,%,::,0&'0"%

–  /'%4"#"'&%K/Y%I&'&+,+&%;X&O&'@"%h,>("'&i%:H&%)(I'(_:,%:H&%W%)@,@"%K+&=/@"$%

–  /',%:,)&;;,%0(%"K>("'&%K/Y%I&'&+,+&%;X&O&'@"%h="0(_:,@"i%K&+%('0(:,+&%:H&%;,%:,)&;;,%W%)@,@,%)&;&>("',@,%j%0&)&;&>("',@,$%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'

•  A'%L,O,M%/'%&O&'@"%W%/'%"II&#"M%()@,'>,%0(%R/',%)"#":;,))&%0(S%O<A<PB@6P(A7;K4GO7JK'

•  T&0(,="%;,%I&+,+:H(,%0&;;&%:;,))(%K&+%I;(%&O&'BG%

java.util.EventObject

java.awt.AWTEvent

ActionEvent

KeyEvent MouseEvent

InputEvent WindowEvent

ComponentEvent

package java.awt.event

AdjustmentEvent TextEvent ItemEvent

FocusEvent PaintEvent ContainerEvent

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'•  P'%="0"%)&=K;(:&%K&+%0&_'(+&%/'%=&@"0"%0(%h,):";@,+&i%I;(%&O&'B%:H&%K"))"'"%

&))&+&%I&'&+,B%0,%/'%:"=K"'&'@&%W%\/&;;"%0(%(=K;&=&'@,+&%/'%ActionListener$%

•  N&+%):+(O&+&%/'%ActionListerer%4()"I',%)&I/(+&%(%)&I/&'B%K,))(G%–  !(:H(,+,+&%/'%I&)@"+&%0(%&O&'B%R&O&'@%H,'0;&+S%:H&%(=K;&=&'@,%;X('@&+3,::(,%ActionListenerM%"KK/+&%&)@&'0&%/',%:;,))&%:H&%,%)/,%O";@,%;X('@&+3,::(,%ActionListener$%

%public class MyClass implements ActionListener {

–  p&I()@+,+&%/',%()@,'>,%0&;;,%:;,))&%I&)@"+&%0(%&O&'B%:"=&%;()@&'&+%0(%/'"%"%K(d%&O&'B$%

%someComponent.addActionListener(instanceOfMyClass); –  A':;/0&+&%(;%:"0(:&%:H&%(=K;&=&'@,%;X('@&+3,::(,%Z:B"'.()@&'&+%K&+%I&)B+&%

;X&O&'@"$%%public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'B;'7D7?S9:'N&+%K+"O,+&%,%I&)B+&%\/,;:H&%&O&'@"M%K+"O(,="%,%):+(O&+&%/'%

K+"I+,==,%:H&%(=K;&=&'B%/',%)&=K;(:&%:,;:";,@+(:&$%

#=JU9K7VB=<G%%%

•  /'%K,''&;;"%:"'%/'%:,=K"%0(%@&)@"%&%)&(%K/;),'B%

•  /'%/'(:"%ActionListener%%K&+%@/-%(%K/;),'B%RW%(;%O&+"%:,;:";,@"+&S`%

!7D@:;7'C7>69'7A7;@G%UI'(%O";@,%:H&%)(%K+&=&%/'%K/;),'@&G%•  )(%+&:/K&+,%(;%'"=&%0&;%K/;),'@&%RW%;,%)/::&))(O,%"K&+,>("'&%0,%)O";I&+&S`%•  )(%;&II&%(;%O,;"+&%'&;%:,=K"%0(%@&)@"%•  )(%)O";I&%;r"K&+,>("'&%K+&:&0&'@&%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'B;'7D7?S9:'•  N,+B,="%0,;;X('@&+3,::(,%I+,_:,G%•  N&+%:+&,+&%(%4"#"'(M%,44(,="%4()"I'"%0(%/',%:;,))&%:H&%:(%K&+=&#,%0(%3,+;"G%LQ/#"'$%import javax.swing.JButton; import java.awt.Font; public class CalcButton extends JButton {

public CalcButton(String n) { super(n); setFont(new Font("Bitstream Vera Sans Mono", Font.BOLD, 20)); }

}

•  J+&(,="%"+,%(;%0()K;,m%0&;;,%:,;:";,@+(:&G%import javax.swing.JTextField; import java.awt.*; public class CalcDisplay extends JTextField { public CalcDisplay(int n) { super(n);

setFont(new Font("Bitstream Vera Sans Mono", Font.PLAIN, 30)); }

}

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'B;'7D7?S9:'•  Q()"I',%(=K;&=&'@,+&%;,%*+,=&%0&;%:,;:";,@"+&G%

%import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.JPanel; import java.awt.Container; public class Calculator extends JFrame { public Calculator() { super(); setBounds(200, 100, 400, 120); JPanel ui = createUI(); Container c = this.getContentPane(); c.add(ui); setTitle("Mini Calcolatrice"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); setVisible(true); } public static void main(String[] args) { Calculator calc = new Calculator(); } }

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'B;'7D7?S9:'•  9&@"0"%K&+%:+&,+&%;X('@&+3,::(,%I+,_:,G%

% %%

private JPanel createUI() { JPanel pannello = new JPanel(); CalcDisplay testo = new CalcDisplay(15); testo.setHorizontalAlignment(JTextField.RIGHT); CalcButton calc = new CalcButton("="); CalcButton canc = new CalcButton("C"); CalcButton sum = new CalcButton("+"); CalcButton sub = new CalcButton("-"); CalcButton div = new CalcButton("/"); CalcButton mul = new CalcButton("*"); pannello.add(testo); pannello.add(sum); pannello.add(sub); pannello.add(div); pannello.add(mul); pannello.add(calc); pannello.add(canc); CalcEngine engine = new CalcEngine(testo); sum.addActionListener(engine); sub.addActionListener(engine); mul.addActionListener(engine); div.addActionListener(engine); calc.addActionListener(engine); canc.addActionListener(engine); return pannello; } % % %%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'B;'7D7?S9:'•  J+&,>("'&%0&;%="@"+&%:H&%,):";@,%;&%,>("'(%&0%&)&I/&%(%:,;:";(G%import java.awt.event.*; public class CalcEngine implements ActionListener { public CalcEngine(CalcDisplay dis) { display = dis; precOp = "nop"; }

public void actionPerformed(ActionEvent ev) { ...//Implementazione del metodo... } private CalcDisplay display; private double result; private String precOp;

}

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'B;'7D7?S9:' public void actionPerformed(ActionEvent ev) { double valore = Double.parseDouble(display.getText()); display.setText(""); display.requestFocus(); String operazione = ev.getActionCommand(); if (operazione.equals("C")) { result = 0; valore = 0; precOp = "nop"; } else { if (precOp.equals("+")) { result += valore; } else if (precOp.equals("-")) { result -= valore; } else if (precOp.equals("*")) { result *= valore; } else if (precOp.equals("/")) { result /= valore; } else if (precOp.equals("nop")) { result = valore; } display.setText("" + result); precOp = operazione;

} }

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

-E9;>F'(A7;@'B;'7D7?S9:'•  A;%+()/;@,@"%0"O+&44&%&))&+&%(;%)&I/&'@&G%

•  Z44(,="%/B;(>>,@"%/'%/'(:"%I&)@"+&%0(%&O&'@"%K&+%K+&;&O,+&%&%I&)B+&%@/-%I;(%&O&'B%0&(%K/;),'B%:H&%,44(,="%:+&,@"$%

•  ?+,=(@&%(;%=&@"0"%String getActionCommand();%%%,44(,="%K+&;&O,@"%(;%'"=&%0&;%4"#"'&%:H&%&+,%+&)K"'),4(;&%0&;;X&O&'@"$%

•  J(%)"'"%K,#&+'%0(%K+"I+,==,>("'&%=(I;("+(%0(%\/&)@"$%

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

/6K7=9:=9'7D7?S9'T'Esempio di utilizzo dei fondamentali componenti di SWING.

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

/6K7=9:=9'7D7?S9'T'

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

/6K7=9:=9'7D7?S9'T'

-,"/0(+,%'1#*#'2("'34'-*%3/224')%'%+,("$#&&('/,(+,('('-("*%5%')%'"(,('('34"4'#223%&#5%4+('

/6K7=9:=9'7D7?S9'M'

Applicazione per il disegno di funzioni.