Answer the question
In order to leave comments, you need to log in
How to add an image to a panel?
public class cl{
JLabel enterTheA=new JLabel("Введите число A:");
JTextField numberA=new JTextField();
GridBagLayout gridBagLayout=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
public JPanel a1 (){
JPanel panel=new JPanel();
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.NONE;
c.gridheight = 2;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(20, 0, 0, 0);
c.ipadx = 0;
c.ipady = 0;
c.weightx = 0.0;
c.weighty = 0.0;
gridBagLayout.setConstraints(enterTheA, c);
panel.add(enterTheA);
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.NONE;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets(20, 0, 0, 0);
c.ipadx = 60;
c.ipady = 0;
c.weightx = 0.0;
c.weighty = 0.0;
gridBagLayout.setConstraints(numberA, c);
panel.add(numberA);
return panel;
}}
Answer the question
In order to leave comments, you need to log in
Hmm, I have such code in my working project now.
Code for creating a label for a photo:
JLabel photo = new JLabel();
Icon icon = loadPhoto(editingRecord == null ? "" : editingRecord.getPhotoBase64());
photo.setIcon(icon);
private Icon loadPhoto(String base64String) {
if (isBlank(base64String)) {
return null;
}
byte[] bytes = DatatypeConverter.parseBase64Binary(base64String);
ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
try {
BufferedImage original = ImageIO.read(bin);
if (original == null || original.getHeight() == 0) {
return null;
}
double factor = 100.0 / original.getHeight();
int newWidth = new Double(((double) original.getWidth()) * factor).intValue();
int newHeight = 100;
BufferedImage resized = new BufferedImage(newWidth, newHeight, original.getType());
Graphics2D g = resized.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(original, 0, 0, newWidth, newHeight, 0, 0, original.getWidth(),
original.getHeight(), null);
g.dispose();
return new ImageIcon(resized);
} catch (Exception e) {
logger.error(LogHelper.print("Can't load photo preview for employee", e));
MessageDialog.showError(this,
Global.data().getString("ERROR_READING_EMPLOYEE_PHOTO"),
Global.data().getString("ERROR_TITLE"));
}
return null;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question