【问题标题】:How do I put a Background Image in this Specific Code? :) JAVA [duplicate]如何在此特定代码中放置背景图像? :) JAVA [重复]
【发布时间】:2016-06-03 19:47:55
【问题描述】:

您好 :) 我正在尝试找出要使用的正确代码,以便可以向其中添加背景图像 :) 这是代码,它的作用是只显示 JLABEL 和 JBUTTON,我需要什么是放一个背景图片:O 但是每当我尝试它时,它要么覆盖整个事物,要么不显示:(

    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();
    }
}

}

【问题讨论】:

    标签: java


    【解决方案1】:

    您可以在标签上添加图片:

    ImageIcon background;
    background = new ImageIcon("/image/image.png");
    
    label= new JLabel() {
      public void paintComponent(Graphics g) {
        g.drawImage(icon.getImage(), 0, 0, null);
        super.paintComponent(g);
      }
    };
    

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2013-09-06
      • 2014-07-22
      • 1970-01-01
      • 2010-10-06
      • 2011-09-02
      • 1970-01-01
      相关资源
      最近更新 更多