Hello,
Java: 1.6.0_23
Netbeans: 6.9.1
I`m making chatting program and I wanted to add a background image to the main window. So I added a image via JPanel and after that the components are no longer displaying properly. The components are displayed after i point/click on them with the mouse, except the JLabel components. On the attached screenshots you can see what is the problem and the difference. Here is the code of the window:
Here is the code of WindowBackgroun d:
Java: 1.6.0_23
Netbeans: 6.9.1
I`m making chatting program and I wanted to add a background image to the main window. So I added a image via JPanel and after that the components are no longer displaying properly. The components are displayed after i point/click on them with the mouse, except the JLabel components. On the attached screenshots you can see what is the problem and the difference. Here is the code of the window:
Code:
public Login() {
BufferedImage icon = null;
URL path = null;
language.Init(); // The applications is multilangual, so initializes the language
this.setTitle(language.getMessage("LOGIN_WINDOW_TITLE"));
path = getClass().getResource("images/securChatter_01.jpg");
JPanel panel = new WindowBackground(new ImageIcon(path).getImage());
panel.setLayout(new GridBagLayout());
GridBagConstraints gbCons = new GridBagConstraints();
//adding the components
gbCons.gridx = 0;
gbCons.gridy = 0;
lblUserName = new JLabel(language.getMessage("USERNAME"));
panel.add(lblUserName, gbCons);
gbCons.gridx = 0;
gbCons.gridy = 1;
txtUsrName = new JTextField(10);
panel.add(txtUsrName, gbCons);
gbCons.gridx = 0;
gbCons.gridy = 2;
lblUserPwd = new JLabel(language.getMessage("PASSWORD"));
panel.add(lblUserPwd, gbCons);
gbCons.gridx = 0;
gbCons.gridy = 3;
txtUsrPwd = new JPasswordField(10);
panel.add(txtUsrPwd, gbCons);
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new FlowLayout());
icon = loadIcon(labelIcon, "loginIcon.png");
lblLoginIcon = new JLabel(new ImageIcon(icon));
lblLoginIcon.setToolTipText(language.getMessage("LOGIN_BUTTON"));
lblLoginIcon.addMouseListener(this);
btnPanel.add(lblLoginIcon);
icon = loadIcon(labelIcon, "registerIcon.png");
lblRegisterIcon = new JLabel(new ImageIcon(icon));
lblRegisterIcon.setToolTipText(language.getMessage("REGISTER_BUTTON"));
lblRegisterIcon.addMouseListener(this);
btnPanel.add(lblRegisterIcon);
icon = loadIcon(labelIcon, "settingsIcon.png");
lblSettingsIcon = new JLabel(new ImageIcon(icon));
lblSettingsIcon.setToolTipText(language.getMessage("SETTINGS_BUTTON"));
lblSettingsIcon.addMouseListener(this);
btnPanel.add(lblSettingsIcon);
gbCons.gridx = 0;
gbCons.gridy = 4;
gbCons.anchor = GridBagConstraints.CENTER;
gbCons.gridwidth = 2;
panel.add(btnPanel, gbCons);
getContentPane().add(panel);
setSize(150, 180);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
Code:
public class WindowBackground extends JPanel {
// The Image to store the background image in.
Image img;
public WindowBackground(Image img) {
this.img = img;
}
public void paint(Graphics g) {
// Draws the img to the BackgroundPanel.
g.drawImage(img, 0, 0, null);
}
}