【问题标题】:Java Jframe not opening on exportJava Jframe 未在导出时打开
【发布时间】:2013-10-28 10:45:50
【问题描述】:

我试图在 Runnable Jar 中显示一些图像,当我在 eclipse 中运行它时它工作正常,但是当我导出它时,它只是不加载,文件在 jar 中但它只是不会运行,我一直试图破解为什么,我原本以为是图像,但我将它从项目中完全删除,它仍然无法运行,我认为它与资源加载有关,但老实说我不知道​​,这是我的代码。

public class Main {

    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException ex) {
        }

        final JFrame frame = new JFrame("Window");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.pack();
        final JFrame frame1 = new JFrame("Window2");
        frame.setLocation(600, 300);
        frame.setSize(140, 125);
        Dimension minSize = new Dimension(800, 600);
        Dimension maxSize = new Dimension(800, 600);
        frame.setMinimumSize(minSize);
        frame.setMinimumSize(maxSize);
        frame.setResizable(false);
        frame.setSize(784, 561);
        frame.getContentPane().setLayout(null);

        JLabel label = new JLabel("");
        label.setIcon(new ImageIcon(Main.class
                .getResource("/res/BlueSpace.jpg")));
        label.setBounds(0, 0, 794, 493);
        frame.getContentPane().add(label);

        JButton btnNewButton = new JButton("Install Modpack");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.out
                        .println("Developer - The Modpack has began Installation!");

                frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame1.pack();
                frame1.setLocation(600, 300);
                frame1.setSize(140, 125);
                Dimension minSize = new Dimension(800, 600);
                Dimension maxSize = new Dimension(800, 600);
                frame1.setMinimumSize(minSize);
                frame1.setMinimumSize(maxSize);
                frame1.setResizable(false);
                frame1.setSize(784, 561);
                frame1.getContentPane().setLayout(null);

                JLabel label = new JLabel("");
                label.setIcon(new ImageIcon(Main.class
                        .getResource("/res/BlueSpace.jpg")));
                label.setBounds(0, 0, 794, 493);
                frame1.getContentPane().add(label);

                JProgressBar progressBar = new JProgressBar();
                progressBar.setBounds(10, 504, 774, 14);
                frame1.getContentPane().add(progressBar);
                progressBar.setValue(25);

                JLabel lblInstallingModpack = new JLabel(
                        "Installing Modpack...");
                lblInstallingModpack.setToolTipText("Modpack Is Installing!");
                lblInstallingModpack
                        .setHorizontalAlignment(SwingConstants.CENTER);
                lblInstallingModpack.setBounds(300, 529, 150, 14);
                frame1.getContentPane().add(lblInstallingModpack);
                frame1.setVisible(true);
                JOptionPane.showMessageDialog(frame1,
                        "I'm 25% done on the Modpack!", "Not Done Yet...",
                        JOptionPane.WARNING_MESSAGE);

                frame.setVisible(false);
            }
        });
        btnNewButton.setEnabled(false);
        btnNewButton.setBounds(531, 504, 253, 56);
        frame.getContentPane().add(btnNewButton);
        frame.setVisible(true);
        btnNewButton.setEnabled(true);

        JLabel lblWelcomeToThe = new JLabel("Welcome to the Realism Modpack");
        lblWelcomeToThe.setBounds(10, 504, 511, 14);
        frame.getContentPane().add(lblWelcomeToThe);

        JLabel lblNewLabel = new JLabel(
                "All Mods owned by there respecive owners");
        lblNewLabel.setBounds(10, 546, 511, 14);
        frame.getContentPane().add(lblNewLabel);

        JButton btnFinish = new JButton("Creators");
        btnFinish.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(frame,
                        "Ribenja - ModPack Author", "A List of Great People",
                        JOptionPane.OK_OPTION);
                JOptionPane.showMessageDialog(frame, "Dave - The 'Enthusiast'",
                        "A List of Great People", JOptionPane.OK_OPTION);
                JOptionPane.showMessageDialog(frame, "Carl - Ate his hands",
                        "A List of Great People", JOptionPane.OK_OPTION);
                JOptionPane.showMessageDialog(frame,
                        "Surf dude - This message was simple",
                        "A List of Great People", JOptionPane.OK_OPTION);
                JOptionPane.showMessageDialog(frame,
                        "ThatGuy2000 - A true Hero", "A List of Great People",
                        JOptionPane.OK_OPTION);
                JOptionPane.showMessageDialog(frame, "*** - An NSA operative",
                        "A List of Great People", JOptionPane.OK_OPTION);
                JOptionPane.showMessageDialog(frame,
                        "Steve - A true Minecrafter", "A List of Great People",
                        JOptionPane.OK_OPTION);
                JOptionPane.showMessageDialog(frame, "Downloader - you",
                        "A List of Great People", JOptionPane.OK_OPTION);
                JOptionPane.showMessageDialog(frame,
                        "I KNOW WHAT YOU DID - A true story teller",
                        "A List of Great People", JOptionPane.OK_OPTION);

            }

        });
        btnFinish.setEnabled(true);
        btnFinish.setBounds(268, 504, 253, 56);
        frame.getContentPane().add(btnFinish);
    }
}

【问题讨论】:

  • 当你从命令行运行 jar 文件时会抛出任何异常吗? “它不会运行”到底是什么意思?
  • MDrabic,它在命令行中运行非常好,而且工作正常!但是为什么我不能点击它呢?
  • 抱歉,我以为我订阅了电子邮件,回复晚了。

标签: java eclipse swing jframe embedded-resource


【解决方案1】:

我怀疑您的 JAR 文件没有打包资源文件,在您的情况下是图像文件。确保您的 JAR 有一个“res”目录,其中包含框架所需的所有图像。

【讨论】:

    【解决方案2】:

    用合成变体替换这两个图像后,应用程序。出现在屏幕上。

    其他提示

    1. 不要设置顶级容器的大小。而是布局内容并致电pack()。
    2. Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them1,以及 white space2 的布局填充和边框。

    【讨论】:

      【解决方案3】:

      谢谢大家,解决了问题是java安装损坏。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多