我有一個ColorChooser面板,當我在程序中單擊一個JButton時,該如何顯示? 編輯: 我想使它出現在一個新的框架是可調整大小,可移動和可關閉。如何在單擊JButton時使JPanel出現?
回答
你需要爲你的JBu編寫一個ActionListener tton。
事情是這樣的:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author roger
*/
public class MyActListener extends JFrame implements ActionListener{
public MyActListener(){
super("My Action Listener");
JButton myButton = new JButton("DisplayAnything");
myButton.addActionListener(this);
this.add(myButton);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
// TODO code application logic here
MyActListener ma = new MyActListener();
}
@Override
public void actionPerformed(ActionEvent e) { // YOur code for your button here
if("DisplayAnything".equals(e.getActionCommand())){
Color c = JColorChooser.showDialog(this, "Color Chooser", Color.BLACK);
JButton displayAnything = (JButton)e.getSource();
displayAnything.setBackground(c);
}
}
看看的How to write an ActionListener的Java tutorials。看看那裏真的很大的索引,通常可以看到關於java的基本教程。
如果這是爲JButton編寫動作監聽器的正確方法,我能否從其他讀者那裏獲得反饋?當按鈕的數量開始增加時,爲它實現ActionListener的類是否更好?或者不是'myButton.addActionListener(this);'make'myButton.addActionListener(new MouseListener(){...})' – Roger 2012-03-26 20:31:55
我個人比較喜歡使用匿名內部類,比如註釋中的最後一個例子,並調用類從那裏實施邏輯。這樣,就不需要定義動作命令了,正如你注意到的那樣,當添加新按鈕時,你的actionPerformed方法會變得相當長。所有的例子都是有效的,當然在使用。 – mort 2012-03-26 20:41:26
謝謝!還有一個問題,我怎麼才能使按鈕顯示我選擇的顏色? – 2012-03-26 20:46:50
你可以看一下在Java Swing指南 - ColorChooserDemo2: http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html#advancedexample
基本上JColorChoose可以在對話框中顯示: http://docs.oracle.com/javase/6/docs/api/javax/swing/JColorChooser.html
Color newColor = JColorChooser.showDialog(
ColorChooserDemo2.this,
"Choose Background Color",
banner.getBackground());
對於按鈕來激活此文件選擇:
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//color is whatever the user choose
Color color = JColorChooser.showDialog(currentComponent, "Color Chooser", Color.WHITE);
JButton thisBtn = (JButton)e.getSource(); //or you can just use button if that's final or global
thisBtn.setBackground(color);
}
});
謝謝:) 如何在我的JButton的ActionPerformed中實現該功能? – 2012-03-26 20:21:05
好的,謝謝,還有一個問題,我怎樣才能使按鈕顯示我選擇的顏色? – 2012-03-26 20:46:38
- 1. 如何在單擊JFrame上的JButton後使JPanel可見
- 2. JButton退出JPanel
- 3. JButton沒有出現在JPanel上
- 4. 單擊JButton時退出JFrame
- 5. 如何通過點擊JButton添加JPanel?
- 6. 如何在JPanel邊框中使用JButton?
- 7. 如何在JFrame中單擊JButton時更改現有圖像? (Java)
- 8. 如何在單擊,右鍵單擊或拖動3個JButton中的任何一個時出現JPopupMenu?
- 9. 單擊GUI時JButton消失
- 10. 如何在JPanel中添加JButton?
- 11. 如何在單擊div時出現彈出式菜單
- 12. JPanel和JButton
- 13. JButton不會出現,直到點擊
- 14. 在JPanel中的JButton方向
- 15. Java Swing刪除Jpanel,同時單擊Jbutton中存在的那個jpanel,我想刪除
- 16. 使用GBC/GBL在JPanel上定位JButton
- 17. jPanel和jButton定製
- 18. 如何在Java中單擊jbutton時發起操作
- 19. 如何在點擊JButton時顯示新表單?
- 20. 右鍵單擊JButton
- 21. 如何動態添加JButton到JPanel?
- 22. Java:如何使JPanel中的ImageIcon出現/隨機出現隨機持續時間
- 23. 如何在單擊jPanel(Java)時調用函數?
- 24. 另一個JPanel中的JPanel中的JButton
- 25. NumberFormatException:單擊JButton時爲空字符串?
- 26. 單擊時更改JButton文本
- 27. 單擊JButton時發送電子郵件?
- 28. JPanel在添加到父項時沒有出現JPanel
- 29. 如何讓我的JPanel在調用動作列表時出現
- 30. 單擊JButton時將不同的圖像繪製到JButton
需要更多信息。從哪裏出現?從另一個面板後面的同一個窗口?在對話框中彈出? – 2012-03-26 19:46:58
我想讓它出現在一個可調整大小,可移動和可關閉的新框架中。 – 2012-03-26 19:57:50
您是如何獲得ColorChooser的?與你自己的面板一樣。 – Randy 2012-03-26 20:02:34