我在按鈕中的JButton和JTextArea上有一個動作偵聽器。然而,當我點擊按鈕的文本區域吞噬事件,並沒有發生任何按鈕。JTextArea吞嚥JButton動作偵聽器Java
如何點擊該區域?
謝謝
按鈕代碼
public class CustomFoodItemButton extends JButton{
public JTextArea buttonTitle;
/**
* Public constructor
* @param title
*/
public CustomFoodItemButton(String title) {
//Set button text by using a text area
buttonTitle = new JTextArea();
buttonTitle.setText(title);
buttonTitle.setFont(new Font("Helvetica Neue", Font.PLAIN, 15));
buttonTitle.setEditable(false);
buttonTitle.setWrapStyleWord(true);
buttonTitle.setLineWrap(true);
buttonTitle.setForeground(Color.white);
buttonTitle.setOpaque(false);
//Add the text to the center of the button
this.setLayout(new BorderLayout());
this.add(buttonTitle,BorderLayout.CENTER);
//Set the name to be the title (to track actions easier)
this.setName(title);
//Clear button so as to show image only
this.setOpaque(false);
this.setContentAreaFilled(false);
this.setBorderPainted(false);
//Set not selected
this.setSelected(false);
//Set image
setImage();
}
GUI類代碼
private void addFoodItemButtons (JPanel panel){
//Iterate over menu items
for (FoodItem item : menuItems) {
//Create a new button based on the item in the array. Set the title to the food name
CustomFoodItemButton button = new CustomFoodItemButton(item.foodName);
//Add action listener
button.addActionListener(this);
}
}
添加了代碼。 –
爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –