除了標籤之外,你如何在按鈕上創建帶有圖片的JOptionPane?例如,如果我想要確認按鈕上的複選標記和取消按鈕上的x圖標?這可能沒有創建整個對話從頭開始作爲JFrame/JPanel?JOptionPane圖像在按鈕上?
1
A
回答
2
我找到了一個稍微混亂的尋找解決方案上java 2 schools,似乎實際上是通過和動作偵聽器工作,響應按鈕點擊:
JFrame frame = new JFrame();
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage("I got an icon and a text label");
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
Icon icon = new ImageIcon("yourFile.gif");
JButton jButton = getButton(optionPane, "OK", icon);
optionPane.setOptions(new Object[] { jButton });
JDialog dialog = optionPane.createDialog(frame, "Icon/Text Button");
dialog.setVisible(true);
}
public static JButton getButton(final JOptionPane optionPane, String text, Icon icon) {
final JButton button = new JButton(text, icon);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
// Return current text label, instead of argument to method
optionPane.setValue(button.getText());
System.out.println(button.getText());
}
};
button.addActionListener(actionListener);
return button;
}
5
JOptionPane.showOptionDialog()
有一個參數options
這是一個數組Component
s。 你可以通過它的自定義按鈕數組:
JOptionPane.showOptionDialog(parent, question, title,
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE
new Component[]{ new JButton("OK", myIcon),
new JButton("cancel", myOtherIcon)
}
);
從JOptionPane
文檔:
選項 - 顯示可能的選擇,用戶 可以使對象的數組;如果對象是組件,它們會被正確渲染;
或者,您可以繼承JOptionPane
,並直接更改組件及其佈局。
+0
對於這似乎是正確的軌道解決方案的大部分但是,這對我來說並不完全適合。看起來好像有幾個參數需要指定(也許不是所有的Java版本都是相同的?我使用的是Java 7),但我沒有在按下OK或Cancel時添加空值。自定義按鈕是否需要動作偵聽器觸發「ok」動作? –
相關問題
- 1. 如何將圖像和字符串放在JOptionPane的按鈕中
- 2. JOptionPane CDialog Box按鈕?
- 3. JOptionPane按鈕排列
- 4. 在按鈕上添加圖像圖標?
- 5. 按鈕上的圖像
- 6. Java Swing JOptionPane按鈕選項
- 7. JOptionPane按鈕大小(Nimbus LAF)
- 8. 圖像不顯示在按鈕上
- 9. 在webview上設置圖像按鈕
- 10. css在圖像上顯示按鈕
- 11. 在按鈕圖像上設置編號
- 12. 圖像在iOS上顯示爲按鈕
- 13. 在全屏圖像上對齊按鈕
- 14. Android在按鈕上寫入圖像
- 15. 在按鈕上單擊顯示圖像
- 16. 如何在圖像上放置按鈕
- 17. 在Android的按鈕上設置圖像?
- 18. 在圖像上添加按鈕查看
- 19. 在圖像上定位按鈕
- 20. 在單選按鈕上翻轉圖像
- 21. 如何在圖像上放置按鈕
- 22. 旋轉按鈕上的圖像按
- 23. 按鈕圖像
- 24. c#按鈕上的圖像和文本,居中在按鈕中?
- 25. html按鈕不會隱藏在圖像按鈕上點擊
- 26. 在div按鈕上的手形圖標。像reall按鈕
- 27. C#如何在按鈕陣列按鈕上添加圖像
- 28. 如果圖像存在,在按鈕上顯示圖像
- 29. 圖像按鈕,圖像resoultion
- 30. 修改圖像視圖按鈕上點擊按鈕動態
一旦你開始自定義'JOptionPane'實例,就像你發現的那樣,它變得混亂。通常情況下,最好是側面使用「JDialog」。 +1來尋找解決方案,但也可以查看'Action' API來代替'ActionListener'。 –