gpt4 book ai didi

java - 如何分发 java GUI 构建器项目

转载 作者:太空宇宙 更新时间:2023-11-04 06:27:18 26 4
gpt4 key购买 nike

目前,我分发其他内容以从 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
}

这是显示我的项目文件和类路径的屏幕截图

enter image description here

最佳答案

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

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

Java Path ImageIcon URL .JAR

顺便说一句,发生异常时引发的堆栈跟踪在诊断错误时非常有值(value)。您绝对应该将其与任何进一步的问题一起发布。不要用这样的代码吞掉异常:

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

}

除非您完全确定该异常无关紧要(这种情况非常罕见!)。

关于java - 如何分发 java GUI 构建器项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26619263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com