有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing如何分发JavaGUI构建器项目

目前,我分发的其他文件是为了从Java运行时环境中获取“发生了Java异常”。我将如何将此作为一个整体进行适当分发。罐子

package my.onis;

import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import static java.lang.Thread.sleep;
import javax.swing.JOptionPane;

public class onis extends javax.swing.JFrame
{
    public onis()
    {
        initComponents();
    }

    public int oxygen = 100;

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btn = new javax.swing.JLabel();
        pb = new javax.swing.JProgressBar();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("One Night In Space");
        setAlwaysOnTop(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jPanel1.setPreferredSize(new java.awt.Dimension(1135, 633));
        jPanel1.setLayout(null);

        btn.setIcon(new javax.swing.ImageIcon("C:\\Users\\rando_000\\Desktop\\butt.png")); // NOI18N
        btn.setPreferredSize(new java.awt.Dimension(200, 100));
        btn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnMouseClicked(evt);
            }
        });
        jPanel1.add(btn);
        btn.setBounds(430, 200, 200, 100);

        pb.setBackground(new java.awt.Color(0, 0, 0));
        pb.setForeground(new java.awt.Color(255, 255, 51));
        pb.setValue(oxygen);
        pb.setBorderPainted(false);
        pb.setFocusable(false);
        pb.setStringPainted(true);
        jPanel1.add(pb);
        pb.setBounds(30, 564, 280, 30);

        jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Users\\rando_000\\Desktop\\Background.jpeg")); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(0, 0, 1051, 633);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 1051, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        

    private void btnMouseClicked(java.awt.event.MouseEvent evt) {                                 
        try{BufferedImage image = javax.imageio.ImageIO.read(new java.io.File("C:\\Users\\rando_000\\Desktop\\butt.png"));
        boolean transparent = (image.getRGB(evt.getX(),evt.getY()) & 0x00ffffff)==0;
        if(!transparent)
        {
            mousePressed(evt);
        }}catch(IOException e){};
    }                                

    private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     
        check();
    }                                    
    public void mousePressed(MouseEvent e) 
    {
    if(e.getButton() == MouseEvent.BUTTON1 && oxygen > 0)
    {
            oxygen -= 10;
    }
    else if(e.getButton() == MouseEvent.BUTTON3 && oxygen < 100)
    {
        oxygen += 10;
    }
    }
    public void startUpdating()
    {
        for (int j = 0; j < 1000000; j++)
        {

            pb.setValue(oxygen);

            if (oxygen <= 0)
            {
                JOptionPane.showMessageDialog(this, "You have died.");
                System.exit(0);
            }

            try{sleep(10);}catch(InterruptedException e){};
        }
    }
    public void check()
    {
        Thread update = new Thread()
        {
            public void run()
            {
                startUpdating();
            }
        };
        update.start();
    }
    public static void main(String args[])
    {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(onis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(onis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(onis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(onis.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new onis().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JLabel btn;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    public javax.swing.JProgressBar pb;
    // End of variables declaration                   
}

下面是一个屏幕截图,显示我的项目文件和类路径

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    正如您的问题下面的评论中所指出的,导致错误的最明显原因是您所依赖的资源是使用绝对地址引用的。这基本上意味着您的程序将无效,除非客户端的配置与您的开发机器完全相同

    为了使应用程序可分发,您应该将资源打包到JAR文件中。这个问题有一些有用的提示:

    Java Path ImageIcon URL .JAR

    另一方面,在诊断错误时,异常发生时抛出的stacktrace是非常宝贵的。你一定要把它和其他问题一起贴出来。不要使用如下代码吞咽异常:

    try {
        doSomethingWhichMightFail();
    } catch(Exception e) {
    
    }
    

    除非你绝对确定这个异常是无关紧要的(这是非常罕见的!)