对于初学者来说,我为“正常”(非托盘)应用程序构建的程序可以与电子打包程序和此命令一起正常运行
import acm.program.*;
import javax.swing.*;
import java.awt.*; // for graphics
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import acm.gui.*;
public class Calculator extends DialogProgram implements ActionListener{
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bm,bd,ba,bs,bdec,bc,bequ;
DoubleField screen = new DoubleField();
JPanel P,P1,P2;
Accumulator acc = new Accumulator();
double result;
public void init() {
//create a panel
JPanel P= new JPanel(); // main panel
JPanel P1= new JPanel(); // panel for buttons
JPanel P2= new JPanel(); // panel for screen
// adding panel to dialog box
add(P); // main panel
P.add(P2); // adding screen
P.add(P1); // adding panel of buttons
// set panel color
P.setBackground(Color.yellow);
// grid layout for buttons
P1.setLayout(new GridLayout(5,4,4,4)); // layout for button panel
P.setLayout(new GridLayout(2,1)); // layout for main panel
// create screen
//screen = new DoubleField(); // creating screen
screen.setPreferredSize(new Dimension(200,40)); // setting screen size
screen.setEditable(false); // screen can not be edited
// integer buttons
b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");
// operator buttons
bm = new JButton("*");
bd= new JButton("/");
ba = new JButton("+");
bs = new JButton("-");
// decimal button
bdec = new JButton(".");
// clear button
bc = new JButton("Clr");
// equal button
bequ = new JButton("=");
// adding buttons to panel
P2.add(screen);
P1.add(b7);
P1.add(b8);
P1.add(b9);
P1.add(bm);
P1.add(b4);
P1.add(b5);
P1.add(b6);
P1.add(bd);
P1.add(b1);
P1.add(b2);
P1.add(b3);
P1.add(ba);
P1.add(bc);
P1.add(b0);
P1.add(bdec);
P1.add(bs);
P1.add(bequ);
// adding action listeners
bm.addActionListener(this);
bd.addActionListener(this);
bs.addActionListener(this);
ba.addActionListener(this);
b9.addActionListener(this);
b8.addActionListener(this);
b7.addActionListener(this);
b6.addActionListener(this);
b5.addActionListener(this);
b4.addActionListener(this);
b3.addActionListener(this);
b2.addActionListener(this);
b1.addActionListener(this);
b0.addActionListener(this);
bdec.addActionListener(this);
bc.addActionListener(this);
bequ.addActionListener(this);
//addActionListeners();
} // close
//public static void main(String[] args) {
// new Calculator().start(args);
//}
public void actionPerformed (ActionEvent e) {
String clicked =e.getActionCommand();
if (clicked.equals("1")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("2")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("3")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("4")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("5")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("6")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("7")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("8")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("9")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("0")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("*")) {
// screen.setText(screen.getText()+clicked);
acc.calculate(screen.getValue(), "*");
screen.setText("");
}
if (clicked.equals("/")) {
// screen.setText(screen.getText()+clicked);
acc.calculate(screen.getValue(), "/");
screen.setText("");
}
if (clicked.equals("+")) {
// screen.setText(screen.getText()+clicked);
acc.calculate(screen.getValue(), "+");
screen.setText("");
}
if (clicked.equals("-")) {
acc.calculate(screen.getValue(), "-");
screen.setText("");
}
if (clicked.equals(".")) {
screen.setText(screen.getText()+clicked);
}
if (clicked.equals("Clr")) {
acc.reset();
screen.setText("");
}
if (clicked.equals("=")) {
// screen.setText("");
acc.calculate(screen.getValue(), "=");
screen.setValue(acc.getValue());
// screen.setText(result);
}
}/*action Performed*/
public class Accumulator{
double valueSoFar;
String currentOperator;
Accumulator(){
valueSoFar=0.0;
currentOperator="C";
}
/*constructor*/
public void calculate(double numberOnDisplay, String nextOperator) {
if (currentOperator.contentEquals("+")) {
valueSoFar= valueSoFar + numberOnDisplay;
}
if (currentOperator.contentEquals("-")) {
valueSoFar= valueSoFar - numberOnDisplay;
}
if (currentOperator.contentEquals("*")) {
valueSoFar= valueSoFar * numberOnDisplay;
}
if (currentOperator.contentEquals("/")) {
valueSoFar= valueSoFar / numberOnDisplay;
}
if (currentOperator.equals("C")) {
valueSoFar = numberOnDisplay;
// currentOperator = nextOperator;
}
// getValue();
//}
currentOperator = nextOperator;
}/*calculate*/
public double getValue() {
return valueSoFar;
} /*getValue*/
public double reset() {
return valueSoFar;
}
}/*Accumulator*/
} /*Class*/
我最近重构了我的代码,所以现在是一个托盘应用程序
所以我有
npx electron-packager . --overwrite --plaorm=darwin --arch=x64 --icon=aicons/1024.icns --prune=true --out=release-builds
这与npm start一起正常工作,但是当我使用电子打包程序构建.app时,出现此错误:
tray = new Tray('icons/elektro.png')
tray.setToolTip('elektro')
我认为问题与用于托盘图标的.png有关。如果我将其更改为.icns格式,则无法通过npm start正常运行。您知道如何解决此问题吗?
我正在使用电子6
答案 0 :(得分:0)
好的,问题与路径有关。看起来微不足道,但是.app文件在大多数情况下都可以正常死掉而没有任何错误。这个问题在任何地方的记录都很少,所以我把它留给任何发现相同问题的人:
const path = require('path');
var iconPath = path.join(__dirname, '/icons/elektro.png') // your png tray icon
let trayIcon = nativeImage.createFromPath(iconPath);
// if needed resize to 16x16 but mac also accepted the 24 icon
// trayIcon = trayIcon.resize({
// width: 16,
// height: 16
// });
tray = new Tray(trayIcon)