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();
}
}