import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; public class Icon extends JButton { /** * */ private static final long serialVersionUID = 1L; private String location; private int large = 10; private int length = 10; private String fileLocation = ""; //Constructeur public Icon(String location, int large, int length) { // Vraiment heriter de la classe JButton super(); this.location=location; this.length=length; this.large=large; setPreferredSize(new Dimension(large, length)); setBorderPainted(false); setContentAreaFilled(false); setFocusPainted(false); setOpaque(false); // Assigner une tâche de rafraîchissement // NB: cette tâche est séverement ajoutée dans ce cas //La changer si non nécessaire this.addChangeListener((fx)->{ refresh(); }); } // Bien que la methode soit appellée d'elle même, il // faut prévoir une issue de secours : rafraichîr l'affichage. // Car cetains composants la gèrent mal. public void refresh() { this.repaint(); } public void paintComponent(Graphics g){ // Invoquer Graphics 2D afin de jouir de toutes //les performances de la class graphic Graphics2D g2 = (Graphics2D)g; try { Image img = ImageIO.read(new File(location)); g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); } catch (IOException e) { Image img; try { img = ImageIO.read(new File(fileLocation)); g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); } catch (IOException e1) { e1.printStackTrace(); } } } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionimport java.awt.Dimension; import java.awt.Toolkit; public class RedimApp { //La resolution d'écran de la machine hôte // ayant servit à coder et tester le code. private final double eWidth = 1366.0; private final double eHeight = 768.0; // La future nouvelle dimension private Dimension newDim; /* * Methode servant à calculer la nouvelle dimension * de la GUI. en fonction d'une dimension fonctionnelle * connue de l'écran; et de la dimension de la GUI voulue * par le progammeur et même par l'utilisateur (onResize....fait l'affaire) */ public Dimension dimCalculating(Dimension myAppDim) { // On récupère la dim de l"ecran: machine hôte Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); // On calcule la dimension appropriée double newWidth = Math.abs((eWidth-screenDim.getWidth())+ myAppDim.getWidth()); double newHeight = Math.abs((eHeight-screenDim.getHeight())+ myAppDim.getHeight()); // On rétourne cette nvlle dimension //(ex: utilisée dans la JFrame comme suit: //this.setSize(newDim)) newDim.setSize(newWidth, newHeight); return newDim; } }
import java.awt.*;
import javax.swing.*;
import java.net.URL;
import javax.imageio.ImageIO;
class Gui {
//Importiert Auflösung des Bildschirms
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
//Setzt Variablen für die Auflösung
public int aufloesungBreite = screenSize.width;
public int aufloesungHoehe = screenSize.height;
//Setzt die Berechnung des JFrame hauptfenster Location
private int breite = aufloesungBreite/2 - 640;
private int hoehe = aufloesungHoehe/2 - 400;
public Gui() {
JFrame hauptfenster = new JFrame("Verwaltungssoftware fuer die Jobsuche");
hauptfenster.setDefaultCloseOperation(hauptfenster.EXIT_ON_CLOSE);
hauptfenster.setResizable(false);
hauptfenster.setLocation(breite, hoehe);
hauptfenster.setSize(1280,800);
hauptfenster.setLayout(new BorderLayout(5,5));
//Addet hauptpanel zum JFrame
ImagePanel hauptpanel = new ImagePanel();
hauptfenster.add(hauptpanel);
hauptpanel.setVisible(true);
hauptfenster.setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new Gui();
}
};
SwingUtilities.invokeLater(r);
}
}
class ImagePanel extends JPanel {
//Variablen
Image icon1;
//Konstruktor
public ImagePanel() {
try {
URL url = new URL("http://i.stack.imgur.com/7bI1Y.jpg");
icon1 = ImageIO.read(url);
} catch (Exception e) {
e.printStackTrace();
}
setLayout(new BorderLayout (5,5));
JLabel myLabel=new JLabel(new ImageIcon(icon1));
add(myLabel);
}
// very important!
@Override
public Dimension getPreferredSize() {
return new Dimension(icon1.getWidth(this), icon1.getHeight(this));
}
}
25 avril 2019 à 10:01
25 avril 2019 à 10:27