繁体   English   中英

使用Swing将背景图像添加到JFrame

[英]Adding a background image to a JFrame using Swing

您好,我一直在遵循有关如何将背景图像添加到JFrame的多个指南。 但是,当运行我的程序时,背景显示出来了,左下角和底侧只显示了一部分背景图像。

这是我当前的代码:

private int width = 1280;
private int height = 720;

private String title = "Creation - " + Component.versionID + " Launcher";

private JPanel window = new JPanel();
private JFrame frame = new JFrame();
private JLabel name, version;


private JButton play, options, changelog, quit;
private Rectangle rPlay, rOptions, rChangelog, rQuit, rName, rVersion;
private int buttonWidth = 200;
private int buttonHeight = 80;

public Menu (Component component) {
    frame.setTitle(title);
    frame.setSize(new Dimension(width, height));
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.add(component);
    frame.getContentPane().add(window);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setIconImage(new ImageIcon("res/icon.png").getImage());

    setLayout(new BorderLayout());
    JLabel background = new JLabel(new ImageIcon("res/background.png"));
    frame.add(background);
    background.setLayout(new FlowLayout());

    window.setLayout(null);
    drawButtons();
    frame.repaint();
    frame.pack();
}

private void drawButtons() {

    name = new JLabel("Creation");
    rName = new Rectangle((width/2) - (buttonWidth/2) + 60, 20, buttonWidth, buttonHeight);
    name.setBounds(rName);
    window.add(name);

    play = new JButton("Play");
    rPlay = new Rectangle((width/2) - (buttonWidth/2), 250, buttonWidth, buttonHeight);
    play.setBounds(rPlay);
    window.add(play);

    options = new JButton("Options");
    rOptions = new Rectangle((width/2) - (buttonWidth/2), 350, buttonWidth, buttonHeight);
    options.setBounds(rOptions);
    window.add(options);

    changelog = new JButton("View change-log");
    rChangelog = new Rectangle((width/2) - (buttonWidth/2), 450, buttonWidth, buttonHeight);
    changelog.setBounds(rChangelog);
    window.add(changelog);

    quit = new JButton("Quit");
    rQuit = new Rectangle((width/2) - (buttonWidth/2), 550, buttonWidth, buttonHeight);
    quit.setBounds(rQuit);
    window.add(quit);

    play.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = new Component();
            frame.dispose();
            component.start();
        }
    });
    options.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    changelog.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    quit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {        
            System.exit(0);
        }
    });
}

(如您所见)我的JFrame大小是1280x720。 此尺寸与背景图片相同,因此看不到任何问题。 如果有人可以帮助我编辑代码或给我指针,我只是在寻找要在按钮后面显示的背景图像。

  1. 如果您的GUI将是图像的大小,则将BufferedImage添加到JLabel,然后将JLabel添加到JFrame的contentPane,而不是一些不显示图像的JPanel。
  2. 不要使用null布局和setBounds(...) ,而要使用体面的布局管理器。 尽管空布局对于新手来说似乎更容易,但这通常是因为您可能不熟悉如何使用布局管理器并相信我,但是创建和维护使用行为良好的布局管理器的GUI 则要容易得多
  3. 给JPanel / contentPane一个合适的布局,因为您要向其中添加组件。
String filename = "demo//6.jpg";

BufferedImage image = ImageIO.read(new File(filename));

//Create Image Label
JLabel label = new JLabel(new ImageIcon(image));
label.setBounds(0, 0, image.getWidth(), image.getHeight());
lpane.setLayout(null);
lpane.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
lpane.add(label, new Integer(JLayeredPane.DEFAULT_LAYER-1));
ImageIcon ic=new ImageIcon("demo//12.jpg");
but.setIcon(ic);

类MyFrame {

public static void main(String arr[])
{
   JPanel panel=new JPanel()
   {
       @Override
       protected void paintComponent(Graphics g)
       {
           try
           {
               BufferedImage image = ImageIO.read(new File("C://Users//MulayamYadav//Desktop//1.jpg"));
               Image image1=image.getScaledInstance(getWidth(), getHeight(), Image.SCALE_SMOOTH);
               g.drawImage(image1, 0, 0, null);
            }catch(Exception ex)
            {
              System.out.println(ex);
            }
        }
   };
   JFrame frame=new JFrame();
   panel.setLayout(new FlowLayout());
   panel.add(new JButton("Button1"));
   panel.add(new JButton("Button2"));
   add(panel);
   frame.setVisible(true);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM