Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    1,965

    [JAVA] Swing, eventi awt

    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'.

  2. #2
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    1,965
    codice:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class test extends JFrame implements MouseListener{
    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.addMouseListener(this);
    setContentPane(pan);
    }
    
    public void mouseClicked(MouseListener fg){
    Object srcfg = fg.getSource();
    test t2 = new test();
    if(srcfg == t2.campo && isPressed(t2.campo)){
    t2.campo.setBackground(t2.colcampo);
    }
    }
    
    
    
    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:5: test should be declared abstract; it does not define mouseClicked(java.awt.event.MouseEvent) in test
    public class test extends JFrame implements MouseListener{
    ^
    test.java:25: cannot resolve symbol
    symbol : method getSource ()
    location: interface java.awt.event.MouseListener
    Object srcfg = fg.getSource();
    ^
    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);
    ^
    4 errors

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    1,965
    Bla bla bla bla.
    L'unico errore adesso e':


    test.java:48: cannot resolve symbol
    symbol : method setBackground (javax.swing.JTextField)
    location: class javax.swing.JButton
    t2.campo.setBackground(t2.colcampo);
    ^
    1 error



  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2003
    Messaggi
    1,965
    Ho cambiato in

    codice:
    public void mousePressed(MouseEvent fg){
    Object srcfg = fg.getSource();
    test t2 = new test();
    if(srcfg == t2.campo){
    t2.campo.setBackground(Color.t2.colcampo);
    }
    }
    ma l'errore rimane!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.