Eu estou iniciando Java no meu curso técnico, e o professor pediu para construir um jogo simples e tal, que não vem ao caso agora. Andei estudando sobre JFrame e JButton, mas estou com uma dúvida no código quanto colocar imagens na janela. Já tentei de tudo, e não consigo... O meu protótipo tem duas classe: apples (onde fica a main) e a Gui (onde está o código).
Classe Gui:import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Gui extends JFrame {
private JButton atk;
private JButton def;
private JButton halfatk;
private Image fundo;
public Gui() {
super("Prótotipo JoguECA");
setFocusable(true);
ImageIcon referencia = new ImageIcon("fundo.jpg");
fundo = referencia.getImage();
Container c = getContentPane();
c.setLayout(new BorderLayout());
Icon b = new ImageIcon(getClass().getResource("atk.gif"));
Icon x = new ImageIcon(getClass().getResource("def.gif"));
Icon w = new ImageIcon(getClass().getResource("halfatk.gif"));
atk = new JButton(b);
atk.setForeground(Color.black);
atk.setHorizontalTextPosition(SwingConstants.CENTER);
atk.setPreferredSize(new Dimension(100, 50));
add(atk);
atk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, String.format("%s", "Atacou."));
System.out.println("Atacou");
}
});
halfatk = new JButton(w);
halfatk.setForeground(Color.black);
halfatk.setHorizontalTextPosition(SwingConstants.CENTER);
halfatk.setPreferredSize(new Dimension(60, 50));
add(halfatk);
halfatk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, String.format("%s", "Atacou parcialmente."));
System.out.println("Atacou parcialmente.");
}
});
def = new JButton(x);
def.setForeground(Color.black);
def.setHorizontalTextPosition(SwingConstants.CENTER);
def.setPreferredSize(new Dimension(100, 50));
add(def);
def.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, String.format("%s", "Defendeu."));
System.out.println("Defendeu");
}
});
JPanel options = new JPanel();
options.add(atk);
options.add(halfatk);
options.add(def);
c.add(BorderLayout.SOUTH, options);
}
}
import java.awt.Color;
import javax.swing.JFrame;
public class apples {
public static void main(String[] args) {
Gui go = new Gui();
go.setBackground(Color.red);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(500,400);
go.setVisible(true);
go.setLocationRelativeTo(null);
go.setResizable(false);
}
}
A minha dúvida é o que eu devo fazer para aparecer uma imagem no background da tela, sem interferir nos botões e etc.
Obrigado desde já!