Questo è un programmino stupido, che pero' mi da' errore. E se mi da' errore e non trovo soluzione, chiedo a voi, esperti.
Questa applicazione fa si' che scrivendo il nome di un colore o il codice rrggbb in un JTextField e cliccando su un JButton il colore di sfondo del JButton cambi nel colore scritto nel JTextField.
Ecco il codice:
codice:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test extends JFrame implements FocusListener{
public static JButton campo;
public static JTextField colcampo;
test(){
super("Qua testero' varie applicazioni modificando continuamente il codice.");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,500,500);
JPanel pan = new JPanel();
JLabel eticc = new JLabel("Scrivi colore");
JTextField colcampo = new JTextField(15);
JButton campo = new JButton("Cambia colore");
pan.add(eticc);
pan.add(colcampo);
pan.add(campo);
campo.addFocusListener(this);
setContentPane(pan);
}
public void focusGained(FocusEvent fg){
Object srcfg = fg.getSource();
test t2 = new test();
if(srcfg == t2.campo && isPressed(t2.campo)){
t2.campo.setBackground(t2.colcampo);
}
}
public void focusLost(FocusEvent fl){
Object srcfl = fl.getSource();
}
public static void main(String[] arg){
test t = new test();
t.setVisible(true);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
catch(Exception e){
System.out.println("Errore nel definire il Look del sistema per l\'applicazione");
System.exit(0);
}
}
}
Errore
test.java:27: cannot resolve symbol
symbol : method isPressed (javax.swing.JButton)
location: class test
if(srcfg == t2.campo && isPressed(t2.campo)){
^
test.java:28: cannot resolve symbol
symbol : method setBackground (javax.swing.JTextField)
location: class javax.swing.JButton
t2.campo.setBackground(t2.colcampo);
^
2 errors
Ciao e grazie a chi mi mostrerà disponibilita'.