Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 14

Discussione: Primo approccio Swing

  1. #1

    Primo approccio Swing

    studio.jpg Il jar si apre mostrando l'immagine di fianco, ma mi domando come mai essendo 4 Kb impiega un bel pò di secondi per aprirsi. E' possibile ottimizzare qualcosa? Sarà il fatto che le icone vanno prima scaricate dall'URL e poi ridimensionate?

    codice:
    public class MyProgram {
        
        private String pathjava= "C:\\Users\\Paolo\\Desktop\\jjj.gif";  
        private URL pathpython= new URL("http://screenshot.it.sftcdn.net/it/scrn/69000/69838/python-20.jpg");    
        private URL pathmips= new URL("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQi_VaanDQyfTAph9BNO0NF7sN9ZR4Ven9LM9W_uiBbAYc5oVq7Pg");
        private URL pathenglish= new URL("http://upload.wikimedia.org/wikipedia/commons/3/3a/English_Speech_balloon.png");
    
    
        public MyProgram() throws MalformedURLException{start();};
        
        public void start() throws MalformedURLException{
            JFrame f = new JFrame("STUDIO"); 
            f.setSize(new Dimension(240,260));    //dimensioni frame 
            f.setLocation(300, 300);            //locazione frame
            
            Container c = f.getContentPane();    
            JPanel panel = new JPanel();    
            panel.setBackground(new Color(1));
            c.add(panel);                        
            Image im = new ImageIcon(pathjava).getImage();
            im = im.getScaledInstance(100, 100, Image.SCALE_DEFAULT);  //riscalatura immagine
            ImageIcon java = new ImageIcon(im);
            Image im2 = new ImageIcon(pathpython).getImage();
            im2 = im2.getScaledInstance(100, 100, Image.SCALE_DEFAULT);
            ImageIcon python = new ImageIcon(im2);
            Image im3= new ImageIcon(pathmips).getImage();
            im3 = im3.getScaledInstance(100, 100, Image.SCALE_DEFAULT);
            ImageIcon mips = new ImageIcon(im3);
            Image im4 = new ImageIcon(pathenglish).getImage();
            im4 = im4.getScaledInstance(100, 100, Image.SCALE_DEFAULT);
            ImageIcon english = new ImageIcon(im4);
            JButton j = new JButton(java);
            JButton p = new JButton(python);
            JButton m = new JButton(mips);
            JButton e = new JButton(english);
            j.setPreferredSize(new Dimension(100,100));        //dimensioni del bottone
            p.setPreferredSize(new Dimension(100,100));        
            m.setPreferredSize(new Dimension(100,100));        
            e.setPreferredSize(new Dimension(100,100));        
            panel.add(j);                                    //aggiunge i bottoni al pannello
            panel.add(p);
            panel.add(m);
            panel.add(e);
            f.setVisible(true);                      // mostra il frame
        }
        
        public static void main(String[] args) throws MalformedURLException {
            new MyProgram();
        }
    }

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da paolovox Visualizza il messaggio
    Sarà il fatto che le icone vanno prima scaricate dall'URL
    Sì, visto che le immagini arrivano da internet, il maggior tempo perso è sicuramente lì e dipende ovviamente anche dalla tipologia di connessione a internet.
    Il tempo perso nel ridimensionamento è sicuramente "trascurabile", cioè non è sicuramente quello che impatta negativamente.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    I Listener vanno implementati sempre come inner class o esiste qualche altro metodo? Poi ho inserito un MouseListener che cliccando col destro su un bottone dovrebbe aprire soltanto una JTextField ma non funge. Mi domando ci manca tutto il blocco alle spalle del JFrame?

  4. #4
    Per essere più chiaro:
    codice:
    @Override
    			public void mouseClicked(MouseEvent e) {
    				if(e.getButton() == MouseEvent.BUTTON3){System.out.println("augh");}
    al posto dell'augh avevo provato ad aprire una JTextField.

  5. #5
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da paolovox Visualizza il messaggio
    I Listener vanno implementati sempre come inner class o esiste qualche altro metodo?
    Salvo casi particolari o generalizzazioni che ha senso implementare esternamente, è bene tenere i listener più nascosti possibile ma anche il più "vicino" possibile al punto dove hai e gestisci i componenti.
    Quindi tipicamente delle inner-class, di tipo regular o anonymous.

    Quote Originariamente inviata da paolovox Visualizza il messaggio
    Poi ho inserito un MouseListener che cliccando col destro su un bottone dovrebbe aprire soltanto una JTextField ma non funge.
    C'è un motivo preciso per cui hai bisogno del click con tasto destro? Generalmente si usa il tasto standard di selezione e si usa ActionListener per ricevere la azione del pulsante.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  6. #6
    Con questo ActionListener apro un file nel momento in cui clicco sul JButton:
    codice:
    class  Apri  implements ActionListener {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        Desktop.getDesktop().open(new File(path));
                    } catch (IOException e1) {
                
                        e1.printStackTrace();;
                    }
                }
            }
    Con questo MouseListener vorrei che si potesse cambiare il path del file, catturando il testo da un JTextField nel momento in cui ci clicco col destro.
    codice:
    class Percorso  implements MouseListener {
                
                @Override
                public void mouseClicked(MouseEvent e) {
                    if(e.getButton() == MouseEvent.BUTTON3){System.out.println("augh");
    Spero sia stato chiaro...

    Immagine.jpg
    Ultima modifica di paolovox; 03-07-2014 a 16:29

  7. #7
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da paolovox Visualizza il messaggio
    Con questo MouseListener vorrei che si potesse cambiare il path del file, catturando il testo da un JTextField nel momento in cui ci clicco col destro.
    E quale è il dubbio/problema? Devi chiaramente avere il riferimento al JTextField. Se ce l'hai, puoi prendere il testo.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  8. #8
    Il problema è che se aggiungo il JTextField al pannello del frame principale non me lo mostra... Forse bisogna creare un nuovo frame, ma mi sembra strano.

  9. #9
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Fa vedere il codice che hai scritto, altrimenti è un tirare ad indovinare...
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  10. #10
    codice:
    package myprogram;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.File;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    
    import javax.swing.*;
    
    
    public class MyProgram {
        private String path = "C:\\Users\\Paolo\\Desktop\\istruzioni.txt";
        private String pathjava= "C:\\Users\\Paolo\\Desktop\\jjj.gif";  
        private URL pathpython= new URL("http://screenshot.it.sftcdn.net/it/scrn/69000/69838/python-20.jpg");    
        private URL pathmips= new URL("https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQi_VaanDQyfTAph9BNO0NF7sN9ZR4Ven9LM9W_uiBbAYc5oVq7Pg");
        private URL pathenglish= new URL("http://upload.wikimedia.org/wikipedia/commons/3/3a/English_Speech_balloon.png");
        private JTextField testo = new JTextField(20);
        
        public MyProgram() throws MalformedURLException{start();};
       
        public void setPath(String s){
            this.path = s;
        }
        
        public void start() throws MalformedURLException{
            JFrame f = new JFrame("STUDIO"); 
            f.setSize(new Dimension(220,215));    //dimensioni frame 
            f.setLocation(1150,50);            //locazione frame
            
            Container c = f.getContentPane();    
           
           
            final JPanel panel = new JPanel();    
            panel.setBackground(new Color(1));
            c.add(panel);        
            
            Image im = new ImageIcon(pathjava).getImage();
            im = im.getScaledInstance(100, 100, Image.SCALE_DEFAULT);  //riscalatura immagine
            ImageIcon java = new ImageIcon(im);
            Image im2 = new ImageIcon(pathpython).getImage();
            im2 = im2.getScaledInstance(100, 100, Image.SCALE_DEFAULT);
            ImageIcon python = new ImageIcon(im2);
            Image im3= new ImageIcon(pathmips).getImage();
            im3 = im3.getScaledInstance(100, 100, Image.SCALE_DEFAULT);
            ImageIcon mips = new ImageIcon(im3);
            Image im4 = new ImageIcon(pathenglish).getImage();
            im4 = im4.getScaledInstance(100, 100, Image.SCALE_DEFAULT);
            ImageIcon english = new ImageIcon(im4);
            JButton j = new JButton(java);
            JButton p = new JButton(python);
            JButton m = new JButton(mips);
            JButton e = new JButton(english);
            f.setUndecorated(true);
            j.setPreferredSize(new Dimension(100,100));        //dimensioni del bottone
            p.setPreferredSize(new Dimension(100,100));        
            m.setPreferredSize(new Dimension(100,100));        
            e.setPreferredSize(new Dimension(100,100));        
            panel.add(j);                                    //aggiunge i bottoni al pannello
            panel.add(p);
            panel.add(m);
            panel.add(e);
            
            f.setAlwaysOnTop(true);
            f.setDefaultCloseOperation(3);
            f.setVisible(true);                      // mostra il frame
            
            
            
            class Percorso  implements MouseListener {
                
                @Override
                public void mouseClicked(MouseEvent e) {
                    if(e.getButton() == MouseEvent.BUTTON3){System.out.println("augh");
                    testo.setSize(100,100);
                    testo.setLocation(MouseInfo.getPointerInfo().getLocation()); // si dovrebbe aprire dove clicco
                    System.out.println(MouseInfo.getPointerInfo().getLocation()); // coordinate mouse
                    testo.setBackground(Color.RED);
                    testo.setVisible(true);
                    }
                    else testo.setVisible(false);
                }
    
    
                @Override
                public void mouseEntered(MouseEvent e) {
                    // TODO Auto-generated method stub
                    
                }
    
    
                @Override
                public void mouseExited(MouseEvent e) {
                    // TODO Auto-generated method stub
                    
                }
    
    
                @Override
                public void mousePressed(MouseEvent e) {
                    // TODO Auto-generated method stub
                    
                }
    
    
                @Override
                public void mouseReleased(MouseEvent e) {
                    // TODO Auto-generated method stub
                    
                }
                
            }
            class  Apri  implements ActionListener {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        Desktop.getDesktop().open(new File(path));
                    } catch (IOException e1) {
                
                        e1.printStackTrace();;
                    }
                }
            }
    
    
            j.addActionListener(new Apri());
            p.addActionListener(new Apri());
            e.addMouseListener(new Percorso());
            
            }
        
        public static void main(String[] args) throws MalformedURLException {
            new MyProgram();
            
        }
    }

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.