当前位置: 首页 > 知识库问答 >
问题:

为什么我的java代码在eclipse中运行,而不是作为。exe运行。或.class文件

康恩
2023-03-14

我使用Eclipse用java编写了一个很酷的程序。当我在eclipse中运行它时,它完全按照预期工作。我将它导出为一个jar文件,这样我就可以使用Launch4j将它转换为一个可执行文件(.exe文件扩展名),我成功地完成了,但是现在当我试图打开可执行文件时,它说程序不兼容。我尝试在命令行编译代码,当我键入“Java计算器”试图运行程序时,它运行得很好。所以我的问题是为什么可执行文件不能工作?任何帮助都将不胜感激。源代码完整地显示在下面。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class Calculator implements ActionListener {

JFrame frame = new JFrame("Calculator");
JPanel panel = new JPanel(new GridLayout(4,4));

JTextArea text = new JTextArea(1,17);
JButton but1 = new JButton("1");
JButton but2 = new JButton("2");
JButton but3 = new JButton("3");
JButton but4 = new JButton("4");
JButton but5 = new JButton("5");
JButton but6 = new JButton("6");
JButton but7 = new JButton("7");
JButton but8 = new JButton("8");
JButton but9 = new JButton("9");
JButton but0 = new JButton("0");

JButton butadd = new JButton("+");
JButton butsub = new JButton("-");
JButton butmulti = new JButton("*");
JButton butdiv = new JButton("/");
JButton buteq = new JButton("=");

JButton butclear = new JButton("C");

double number1, number2, result;
int addc = 0, subc = 0, multic = 0, divc = 0;

public Calculator()
{
    frame.setVisible(true);
    frame.setSize(210,220);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    text.setEditable(false);
    frame.add(panel);
    frame.add(text, BorderLayout.NORTH);


    panel.add(but1);
    panel.add(but2);
    panel.add(but3);
    panel.add(but4);
    panel.add(but5);
    panel.add(but6);
    panel.add(but7);
    panel.add(but8);
    panel.add(but9);
    panel.add(but0);

    panel.add(butadd);
    panel.add(butsub);
    panel.add(butmulti);
    panel.add(butdiv);
    panel.add(buteq);
    panel.add(butclear);



    but1.addActionListener(this);
    but2.addActionListener(this);
    but3.addActionListener(this);
    but4.addActionListener(this);
    but5.addActionListener(this);
    but6.addActionListener(this);
    but7.addActionListener(this);
    but8.addActionListener(this);
    but9.addActionListener(this);
    but0.addActionListener(this);

    butadd.addActionListener(this);
    butsub.addActionListener(this);
    butmulti.addActionListener(this);
    butdiv.addActionListener(this);
    buteq.addActionListener(this);
    butclear.addActionListener(this);


}
public void colorChange(){
Random rand=new Random();
float r=rand.nextFloat(); 
float g= rand.nextFloat();
float b=rand.nextFloat();
Color randomColor=new Color(r,g,b);

but1.setBackground(randomColor); 
but2.setBackground(randomColor);
but3.setBackground(randomColor);
but4.setBackground(randomColor); 
but5.setBackground(randomColor); 
but6.setBackground(randomColor); 
but7.setBackground(randomColor);
but8.setBackground(randomColor);
but9.setBackground(randomColor);
but0.setBackground(randomColor); 
butadd.setBackground(randomColor);
butsub.setBackground(randomColor);
butmulti.setBackground(randomColor);
butdiv.setBackground(randomColor); 
butclear.setBackground(randomColor); 
buteq.setBackground(randomColor); 

but1.setForeground(Color.white); 
but2.setForeground(Color.white);
but3.setForeground(Color.white);
but4.setForeground(Color.white); 
but5.setForeground(Color.white); 
but6.setForeground(Color.white); 
but7.setForeground(Color.white);
but8.setForeground(Color.white);
but9.setForeground(Color.white);
but0.setForeground(Color.white); 
butadd.setForeground(Color.white);
butsub.setForeground(Color.white);
butmulti.setForeground(Color.white);
butdiv.setForeground(Color.white); 
butclear.setForeground(Color.white); 
buteq.setForeground(Color.white);


}


@Override
public void actionPerformed(ActionEvent arg0) 
{
    Object source = arg0.getSource();

    if(source == butclear)
    {
        number1 = 0.0;
        number2 = 0.0;
        text.setText("");
        colorChange();
    }

    if(source == but1)
    {
        text.append("1");
    }

    if(source == but2)
    {
        text.append("2");
    }

    if(source == but3)
    {
        text.append("3");
    }

    if(source == but4)
    {
        text.append("4");
    }

    if(source == but5)
    {
        text.append("5");
    }

    if(source == but6)
    {
        text.append("6");
    }

    if(source == but7)
    {
        text.append("7");
    }

    if(source == but8)
    {
        text.append("8");
    }

    if(source == but9)
    {
        text.append("9");
    }

    if(source == but0)
    {
        text.append("0");
    }

    if(source == butadd)
    {
        number1 = numberReader();
        text.setText("");
        addc = 1;
        subc = 0;
        multic = 0;
        divc = 0;
    }

    if(source == butsub)
    {
        number1 = numberReader();
        text.setText("");
        addc = 0;
        subc = 1;
        multic = 0;
        divc = 0;
    }

    if(source == butmulti)
    {
        number1 = numberReader();
        text.setText("");
        addc = 0;
        subc = 0;
        multic = 1;
        divc = 0;
    }

    if(source == butdiv)
    {
        number1 = numberReader();
        text.setText("");
        addc = 0;
        subc = 0;
        multic = 0;
        divc = 1;
    }

    if(source == buteq)
    {
        number2 = numberReader();

        if(addc > 0)
        {
            result = number1 + number2;
            text.setText(Double.toString(result));
        }

        if(subc > 0)
        {
            result = number1 - number2;
            text.setText(Double.toString(result));
        }

        if(multic > 0)
        {
            result = number1 * number2;
            text.setText(Double.toString(result));
        }

        if(divc > 0)
        {
            result = number1 / number2;
            text.setText(Double.toString(result));
        }
    }
    colorChange(); 
}

public double numberReader()
{
    Double num1;
    String s;
    s = text.getText();
    num1 = Double.valueOf(s);

    return num1;
}

public static void main(String[] args)
{
    new Calculator();
}
}

下面是错误代码。PrintProgram兼容性疑难解答发布服务器详细信息

共有1个答案

松元明
2023-03-14

它需要是一个。exe而不是一个可执行JAR有什么原因吗?如果您不确定,请查看http://www.excelsior-usa.com/articles/java-to-exe.html它作为一个可执行jar为我运行,没有任何问题。

 类似资料:
  • 我正在尝试将我的程序导出为一个可运行的JAR。该程序在eclipse中工作得非常好,但它不能作为一个可运行的JAR运行。我正在使用另外3个jar文件作为引用jar,这样我就可以使用音频,我认为这可能是问题所在。可运行的jar启动,但它只是一个全白的窗口,程序没有启动。 我点击我的项目,然后右键点击并选择“导出”,然后我选择“可运行的JAR”选项。我尝试使用所有三个处理引用库的选项来创建jar。 将

  • 我使用Python 3.7、JRE 8、JDK 1.8在Eclipse(Eclipse plugins:PyDev)上安装了带有Hadoop2.6的Pysark2.1。 在scala.collection.maplike$class.default(maplike.scala:228) 在scala.collection.abstractmap.default(map.scala:59) 在sca

  • 问题内容: 给定我要提取的字符串和。我为此创建了正则表达式。 我在RegexPlanet和regex101上测试了regex 。两种工具都能给我带来预期的结果,但是当我尝试在代码中使用它时,却找不到匹配的结果。我使用以下代码片段测试正则表达式: 输出是 我使用Java 8来编译和运行程序。为什么正则表达式不能与在线工具一起使用,而不能在我的程序中使用? 问题答案: 一个对象允许您对其进行多次查询,

  • 问题内容: 我有下面的代码。我只想检查代码块的运行时间。错误地,我再次复制并粘贴了相同的代码,并得到了有趣的结果。尽管代码块相同,但运行时间不同。而且 比其他人花费更多的时间。如果我切换代码块,则代码块4将比其他代码花费更多时间。 我在代码块中使用了两种不同类型的数组来检查它是否依赖于此。结果是一样的。如果代码块具有相同类型的数组,则最上面的代码块将花费更多时间。参见下面的代码和给出的输出。 运行

  • 首先,我安装了一个新的Laravel7然后, 我有这些错误, 再一次, 我能做什么? @dev C:\xampp\htdocs\my-task npm运行开发 @development C:\xampp\htdocs\my-task cross-env node_env=development node_modules/webpack/bin/webpack.js--progress--hide-

  • cross-env node_env=development node_modules/webpack/bin/webpack.js--progress--hide-modules--config=node_modules/laravel-mix/setup/webpack.config.js /users/xwu33/src/bioproc/node_modules/webpack-cli/bi