I create a JPanel and added a few simple buttons with listeners attached to them. Then I decided to add an Image to the background of my panel, so I switched my JPanel to an ImagePanel. The buttons were working on JPanel, but now that I added a bunch of code for the background image to be displayed, the buttons no longer show. I did not change of any of the button adding code so I'm very confused as to why the buttons no longer show. This also happened in my separate GameFrame class. I added 2 rectangle components to a panel, then 3 buttons. For that panel, only the buttons show, despite the rectangles working before the buttons were added. Can I only have one type of JComponent per panel or something? I really do not understand why it's doing this. Thank you for your time.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TitleFrame extends JFrame
{
private ImagePanel panel;
private JButton mage;
private JButton rogue;
private JButton warrior;
private Image image;
public TitleFrame()
{
JFrame frame = new JFrame();
frame.setSize(1024, 768);
frame.setTitle("Title Screen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
createMageButton();
createRogueButton();
createWarriorButton();
ImagePanel panel = new ImagePanel(new ImageIcon("C:/Users/Derek Reitz/Documents/Eclipse Projects/GameApp/src/background.jpg").getImage());
panel.add(mage);
panel.add(rogue);
panel.add(warrior);
panel.paintComponent(frame.getGraphics());
frame.getContentPane().add(panel);
}
private void createRogueButton() {
rogue = new JButton("Select Rogue");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('r');
}
}
ActionListener listener = new AddButtonListener();
rogue.addActionListener(listener);
}
private void createWarriorButton() {
warrior = new JButton("Select Warrior");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('w');
}
}
ActionListener listener = new AddButtonListener();
warrior.addActionListener(listener);
}
private void createMageButton() {
mage = new JButton("Select Mage");
class AddButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
moveToNextFrame('m');
}
}
ActionListener listener = new AddButtonListener();
mage.addActionListener(listener);
}
public void moveToNextFrame(char c)
{
GameFrame game = new GameFrame(c);
}
class ImagePanel extends JPanel
{
private Image img;
public ImagePanel(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);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
}
You need to use a LayoutManager .
You should then be able to add your ImagePanel and buttons to the contentPane and have them all layed out and visible.
Try the follwoing:
JFrame frame = new JFrame();
frame.setTitle("Title Screen");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
createMageButton();
createRogueButton();
createWarriorButton();
ImagePanel panel = new ImagePanel(new ImageIcon(".../background.jpg").getImage());
panel.setLayout(new FlowLayout());
panel.add(mage);
panel.add(rogue);
panel.add(warrior);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
You set as layout null
. That is a special case where absolute positions of the components are accepted. So use `setBounds(x, y, width, height). Better still use a real layout.
Another remark, you can take the image from the class path, say from out of the resulting .jar file):
URL url = getClass().getResource("/background.jpg");
... new ImageIcon(url);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.