java - java swing问题中的背景图像

标签 java image swing user-interface paintcomponent

我制作了一个带有背景图像的 Jpanel 和一个带有背景图像的 Jbutton。问题是背景有时加载有时不加载。

public class Window extends JFrame {

    public static JFrame createwindow() {//fare singleton
        JFrame frame = new JFrame("Battaglia navale");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(740, 740);
        frame.setVisible(true);
        frame.setResizable( false );

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        frame.setLocation(((int)dim.getWidth()-(int)frame.getWidth())/2, ((int)dim.getHeight()-(int)frame.getHeight())/2);

        return frame;
    }
}

public class StartWindow {

    JFrame frame;
    private JButton button;
    private JButton button2;
    final String button_start = "img/start.png";
    ImageIcon start = new ImageIcon(button_start);

    public void CreateStartWindow() {
        frame = Window.createwindow();
        Container container = frame.getContentPane();
        JpanelStart panel = new JpanelStart();
        container.add(panel);

        this.button = new JButton(start);
        button.setActionCommand("start");
        button.setHideActionText(true);
        button.setOpaque(false);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);

        this.button2 = new JButton("Classifica");

        panel.add(button);
        panel.add(button2);
        frame.setVisible(true);
    }

    public void addActionListener(ActionListener al) {
        this.button.addActionListener(al);
        this.button2.addActionListener(al);
    }

    public void chiudi() {
        frame.dispose();
    }
}

class JpanelStart extends JPanel {

    private Image img;
    private String path_img = "img/sfondo.jpg";

    public JpanelStart() {
        img = Toolkit.getDefaultToolkit().createImage(path_img);
        loadImage(img);
    }

    private void loadImage(Image img) {
        try {
            MediaTracker track = new MediaTracker(this);
            track.addImage(img, 0);
            track.waitForID(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        setOpaque(false);
        g.drawImage(img, 0, 0, this);
        super.paintComponent(g);

    }
}

最佳答案

  1. super.paintComponet 应紧接在方法签名之后。
  2. 您将按钮不透明度设置为 false,因此它不会被看到。
  3. 从事件调度线程运行您的程序。

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new StartWindow().CreateStartWindow();  
            }
        });
    }
    
  4. 在您的方法中,您在添加任何内容之前使框架可见。将其保留在方法中

  5. 不要设置框架的大小。而是重写 JPanelgetPrefereedSize() 并在框架上调用 pack()
  6. IMO,我认为这个所谓的辅助方法根本没有任何用处。我会把它扔出窗外
  7. 您应该将图像作为嵌入式资源加载,而不是从文件系统加载。

    img = ImageIO.read(StartWindow.class.getResource(path_img));
    
<小时/>
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class StartWindow {

    JFrame frame;
    private JButton button;
    private JButton button2;
    final String button_start = "img/start.png";
    ImageIcon start = new ImageIcon(button_start);

    public void CreateStartWindow() throws IOException  {
        frame = Window.createwindow();
        Container container = frame.getContentPane();
        JpanelStart panel = new JpanelStart();
        container.add(panel);

        this.button = new JButton(start);
        button.setActionCommand("start");
        button.setHideActionText(true);
        button.setOpaque(false);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setContentAreaFilled(false);

        this.button2 = new JButton("Classifica");

        panel.add(button);
        panel.add(button2);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);


    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                try {
                    new StartWindow().CreateStartWindow();
                } catch (IOException ex) {
                    Logger.getLogger(StartWindow.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

    }

    public void addActionListener(ActionListener al) {
        this.button.addActionListener(al);
        this.button2.addActionListener(al);
    }

    public void chiudi() {
        frame.dispose();
    }
}

class Window {

    public static JFrame createwindow() {//fare singleton

        JFrame frame = new JFrame("Battaglia navale");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setResizable(false);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        return frame;

    }

}



class JpanelStart extends JPanel {
    private static final int D_W = 700;
    private static final int D_H = 700;

    private Image img;
    private String path_img = "/images/logo.gif";

    public JpanelStart() throws IOException {

        img = ImageIO.read(StartWindow.class.getResource(path_img));
        loadImage(img);

    }

    private void loadImage(Image img) {
        try {
            MediaTracker track = new MediaTracker(this);
            track.addImage(img, 0);
            track.waitForID(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(img, 0, 0, D_W, D_W, this);
    }

    @Override 
    public Dimension getPreferredSize() {
        return new Dimension(D_W, D_H);
    }
}
<小时/>

**每次都会出现**

enter image description here

关于java - java swing问题中的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433232/

相关文章:

Java:重复、过度使用——问题?

html - 内部有滚动图像的 iphone 剪影

html - 不知道图像大小的图像最大宽度

java - 设置 JLabel 左右对齐

java - 将鼠标悬停在 jtable 单元格上时显示图像

java - 是否有必要使用 JPA 和 MySQL 获取实体以引用它?

java - 在适配器中获取 Activity - Java 中的 Android studio

java - 重载paintComponent()

java - 如何处理在 try-catch 中抛出普通异常的方法

java - Java 中的可点击图像