2016-08-17 59 views
1

對於我的Java應用程序,我想要類似於Android Toast的東西。爲了與我的同事分享並在將來重新使用該代碼片段,我將Toast項目創建爲自己的項目,成爲可導入的庫。無法顯示庫的JFrame

我沒有與吐司項目本身的問題。實際上,它可以像預期的那樣工作,直接在Eclipse的主要方法中進行測試。

之後,我將其作爲庫(jar)導出,並將其導入到我的主項目(具有Swing GUI)的類中。我打電話給特定的方法,它應該顯示敬酒,但沒有任何反應。沒有錯誤信息,只是沒有。 我添加了一些println的來查看我是否達到了代碼,我確實做了什麼。所以從我的庫中調用的方法應該被執行,但不知何故它不會。吐司是一個簡單,未裝飾和半透明的JFrame。因此,敬酒,代碼非常簡單:

// class extends JFrame 
    public Toast(String msg, int x, int y, int width, int height, Font font, Color background, Color foreground) { 

    setUndecorated(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setAlwaysOnTop(true); 
    setBackground(new Color(0f, 0f, 0f, 1f/3f)); 
    setBounds(x, y, width, height); 
    contentPane = new ToastPane(background); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    SpringLayout sl_contentPane = new SpringLayout(); 
    contentPane.setLayout(sl_contentPane); 
    JLabel label = new JLabel(msg); 
    label.setFont(font); 
    label.setForeground(foreground); 
    sl_contentPane.putConstraint(SpringLayout.WEST, label, 5, SpringLayout.WEST, contentPane); 
    sl_contentPane.putConstraint(SpringLayout.SOUTH, label, 85, SpringLayout.NORTH, contentPane); 
    sl_contentPane.putConstraint(SpringLayout.EAST, label, 385, SpringLayout.WEST, contentPane); 
    label.setHorizontalAlignment(SwingConstants.CENTER); 
    sl_contentPane.putConstraint(SpringLayout.NORTH, label, 5, SpringLayout.NORTH, contentPane); 
    getContentPane().add(label); 
} 

這是用來顯示來自外部的吐司(從我的主要項目)的方法:

public static void showToast(String msg, int x, int y, int width, int height, Font font, Color background, Color foreground, int displayTime) 
{ 
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 
    GraphicsDevice gd = ge.getDefaultScreenDevice(); 
    boolean isPerPixelTranslucencySupported = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT); 

    if(!isPerPixelTranslucencySupported) 
    { 
     System.out.println("Per-pixel translucency is not supported"); 
     System.exit(0); 
    } 

    Toast toast = new Toast(msg, x, y, width, height, font, background, foreground); 
    toast.setVisible(true); 

    closeToast(displayTime, toast); 
} 

最後一個方法處置的JFrame的一定量的毫秒數後的吐司:

private static void closeToast(int displayTime, Toast toast) 
{ 
    final Timer timer = new Timer(displayTime, null); 

    timer.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) { 
      toast.dispose(); 

     } 
    }); 

    timer.start(); 
} 

我的主要項目是調用敬酒方式如下:

public void showToast() 
{ 
    EventQueue.invokeLater(new Runnable(){ 
     public void run() 
     { 
      try 
      { 
      System.out.println("Going to show the toast!"); 
      Toast.showToast(message, x, y, width, height, font, background, foreground, displayTime); 
      } catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

println給我的反饋,我打印出"Going to show the toast!"達到Toast.showToast(...)行。

有沒有人知道,爲什麼吐司項目的主要方法是正確顯示吐司,但將其作爲庫導入不?

+0

*「類似於Android Toast。爲了與我的同事分享並在將來重新使用該代碼片段,我創建了我的Toast項目..」*我懷疑你可以創建一個Android基於Java的應用程序的基於項目。並按預期工作。用start&work的方式創建一個Java項目。 –

+0

如果您引用,請正確填寫。我寫了_「對於我的Java應用程序**,我希望與Android Toast類似**。這是一個基於Java的應用程序。就像我在我的帖子中所說的,我的「Toast-project」是一個帶有未修飾的透明JFrame(Swing)的Java應用程序。它僅與Android Toast類似,因爲它是一個黑色的小透明盒子,裏面有文字,它會在5秒內消失。我沒有使用Android Toast。我創建了一個自己的「Java-Toast」。所以從一開始它就是一個Java項目。 – KJaeg

+0

爲了更快得到更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

回答

0

我發現並解決了這個問題。

問題是在方法之前使用了static。將方法從static更改,稍微修改代碼以適應刪除關鍵字static

雖然它在Toast項目的主要方法中直接測試時工作正常,但導入Toast項目的另一個項目未正確顯示Toast。原因:在類構造函數中設置字段,但試圖通過靜態方法顯示吐司。