我在Eclipse中创建了一个Swing应用程序,在完成之后我编译了它,没有任何错误或警告,但是在编译之后,我打开了jar文件并且没有显示窗口。它显示它正在运行码头,但没有别的。但是,当我在Eclipse中运行它时,它可以正常工作。
我尝试使用Archive Utility进行反编译并手动运行,它也能正常工作。
我将包含所有代码和目录:
Unarchived Manifest File
Manifest-Version: 1.0
Class-Path: .
Main-Class: swing.MainSwing
目录(如Eclipse中所示):
| Cookie Clicker
| src
| swing
| DetailsPanel.java
| MainFrame.java
| MainSwing.java
| package-info.java
| PM.java
| src/res
| cookie_icon.png
| cookie.png
MainSwing.java
package swing;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainSwing extends JFrame {
private static final long serialVersionUID = -8231889836024827530L;
public static void main(String[] args) {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
JFrame frame = new MainFrame("Cookie Clicker");
frame.setSize(500, 500);
frame.setResizable(false);
ClassLoader classLoader = getClass().getClassLoader();
if (PM.getImage("cookie_icon.png", classLoader) != null) {
frame.setIconImage(PM.getImage("cookie_icon.png", classLoader));
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
DetailsPanel.java
package swing;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class DetailsPanel extends JPanel {
private static final long serialVersionUID = -8854226121488997931L;
private JLabel cookies;
private long cookiesBefore = MainFrame.cookies;
public DetailsPanel() {
this.setBackground(new Color(0, 51, 102, 255));
this.setForeground(Color.WHITE);
TitledBorder border = BorderFactory.createTitledBorder("Stats");
border.setTitleColor(Color.WHITE);
setBorder(border);
this.cookies = new JLabel("Cookies in total: " + MainFrame.cookies);
this.cookies.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.cookies.setForeground(Color.WHITE);
JLabel cookiesPerSec = new JLabel("Per second: ");
cookiesPerSec.setForeground(Color.WHITE);
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
PM.setGC(gc, 0, 0);
gc.anchor = GridBagConstraints.PAGE_START;
PM.setGCWeight(gc, 2, 0);
add(cookies, gc);
PM.setGC(gc, 0, 1);
add(cookiesPerSec, gc);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new updateSeconds(cookiesBefore, cookiesPerSec), 0, 1000);
}
public void updateCookies() {
String amount = String.valueOf(MainFrame.cookies);
cookies.setText("Cookies in total: " + amount);
}
class updateSeconds extends TimerTask {
long cookiesBefore;
JLabel cookiesPerSec;
public updateSeconds(long cookiesBefore, JLabel cookiesPerSec) {
this.cookiesBefore = cookiesBefore;
this.cookiesPerSec = cookiesPerSec;
}
@Override
public void run() {
long cookiesNow = MainFrame.cookies;
cookiesPerSec.setText("Per second: " + String.valueOf(cookiesNow - cookiesBefore));
cookiesBefore = cookiesNow;
}
}
}
MainFrame.java
package swing;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 7853386173600171822L;
private DetailsPanel detailspanel;
public static long cookies = 0;
private BufferedImage img_cookie = null;
public MainFrame(String title) {
super(title);
// Layout
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
// Create Components
JButton reset = new JButton("Reset");
reset.setOpaque(true);
reset.setBackground(new Color(30, 30, 30, 255));
reset.setForeground(Color.WHITE);
reset.setBorder(BorderFactory.createEmptyBorder());
reset.setFocusPainted(false);
JButton cookie = new JButton();
detailspanel = new DetailsPanel();
//Set Button image
ClassLoader classLoader = getClass().getClassLoader();
BufferedImage temp = PM.getImage("cookie.png", classLoader);
if (temp != null) {
img_cookie = PM.getImage("cookie.png", classLoader);
cookie.setIcon(new ImageIcon(img_cookie.getScaledInstance(80, 80, Image.SCALE_SMOOTH)));
cookie.setBorder(BorderFactory.createEmptyBorder());
} else {
cookie.setText("add");
}
// Cookie button animation
cookie.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt) {
if (img_cookie != null) {
cookie.setIcon(new ImageIcon(
img_cookie.getScaledInstance(100, 100, Image.SCALE_SMOOTH)
));
}
}
public void mouseExited(MouseEvent evt) {
if (img_cookie != null) {
cookie.setIcon(new ImageIcon(
img_cookie.getScaledInstance(80, 80, Image.SCALE_SMOOTH)
));
}
}
});
// Add Components
Container c = getContentPane();
c.setBackground(new Color(255,247,217,255));
PM.setGC(gc, 0, 1);
PM.setGCWeight(gc, 10, 0);
gc.fill = GridBagConstraints.HORIZONTAL;
gc.gridwidth = 2;
c.add(reset, gc);
PM.setGC(gc, 0, 0);
PM.setGCWeight(gc, 1, 2);
gc.fill = GridBagConstraints.VERTICAL;
gc.gridwidth = 1;
gc.anchor = GridBagConstraints.WEST;
c.add(detailspanel, gc);
PM.setGC(gc, 1, 0);
PM.setGCWeight(gc, 1, 0);
gc.fill = GridBagConstraints.CENTER;
c.add(cookie, gc);
// Listener
cookie.addActionListener(new CookieClickListener());
reset.addActionListener(new ResetClickListener(reset));
reset.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent evt) {
reset.setBackground(new Color(46, 46, 46, 255));
}
@Override
public void mousePressed(MouseEvent evt) {
reset.setBackground(new Color(10, 10, 10, 255));
}
@Override
public void mouseReleased(MouseEvent evt) {
reset.setBackground(new Color(30, 30, 30, 255));
}
@Override
public void mouseExited(MouseEvent evt) {
reset.setBackground(new Color(30, 30, 30, 255));
}
});
}
// Add cookie after click
private class CookieClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
cookies++;
detailspanel.updateCookies();
}
}
// Reset
private class ResetClickListener implements ActionListener {
JButton reset;
public ResetClickListener(JButton reset) {
this.reset = reset;
}
@Override
public void actionPerformed(ActionEvent e){
cookies = 0;
detailspanel.updateCookies();
reset.setBackground(new Color(10, 10, 10, 255));
}
}
}
PM.java
package swing;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.imageio.ImageIO;
public class PM {
public static void setGC(GridBagConstraints gc, int col, int row) {
gc.gridx = col;
gc.gridy = row;
}
public static void setGCWeight(GridBagConstraints gc, double x, double y) {
gc.weightx = x;
gc.weighty = y;
}
public static BufferedImage rotate(BufferedImage image, double angle) {
int w = image.getWidth();
int h = image.getHeight();
BufferedImage newImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphic = newImage.createGraphics();
graphic.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphic.rotate(Math.toRadians(angle), w/2, h/2);
graphic.drawImage(image, null, 0, 0);
return newImage;
}
public static BufferedImage getImage(String path, ClassLoader classLoader) {
try {
File f = new File(classLoader.getResource(path).toURI());
return ImageIO.read(f);
} catch (IOException e) {
System.out.println("Error occurred.");
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
return null;
}
}
}
package-info.java
/**
*
*/
/**
* @author mac
*
*/
package swing;
修改 我发现了这个问题。这基于2个链接:
首先找出错误:Here
第二步找到解决方案:Here
所以问题是在PM.java中我无法做到:
File f = new File(classLoader.getResource(path).toURI());
return ImageIO.read(f);
但我必须将资源作为这样的流来获取:
return ImageIO.read(classLoader.getResourceAsStream(path));
答案 0 :(得分:1)
要运行MainSwing
,您需要位于包含类文件的文件夹的父级中(例如," swing"的父级,上面给出{{1} })。然后你可以像
~/Cookie Clicker/
注意该包是类名的一部分。有关详细信息,请参阅Setting an Application's Entry Point: The Java Tutorials。