簡體   English   中英

為什么我的Java代碼在eclipse中運行,但不能作為.exe運行。 或.class文件

[英]why does my java code run in eclipse but not as an .exe. or .class file

我有一個用eclipse在java中編寫的很酷的程序。 當我在Eclipse中運行它時,它的工作原理與預期的完全一樣。 我將其導出為jar文件,因此可以使用Launch4j將其轉換為成功完成的可執行文件(.exe文件擴展名),但是現在當我嘗試打開可執行文件時,它說程序不兼容。 我嘗試在命令行中編譯代碼,當我鍵入“ java Calculator”以嘗試運行程序時,它運行完美。 所以我的問題是為什么可執行文件不起作用? 任何幫助將不勝感激。 源代碼完整顯示在下面。

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兼容性疑難解答發布者詳細信息

發現的問題不兼容的程序不兼容的程序計算器不兼容。 檢測到的檢測到的修復應用程序計算器已完成發現的問題檢測到的詳細信息6檢測到的程序不兼容檢測到的檢測到的計算器不兼容。 修復應用程序計算器已完成提供修復不兼容程序的步驟。 InformationalCompatibility Mode應用的兼容模式:Windows XP(Service Pack 3)用戶驗證:已修復檢測詳細信息展開集合信息計算機名稱:JOSHVAYLELAPTOP Windows版本:6.2體系結構:x64時間:2013年8月7日,星期三7:40:32 PM發布者詳細信息展開程序兼容性疑難解答,查找並修復在此Windows版本上運行較舊程序的問題。 軟件包版本:1.5發布者:Microsoft Windows程序兼容性疑難解答在此版本的Windows上查找並修復與運行較舊程序有關的問題。 軟件包版本:1.0發布者:Microsoft Corporation

是否有理由需要一個.exe而不是一個可執行jar? 如果不確定,請看這里http://www.excelsior-usa.com/articles/java-to-exe.html它作為我的可執行jar正常運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM