
下面給出了示例代碼:
String message = 'You got a new notification message. Isn't it awesome to have such a notification message.';
String header = 'This is header of notification message';
JFrame frame = new JFrame();
frame.setSize(300,125);
frame.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1.0f;
constraints.weighty = 1.0f;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.fill = GridBagConstraints.BOTH;
JLabel headingLabel = new JLabel(header);
headingLabel .setIcon(headingIcon); // --- use image icon you want to be as heading image.
headingLabel.setOpaque(false);
frame.add(headingLabel, constraints);
constraints.gridx++;
constraints.weightx = 0f;
constraints.weighty = 0f;
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.NORTH;
JButton cloesButton = new JButton('X');
cloesButton.setMargin(new Insets(1, 4, 1, 4));
cloesButton.setFocusable(false);
frame.add(cloesButton, constraints);
constraints.gridx = 0;
constraints.gridy++;
constraints.weightx = 1.0f;
constraints.weighty = 1.0f;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.fill = GridBagConstraints.BOTH;
JLabel messageLabel = new JLabel('<HtMl>'+message);
frame.add(messageLabel, constraints);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
其輸出將是:

在這里,我創建了一個JFrame并添加了兩個標簽; 第一個是headingLabel,它是標題標簽,第二個是messageLabel,它將包含消息信息; 和一個關閉按鈕。 我已經使用了GridBagLayout,但是您可以使用任何選擇。
現在,要使該框架看起來像一個彈出窗口,我們必須從該框架中刪除標題欄和邊框。 為此,在frame.setSize(…)之后添加以下行: :
frame.setUndecorated(true);
現在的輸出將是:

請注意,由于沒有標題欄關閉按鈕,因此現在無法關閉我們的框架。 因此,要使我們的關閉按鈕可以用作框架關閉按鈕,請更改其聲明,如下所示:
JButton cloesButton = new JButton(new AbstractAction('x') {@Overridepublic void actionPerformed(final ActionEvent e) {frame.dispose();}
});
添加后,您將收到錯誤消息“無法引用用其他方法定義的內部類中的非最終變量框架”。 要擺脫此錯誤,您可以采用以下解決方案之一:
- 使您的框架變量最終。
- 使您的框架變量成為類中的全局變量。
- 使您的類擴展JFrame并完全刪除frame變量。
現在,當您運行程序時,它的外觀將與圖2相同,但是現在您可以通過單擊closeButton來關閉框架。
您會注意到您的框架出現在屏幕頂部,因此在創建框架后將其更改為屏幕右下角的位置,添加以下幾行:
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen
Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(frame.getGraphicsConfiguration());// height of the task bar
frame.setLocation(scrSize.width - frame.getWidth(), scrSize.height - toolHeight.bottom - frame.getHeight());
現在,當您運行它時,它將如下所示:

現在,要使其在預定時間后消失,請在末尾添加以下幾行:
new Thread(){@Overridepublic void run() {try {Thread.sleep(5000); // time after which pop up will be disappeared.frame.dispose();} catch (InterruptedException e) {e.printStackTrace();}};
}.start();
至此,您已經成功創建了一個彈出通知,該通知將出現在屏幕的右下角,如果未單擊關閉按鈕,則將在一段時間后消失。
因此,作為最后的修飾,您可以通過應用外觀和感覺或在框架中應用不同的顏色來根據需要進行設計。
您還可以通過添加以下內容使其顯示在所有窗口的頂部:
frame.setAlwaysOnTop(true);
在上面的代碼塊中需要注意的一些事情:
1. messageLabel中的<HtMl>標簽。 它是使標簽自動換行。 但是請確保您在消息中發短信的長度不超過特定的長度。 您可以根據需要調整此高度和彈出窗口的高度。
2. “ headingIcon” 未在代碼中聲明。 它是您要使用的圖像圖標,而不是屏幕快照中的作為標題標題圖標的惡魔圖標。 樣本聲明如下所示:
ImageIcon headingIcon = new ImageIcon(“ image_url”);
3. 當前,我們的彈出窗口會在任務欄中顯示一個新窗口,因此,如果您希望在任務欄中沒有彈出窗口,請將JFrame更改為JDialog。
4. 在上面的代碼中,默認的超時時間是5秒,您可以根據需要通過編輯以下代碼行來更新彈出窗口消失之前的默認超時:
Thread.sleep(5000); //彈出窗口消失的時間。
5. 要使關閉按鈕看起來像默認標題欄的關閉按鈕“ x”,則顯示在其文本中。 如果需要,可以將其關閉。
希望這對您有所幫助。
祝您編程愉快,別忘了分享!
參考: 用Java彈出創建新消息通知。 來自我們的JCG合作伙伴 Harsh Raval,來自harryjoy博客。
翻譯自: https://www.javacodegeeks.com/2012/10/create-new-message-notification-pop-up.html