I've tried numerous ways and methods but nothing works. I can't get both to be displayed, that is.
I've managed to successfully create a JFrame and add a background image to it but I cannot add more components without
'overwriting' or 'hiding' that image no matter what I do.
What I want to do is create 3 java classes. One will be the main class and create the JFrame, the second will paint an image
background to the frame and the third will add some components on it, such as some JButtons, more image labels etc.
(It can also be more than 3 classes of course, I just don't want to put everything in one single class because it looks messy )
This is what I have done so far:
The main class:
import javax.swing.*;
import java.awt.*;
public class ApplicationFrame
{
public static void main(String[] args)
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
JFrame frame = new JFrame("Application");
BackgroundImg panel = new BackgroundImg(Toolkit.getDefaultToolkit().getImage(ApplicationFrame.class.getResource("background.png")));
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
Toolkit kit = frame.getToolkit();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());
Dimension d = kit.getScreenSize();
int max_width = (d.width - in.left - in.right);
int max_height = (d.height - in.top - in.bottom);
frame.setSize(Math.min(max_width, 800), Math.min(max_height, 600));
frame.setLocation((int) (max_width - frame.getWidth()) / 2, (int) (max_height - frame.getHeight() ) / 2);
}
}
The "Background Image" class:
import javax.swing.*;
import java.awt.*;
public class BackgroundImg extends JPanel
{
private Image img;
public BackgroundImg (String img)
{
this(new ImageIcon(img).getImage());
}
public BackgroundImg (Image img)
{
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
And my components class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ApplicationContents
{
public static void addContentsToPane(Container pane)
{
pane.setLayout(null);
JButton b1 = new JButton("Test");
pane.add(b1);
Insets insets = pane.getInsets();
Dimension size = new Dimension(120,32);
b1.setBounds(0 + insets.left, 0 + insets.top,
size.width, size.height);
b1.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null, "Test" , "Test", JOptionPane.ERROR_MESSAGE);
}
}
);
}
}
I'd like to add something like this in the main class:
ApplicationContents.addContentsToPane(frame.getContentPane());
Any tips are welcome, I'm in a dead-end here...
Note: I know that I can also add the background image in a panel or label and then put it in the
background using absolute positioning but I'd like to refrain from doing that.
Also I'd like to add the components on a container and call that instead of adding them in a class
and calling the constructor.

New Topic/Question
Reply
MultiQuote








|