Salve,sto seguendo un libro per studiare un po' awt e swing,in un esercizio mi riporta questo codice:
// Fig. 13.10: ButtonTest.java
// Creating JButtons.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
private JButton plainButton, fancyButton;
// set up GUI
public ButtonTest()
{
super( "Testing Buttons" );
// get content pane and set its layout
Container container = getContentPane();
container.setLayout( new FlowLayout() );
// create buttons
plainButton = new JButton( "Plain Button" );
fancyButton = new JButton( " Fancy Button");
container.add( plainButton );
container.add( fancyButton );
// create an instance of inner class ButtonHandler
// to use for button event handling
ButtonHandler handler = new ButtonHandler();
fancyButton.addActionListener( handler );
plainButton.addActionListener( handler );
setSize( 275, 100 );
setVisible( true );
} // end ButtonTest constructor
public static void main( String args[] )
{
ButtonTest application = new ButtonTest();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
// inner class for button event handling
private class ButtonHandler implements ActionListener {
// handle button event
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog( ButtonTest.this,
"Hai premuto: " + event.getActionCommand() );
}
} // end private inner class ButtonHandler
}
ho modificato l'esempio creando un secondo pulsante come il plain button(l'altro all'inizio era un'immagine) ora vorrei che i due pulsanti eseguissero azioni differenti,mentre al momento se clicco uno o l'altro esegue la stessa azione.Per esempio vorrei che se venisse premuto il plain dica "hai premuto il plain" se invece venisse premuto il fancy dica "2+2 fa 4".Ovviamente è solo un esempio.Avete qualche link chiaro a riguardo?Grazie.
Altra cosa vorrei sapere come dare il nome ad un pulsante esempio "Avvia" e una volta premuto cambiarne il nome con "Disattiva" ed ovviamente eseguire una seconda azione ma differente.
Grazie a chi mi aiuterà!E buona giornata!![]()

Rispondi quotando