Hallo! (mal wieder)
Ich hab ein Panel mit einem Bild.
Jedoch ist das Bild nicht in der Mitte (Also Vertikal) Es soll oben und unten den gleichen Abstand zum Rand haben.
Ich hab auch den Eindruck, dass das ganze Panel, das das Image beinhaltet "ImageContainer" nicht mittig angeordnet (wieder vertical) wird.
Hoffe mir kann wer helfen.
Ich hab ein Panel mit einem Bild.
Jedoch ist das Bild nicht in der Mitte (Also Vertikal) Es soll oben und unten den gleichen Abstand zum Rand haben.
Ich hab auch den Eindruck, dass das ganze Panel, das das Image beinhaltet "ImageContainer" nicht mittig angeordnet (wieder vertical) wird.
Hoffe mir kann wer helfen.
Java:
package com.ludevstudio.passwordmanager;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class ItemPanel extends JPanel {
JLabel iconContainer, labProvider, labAccount, labPassword;
JPanel textContainer;
Image icon;
public ItemPanel() {
try {
icon = ImageIO.read(new File("icon.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setBorder(new LineBorder(Color.black));
setPreferredSize(new Dimension(450, 100)); // HIER SOLL DIE GRÖSSE AUTOM. ANGEPASST WERDEN
setMinimumSize(this.getPreferredSize());
setMaximumSize(this.getPreferredSize());
setLayout(new FlowLayout(FlowLayout.LEFT));
initComponents();
}
public void initComponents() {
Font fontProvider = new Font("Arial", Font.BOLD, 36);
Font fontAccount = new Font("Arial", Font.ITALIC, 28);
iconContainer = new JLabel(new ImageIcon(getScaledImage(icon, 80, 80)));
iconContainer.setPreferredSize(new Dimension(100, 100));
iconContainer.setMaximumSize(iconContainer.getPreferredSize());
iconContainer.setBorder(new LineBorder(Color.black));
this.add(iconContainer);
textContainer = new JPanel();
textContainer.setLayout(new BoxLayout(this.textContainer, BoxLayout.PAGE_AXIS));
labProvider = new JLabel("Microsoft OnleDrive");
labProvider.setFont(fontProvider);
textContainer.add(labProvider);
labAccount = new JLabel("Laura Brandt");
labAccount.setFont(fontAccount);
textContainer.add(labAccount);
this.add(textContainer);
}
private Image getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
}