图像没有出现在带有wordGen的jbutton中

时间:2015-02-04 02:23:30

标签: java swing jframe jpanel jbutton

使用WordGen运行图像时不会被绘制,我该如何解决? 当我没有wordgen运行时,我可以让图像出现。我不确定我做错了什么,因为我没有收到任何错误。 任何帮助表示赞赏。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

    public class tfot extends JComponent{
        private static final long serialVersionUID = 1L;
        public static void main(final String[] args) {      




SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    showGUI(args);
                }
            });
        }

        public static void showGUI(String[] args) {
            JPanel displayPanel = new JPanel();
            JButton okButton = new JButton("Did You Know?"); 
            okButton.setFont(new Font("Times", Font.TRUETYPE_FONT, 100));
            final JLabel jLab = new JLabel();
            okButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    jLab.setText(wordGen());

                }
            });
            JPanel content = new JPanel();
            content.setLayout(new BorderLayout());
            content.add(displayPanel, BorderLayout.CENTER);
            content.add(okButton, BorderLayout.SOUTH);
            content.add(jLab, BorderLayout.NORTH);

           JFrame window = new JFrame("Window");
            window.setContentPane(content);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setSize(800, 600);
            window.setLocation(400, 300);
            window.setVisible(true);
        }
            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.drawImage(Toolkit.getDefaultToolkit().getImage("Pictures/background1.png"), 0, 0, this);

            }


        public static String wordGen() {

            String[] wordListOne = {"generic text","hi",}; 



            int oneLength = wordListOne.length;

            int rand1 = (int) (Math.random() * oneLength);

            String phrase = wordListOne[rand1] + " ";
            return phrase;
        }

    }

1 个答案:

答案 0 :(得分:0)

首先...

不要在绘制方法中加载资源或执行长时间运行的任务,这些可能会连续多次调用。相反,请先手动加载图像并根据需要进行绘制......

public Tfot() {
    setLayout(new BorderLayout());
    try {
        background = ImageIO.read(new File("pictures/background1.png"));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (background != null) {
        g.drawImage(background, 0, 0, this);
    }
}

一般情况下,我们不鼓励你覆盖paint,而应该使用paintComponent,原因很多,但一般来说,这就是背景画的地方......

其次...

您需要将Tfot添加到可显示的内容中,否则它将永远不会被绘制

JFrame window = new JFrame("Window");
window.setContentPane(new Tfot());
window.add(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(800, 600);
window.setLocation(400, 300);
window.setVisible(true);

THRID ...

JPanel默认情况下不透明,您需要将其opaque属性设置为false

JPanel displayPanel = new JPanel();
displayPanel.setOpaque(false);
//...
JPanel content = new JPanel();
content.setOpaque(false);

然后它将允许它下面出现的东西(即背景图像)

查看Painting in AWT and SwingPerforming Custom PaintingReading/Loading an Image了解详情

第四...

你需要学习语言基础知识,以便开始使用GUI和自定义绘画等高级主题,如果没有这些基础知识,这个主题会让你很难受。

您需要将background声明为类Tfot

的实例字段
private BufferedImage background;
public Tfot() {

已更新 - 完全可运行的示例

DidYouKnow

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Tfot extends JComponent {

    private static final long serialVersionUID = 1L;

    public static void main(final String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                showGUI(args);
            }
        });
    }

    public static void showGUI(String[] args) {
        JPanel displayPanel = new JPanel();
        displayPanel.setOpaque(false);
        JButton okButton = new JButton("Did You Know?");
        okButton.setFont(new Font("Times", Font.TRUETYPE_FONT, 100));
        final JLabel jLab = new JLabel();
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jLab.setText(wordGen());

            }
        });
        JPanel content = new JPanel();
        content.setOpaque(false);
        content.setLayout(new BorderLayout());
        content.add(displayPanel, BorderLayout.CENTER);
        content.add(okButton, BorderLayout.SOUTH);
        content.add(jLab, BorderLayout.NORTH);

        Tfot tfot = new Tfot();
        tfot.setLayout(new BorderLayout());
        tfot.add(content);

        JFrame window = new JFrame("Window");
        window.setContentPane(tfot);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(800, 600);
        window.setLocation(400, 300);
        window.setVisible(true);
    }

    private BufferedImage background;

    public Tfot() {
        try {
            background = ImageIO.read(new File("Pictures/background1.png"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

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

    }

    public static String wordGen() {

        String[] wordListOne = {"generic text", "hi",};

        int oneLength = wordListOne.length;

        int rand1 = (int) (Math.random() * oneLength);

        String phrase = wordListOne[rand1] + " ";
        return phrase;
    }

}