HI THERE :) I'm trying to figure out what proper code to use so that I can add a background image to this :) HERE ARE THE CODES, what it does is it only displays JLABELs and JBUTTONs, What I need is to put a background image :O but whenever I try it, it either COVERS the entire thing, or it doesnt display :(
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MainFrame
{
JFrame mainFrame = new JFrame("Main Frame");
JPanel mainPanel = new JPanel();
JLabel label1 = new JLabel("A SECOND OF LIFE");
JButton button1 = new JButton("START");
JButton button2 = new JButton("EXIT");
Font font = new Font ("Impact", Font.PLAIN, 40);
JLabel label2 = new JLabel("Do you have what it takes? ");
//JLabel background = new JLabel(new ImageIcon("C:\\Users\\Master Boat\\Desktop\\PH\\horror bg jisha.png"));
//FlowLayout poli = new FlowLayout();
public MainFrame()
{
label1.setAlignmentX(Component.CENTER_ALIGNMENT);
label2.setAlignmentX(Component.CENTER_ALIGNMENT);
button1.setAlignmentX(Component.CENTER_ALIGNMENT);
button2.setAlignmentX(Component.CENTER_ALIGNMENT);
button1.setFont(font);
button2.setFont(font);
label1.setFont(font);
//mainFrame.add(background);
//background.setLayout(poli);
button2.addActionListener(new btnFunc());
button1.addActionListener(new btnFunc2());
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(label1);
mainPanel.add(label2);
mainPanel.add(button1);
mainPanel.add(button2);
mainFrame.setSize(500, 500);
mainFrame.setLocation(500, 100);
mainFrame.add(mainPanel);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(MainFrame::new);
}
public class btnFunc implements ActionListener {
public void actionPerformed (ActionEvent e) {
JOptionPane.showMessageDialog(null,"YOU ARE A COWARD !", " WAAAAA!",JOptionPane.INFORMATION_MESSAGE);
System.exit(1);
}
}
public class btnFunc2 implements ActionListener {
public void actionPerformed (ActionEvent e) {
new gamesamplingbeta();
}
}
}