java - 从其他类实例化时 JPanel 未打开

标签 java jframe jpanel

在我的主类中,我调用一个类,该类在实例化时应显示其 JFrame 窗口。然而,事实并非如此。我以前做过这门课,当时我通过 Eclipse 运行该项目。现在,通过命令行运行它,它不起作用:(。

从我的主要方法:

PaintTitleMovie q = new PaintTitleMovie();

Jframe 类:

public class PaintTitleMovie extends JPanel implements MouseListener {

    Image image;
    Font ConfettiFinal = new Font("Analog", 1, 20); // fail safe
    static JFrame frame = new JFrame();

    public PaintTitleMovie() {
        image = Toolkit.getDefaultToolkit().createImage("src/Title2.gif");
        try {
            Font Confetti = Font.createFont(Font.TRUETYPE_FONT, new File(
                    "src/Fonts/Confetti.ttf"));
            ConfettiFinal = Confetti.deriveFont(1, 50);
        } catch (FontFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (image != null) {
            g.drawImage(image, 0, 0, this);
        }
        // draw exit button
        g.setColor(Color.BLUE);
        g.fillRect(990, 50, 210, 100);
        g.setColor(Color.BLACK);
        g.fillRect(1000, 60, 190, 80);
        g.setColor(Color.WHITE);
        g.setFont(ConfettiFinal);
        g.drawString("Continue", 1000, 120);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                frame.add(new PaintTitleMovie());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(1200, 800);
                frame.setUndecorated(true);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                SongTitle s = new SongTitle();
            }
        });
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
        int x = arg0.getX();
        int y = arg0.getY();

        if (x >= 990 && y >= 50 && y <= 150) {
            this.setVisible(false);
            frame.dispose();
            PaintMenu load = new PaintMenu(); // load paint menu
        }
    }
}

最佳答案

这个src/Title2.gif会有问题,程序构建时src目录将不存在。

Toolkit.getDefaultToolkit().createImage(String) 还假设资源是文件系统上的文件,但应用程序上下文(或 jar 文件)中包含的任何内容都被视为嵌入的资源,不能被视为文件。

相反,您可能需要使用类似的东西

image = ImageIO.read(getClass().getResource("/Title2.gif"));

这将返回一个 BufferedImage,但如果无法加载图像,也会抛出一个 IOException。如果 gif 是动画 gif,您将需要使用类似的内容

image = new ImageIcon(getClass().getResource("/Title2.gif"));

您的字体也是如此,但在这种情况下您可能需要使用

Font Confetti = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream(
                "/Fonts/Confetti.ttf"));

如果您使用的是 Eclipse,则可能需要将这些资源移出 src 目录并移至与 src 同一级别的“resources”目录中目录,以便将它们包含在最终版本中。

关于java - 从其他类实例化时 JPanel 未打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23531632/

相关文章:

java - JPanels 不会完全拉伸(stretch)以占据可用空间

java - 套接字 : could not connect to server or specific port

java - "SocketException: Unexpected end of file from server"来自 servlet 但不是来自独立应用程序

java - 如何使用 KeyListener 移动 JFrame 中的矩形?

java - 在扩展 JPanel 的类中使用多个面板

java - JPanel java 未显示

java - .drawLine() 问题和缓冲图像

java - Spring Boot Data JPA 无法在 MockMVC 测试中 Autowiring 存储库接口(interface)

java - SMTP发送失败异常 590

java - JButton 多个命令