Forum - Java
- Bottoni
Pagine: [ 1 2 ]
|
ht-never (Member )
Rookie
Messaggi: 49
Iscritto: 24/09/2011
sto sviluppando un programma e mi piacerebbe creare un bottone personalizzato... solo che se creo un bottone che estende AbstractButton quando ci clicco sopra non succede nulla.
Codice sorgente - presumibilmente Java
package glow ;
import javax.swing.* ;
import java.awt.* ;
@SuppressWarnings( "serial" )
{
{
this .color = c;
this .string = s;
this .borderColor = border;
}
{
this .string = s;
}
{
this .color = c;
}
{
this .color = c;
setPreferredSize( dim) ;
}
{
super .paintComponent ( g) ;
graph = g;
paintInDeviceCoords( g) ;
}
public void paintInDeviceCoords
( Graphics g
) {
/* assegna modo di riempimento sfumato */
g.setColor ( color) ;
g.fillRect ( 0,0,( int ) dim.getWidth ( ) ,( int ) dim.getHeight ( ) ) ;
g.
setFont ( new Font ( g.
getFont ( ) .
getFontName ( ) ,
Font .
PLAIN ,18
) ) ; g.drawString ( string,5,20) ;
g.setColor ( borderColor) ;
g.drawLine ( string.length ( ) * 9,0,string.length ( ) * 9,27) ;
g.drawLine ( 0,0,0,27) ;
g.drawLine ( 0,0,string.length ( ) * 9,0) ;
g.drawLine ( string.length ( ) * 9,28,0,28) ;
g.drawLine ( 0,0,0,28) ;
}
public static void main
( String [ ] args
) {
XButton ed
= new XButton
( "Hello!" ,
Color .
gray ,
Color .
red ) ; ed.
addActionListener ( new Action ( ) ) ; XFrame fr = new XFrame( ) ;
fr.setLayout ( null ) ;
fr.setBounds ( 300,300,300,300) ;
ed.setBounds ( 100,100,300,300) ;
fr.add ( ed) ;
fr.setVisible ( true ) ;
}
} ;
GrG (Member )
Guru^2
Messaggi: 3430
Iscritto: 21/08/2007
premetto di essere molto molto molto inesperto in ambito java.. ma guardando il codice mi viene un dubbio...
sinceramente non vedo nessun metodo che si occupa di gestire il click del pulsante qualcosa tipo OnClick.. sicuro che non manchi?
Bonny (Member )
Expert
Messaggi: 437
Iscritto: 24/04/2009
è una classe astratta... non si può fare un istanza di una classe astratta...
non potresti semplicemente estendere la classe JButton ?!?!
GrG ha ragione non vedo nessun metodo che gestisce l'evento click?!
a meno che non sia scritto nella classe Action (riga 68)
Il Totem (Admin )
Guru^2
Messaggi: 3635
Iscritto: 24/01/2006
Scrivi il codice dell'ActionListener.
ht-never (Member )
Rookie
Messaggi: 49
Iscritto: 24/09/2011
@bonny ma io non ho fatto l'istanza di una classe astratta, ma di una classe che la estende e che ne sviluppa tutti i metodi, giusto?
@It Totem non ho scritto il codice dell'ActionListener perchè non mi sembrava rilevante, il problema è eseguirlo questo listener... comunque è contenuto nel file Action.java, che è...
Codice sorgente - presumibilmente Java
package glow ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
System .
out .
println ( "prova 1,2,3..." ) ; }
}
Bonny (Member )
Expert
Messaggi: 437
Iscritto: 24/04/2009
per curiosità prova a scrivere nel main
Codice sorgente - presumibilmente Plain Text
eb.requestFocusInWindow();
ht-never (Member )
Rookie
Messaggi: 49
Iscritto: 24/09/2011
Non funziona... semplicemente non vuole comportarsi come un bottone... se faccio extends JButton la situazione degenera, mentre se estendo AbstractButton non è un bottone...
Bonny (Member )
Expert
Messaggi: 437
Iscritto: 24/04/2009
cosi funziona cmq il metodo paintInDeviceCoords(Graphics g) è inutile basta scrivere quel codice dentro paintComponent()
Codice sorgente - presumibilmente Java
import javax.swing.* ;
import java.awt.* ;
public class XButton
extends JButton {
this .color = c;
this .string = s;
this .borderColor = border;
this .
dim = new Dimension ( 64, 30
) ; // 64 = 9 * s.lenght this .setPreferredSize ( this .dim ) ;
}
this .string = s;
//setPreferredSize(new Dimension(300, 200));
}
public XButton
( Color c
) { this .color = c;
//setPreferredSize(new Dimension(300, 200));
}
this .color = c;
//setPreferredSize(dim);
}
@Override
public void paintComponent
( Graphics g
) { super .paintComponent ( g) ;
graph = g;
paintInDeviceCoords( g) ;
}
public void paintInDeviceCoords
( Graphics g
) { /*
* assegna modo di riempimento sfumato
*/
g.setColor ( color) ;
g.fillRect ( 0, 0, ( int ) dim.getWidth ( ) , ( int ) dim.getHeight ( ) ) ;
g.
setFont ( new Font ( g.
getFont ( ) .
getFontName ( ) ,
Font .
PLAIN , 18
) ) ; g.drawString ( string, 5, 20) ;
g.setColor ( borderColor) ;
g.drawLine ( string.length ( ) * 9, 0, string.length ( ) * 9, 27) ;
g.drawLine ( 0, 0, 0, 27) ;
g.drawLine ( 0, 0, string.length ( ) * 9, 0) ;
g.drawLine ( string.length ( ) * 9, 28, 0, 28) ;
g.drawLine ( 0, 0, 0, 28) ;
}
public static void main
( String [ ] args
) { XButton ed
= new XButton
( "Hello!" ,
Color .
gray ,
Color .
red ) ; //System.out.println("ciao");
ed.addActionListener ( ac) ;
//fr.setLayout(null);
fr.setBounds ( 300, 300, 300, 300) ;
//ed.setBounds(100, 100, 100, 100);
p.add ( ed) ;
fr.getContentPane ( ) .add ( "Center" ,p) ;
fr.
setDefaultCloseOperation ( JFrame .
EXIT_ON_CLOSE ) ; fr.setVisible ( true ) ;
}
}
la classe Action lo lasciata com'era
ht-never (Member )
Rookie
Messaggi: 49
Iscritto: 24/09/2011
Grazie!
solo che adesso per farlo funzionare devo prima creare un JPanel, c'è un modo per saltare il passaggio?