目前其他我分发从java运行时环境中获取“发生了Java异常”。我将如何正确地分发这个.jar?
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
}
以下是显示我的项目文件和类路径
的屏幕截图
答案 0 :(得分:1)
正如您的问题下面的评论中所指出的,错误的最明显原因是您依赖的资源是使用绝对地址引用的。这基本上意味着您的程序将无效,除非客户端具有与开发计算机完全相同的配置。
要使应用程序可分发,您应该将资源打包到JAR文件中。这个问题有一些有用的指示:
另外,在发生异常时抛出的堆栈跟踪在诊断错误时非常有用。你应该把它和任何进一步的问题一起发布在SO上。不要用这样的代码吞下异常:
try {
doSomethingWhichMightFail();
} catch(Exception e) {
}
除非您完全确定异常无关紧要(这种情况非常罕见!)。