I have the following:
public class Frame {
public static void main(String[] args) {
JFrame frame = new JFrame("Frame Demo");
Panel panel = new Panel();
frame.add(panel);
frame.setBounds(250,100,800,500);
frame.setVisible(true);
}
}
public class Panel extends JPanel {
JButton singlePlayerButton;
JButton multiPlayerButton;
BufferedImage image;
Gui gui;
public Panel() {
gui = new Gui();
add(gui);
try {
image = ImageIO.read(new File("C:\Users\void\workspace\BattleShips\src\Testumgebung\background\battleship_main.jpg"));
} catch (IOException e) {
e.getMessage();
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
}
JLabel text;
JPanel mainMenuPanel;
private class Gui extends JPanel {
public Gui() {
setVisible(true);
setSize(500, 300);
setLayout(null);
mainMenuPanel = new JPanel();
mainMenuPanel.setLayout(null);
mainMenuPanel.setBackground(Color.BLUE);
mainMenuPanel.setBounds(150, 10, 200, 230);
add(mainMenuPanel);
mainMenuPanel.setVisible(true);
singlePlayerButton = new JButton("Single Player");
singlePlayerButton.setBounds(100,50 , 150, 40);
singlePlayerButton.setVisible(true);
mainMenuPanel.add(singlePlayerButton);
multiPlayerButton = new JButton("Multi Player");
multiPlayerButton.setBounds(100, 100, 150, 40);
multiPlayerButton.setVisible(true);
mainMenuPanel.add(multiPlayerButton);
repaint();
}
}
}
I just want a MainMenu with a BackgroundImage and buttons like Singleplayer etc. Do I have to set a Layout or is it possible without one. I just started with GUI, as you can probably assume from the code. Thanks in advance...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…