Ciao a tutti,
ho fatto questa semplice interfaccia grafica. La compilo e mi segnala 3 errori, ma poi produce un file class funzionante....che dite?
codice:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonTest3{ public static void main(String[] args){ ButtonFrame frame = new ButtonFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ButtonFrame extends JFrame{ public ButtonFrame(){ setTitle("ButtonTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); //aggiunge il pannello al frame ButtonPanel panel = new ButtonPanel(); add(panel); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; } class ButtonPanel extends JPanel implements ActionListener{ public ButtonPanel(){ // crea i pulsanti JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); // aggiunge i pulsanti al pannello add(yellowButton); add(blueButton); add(redButton); // associa le azioni ai pulsanti yellowButton.addActionListener(this); blueButton.addActionListener(this); redButton.addActionListener(this); } public void actionPerformed(ActionEvent event){ Object source = event.getSource(); if(source == yellowButton) setBackground(Color.YELLOW); else if(source == blueButton) setBackground(Color.BLUE); else if(source == redButton) setBackground(Color.RED); } }

Rispondi quotando