香港專業教育學院建立了一個非常簡單的通知系統,我的應用程序,這是滿級:類慣於罐中運行,但運行從Netbeans的罰款
public class Notification extends JFrame {
Timer timer;
private static int count = 0;
private String from;
private String msg;
private String time;
private final JLabel jLabel1;
private final JLabel jLabel2;
private final JLabel jLabel3;
private final JLabel jLabel4;
public void NotificationStart(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
}
class RemindTask extends TimerTask {
public void run() {
System.out.format("Remove notification");
timer.cancel(); //Terminate the timer thread
dispose(); // Remove the window
count--;
}
}
public Notification(String from, String msg, String time) {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
getContentPane().setLayout(null);
setSize(308,77);
setBackground(new Color(0, 255, 0, 0));
setLocationRelativeTo(null);
setUndecorated(true);
setAlwaysOnTop(true);
jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 12));
jLabel2.setText(from + ":");
jLabel2.setBounds(38, 11, 240, 15);
jLabel1.add(jLabel2);
jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 12));
jLabel3.setText(msg);
jLabel3.setBounds(38, 25, 240, 50);
jLabel1.add(jLabel3);
jLabel4.setBounds(280, 6, 16, 16);
jLabel1.add(jLabel4);
// Start timer
NotificationStart(8);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/notification.png")));
getContentPane().add(jLabel1);
jLabel1.setBounds(0, 0, 308, 77);
// Position it
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
Rectangle rect = defaultScreen.getDefaultConfiguration().getBounds();
int x = (int) rect.getMaxX() - (this.getWidth() + 10);
int y;
if(this.count == 0) {
y = (int) rect.getMinY() - 46 + this.getHeight();
} else {
y = (int) rect.getMinY() + 30 + (this.getHeight() * this.count);
}
this.setLocation(x, y);
this.setVisible(true);
this.count = count + 1;
jLabel1.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
// Remove notification if user clicks it
dispose();
}
public void mouseEntered(MouseEvent e) {
// Show the close icon
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/icnClose.png")));
}
public void mouseExited(MouseEvent e) {
// Hide the close icon
jLabel4.setIcon(null);
}
});
}
產卵通知我用下面的代碼在我的主要的JFrame :
new Notification("Thomas", "New notification!", "13.25");
只要我在NetBeans中運行代碼,一切就像我希望的那樣工作。如果我從NetBeans執行Clean和Build並嘗試運行可執行文件jar,應用程序就會停止。
我知道這個錯誤是在這個類中的某種原因,如果我沒有從我的主JFrame的任何地方調用Notification類,那麼jar執行得很好。
任何想法?
這會打印任何異常? – rajesh
無法找到任何信息,所以永遠:// – Alosyius
你說它的可執行jar。你可以添加一個測試日誌作爲你的main()中的第一行,看它是否打印 – rajesh