背景图片覆盖我的按钮(Java)

时间:2018-06-02 05:24:56

标签: java image swing jbutton

import java.awt.Color;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Ahh {
static boolean pass = false;

public static void PLAY(JFrame e, JFrame h, JFrame g, JFrame s) {
    JPanel p_play = new JPanel();

    p_play.setLayout(null);
    menu.setBounds(600, 620, 75, 25);
    e.setVisible(false);
    h.setVisible(false);
    g.setVisible(false);

    s.setVisible(true);
    p_play.add(menu);
    menu.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try {
                main(null);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
    });
    s.add(p_play);
}

public static void HELP(JFrame e, JFrame h, JFrame g, JFrame s) {
    JPanel p_help = new JPanel();

    p_help.setLayout(null);
    menu.setBounds(600, 620, 75, 25);
    e.setVisible(false);
    h.setVisible(true);
    g.setVisible(false);

    s.setVisible(false);
    p_help.add(menu);
    menu.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try {
                main(null);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
    });
    h.add(p_help);
}

public static void EXIT(JFrame e, JFrame h, JFrame g, JFrame s) {

    System.exit(0);

}

static JButton menu = new JButton("MENU");

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    JFrame.setDefaultLookAndFeelDecorated(true);

    JPanel p = new JPanel();
    JLabel l = new JLabel();

    JTextField text = new JTextField("(Enter Your Name)", 50);

    // frame
    JFrame game = new JFrame();
    JFrame start_frame = new JFrame();
    JFrame help_frame = new JFrame();
    JFrame exit_frame = new JFrame();

    // frame settings

    game.setSize(700, 700);
    game.setBackground(Color.gray);
    game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.setTitle("POINT");

    start_frame.setSize(700, 700);
    start_frame.setBackground(Color.YELLOW);
    start_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    start_frame.setTitle("PLAY");

    help_frame.setSize(700, 700);
    help_frame.setBackground(Color.YELLOW);
    help_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    help_frame.setTitle("HELP");

    exit_frame.setSize(700, 700);
    exit_frame.setBackground(Color.YELLOW);
    exit_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    exit_frame.setTitle("EXIT");

    // buttons
    JButton b = new JButton("Enter");
    JButton start = new JButton("START");
    JButton help = new JButton("HELP");
    JButton exit = new JButton("EXIT");

    // placeing the buttons
    p.setLayout(null);
    start.setBounds(100, 500, 75, 25);// start button
    help.setBounds(300, 500, 75, 25);// help button
    exit.setBounds(500, 500, 75, 25);// exit button
    b.setBounds(280, 100, 100, 50);// enter name button
    text.setBounds(50, 10, 580, 20);// name input
    l.setBounds(50, 50, 580, 20);// name output
    p.setBounds(100, 100, 75, 25);

    p.add(text);
    // button funciton
    text.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            String name = text.getText();

            name = "Welcome: " + name;
            l.setText(name);

        }

    });
    // button funciton
    p.add(b);
    b.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            String name = text.getText();

            name = "Welcome: " + name;
            l.setText(name);
        }
    });
    p.add(start);
    // button funciton
    start.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            PLAY(exit_frame, help_frame, game, start_frame);

        }
    });
    // button funciton
    p.add(help);
    help.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            HELP(exit_frame, help_frame, game, start_frame);
        }
    });
    // button funciton
    p.add(exit);
    exit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            EXIT(exit_frame, help_frame, game, start_frame);

        }
    });

    p.add(l);

    // pictures
    ImageIcon menu_pic = new ImageIcon("pics/Jolteon.jpg");
    JLabel m_pic = new JLabel(menu_pic);
    m_pic.setBounds(0, 0, 650, 650);
    game.getContentPane().add(m_pic);

    game.add(p);
    game.setVisible(true);

}

}

您好我正在为学校项目制作菜单,我正在尝试为我的背景添加图片。然而,当我添加图片时,它覆盖了我的所有按钮和框架中的任何其他内容。我希望能够把照片放在其他一切的背后。我将代码放在最后几行代码中,以便您更轻松地阅读。

0 个答案:

没有答案