我有一组按钮,按下时某些按钮被禁用。按钮有各自的图标。然而,麻烦的是当我点击btnAdd时,按钮被禁用,btnAdd的图像不会改变。我正在使用Java Netbeans 8.0.2,我连接到MySql。
我问我如何在执行actionperform时使btnAdd处理第二张图像
这是代码
if(btnAdd.getText().equals("Add")){
btnAdd.setText("Save");
btnDelete.setEnabled(true);
btnEdit.setEnabled(false);
btnExit.setEnabled(false);
btnSwitch.setEnabled(false);
btnCancel.setEnabled(true);
}else{
btnAdd.setText("Add");
btnAdd.setEnabled(true);
btnEdit.setEnabled(true);
btnDelete.setEnabled(true);
btnSwitch.setEnabled(true);
btnCancel.setEnabled(false);
btnExit.setEnabled(true);
}
答案 0 :(得分:2)
我问我如何在执行actionperform时使btnAdd处理第二张图像?
在这种情况下,解决方案是根据我的评论 - 在JButton上调用setIcon(newImageIcon)
,您想要更改其图像。如果您的按钮使用了操作,您还可以通过LARGE_ICON_KEY
更改操作的putValue(LARGE_ICON_KEY, newImageIcon)
属性。
例如:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
public class MultipleButtons extends JPanel {
private static final long serialVersionUID = 1L;
private JButton btnSaveAdd = new JButton();
private JButton btnDelete = new JButton("Delete");
private JButton btnEdit = new JButton("Edit");
private JButton btnExit = new JButton("Exit");
private JButton btnSwitch = new JButton("Switch");
private JButton btnCancel = new JButton("Cancel");
private JButton[] btns = {btnSaveAdd, btnDelete, btnEdit, btnExit, btnSwitch, btnCancel};
public MultipleButtons() {
Icon saveIcon = UIManager.getIcon("FileView.floppyDriveIcon");
// wouldn't really use this icon but just used as an example
Icon addIcon = UIManager.getIcon("OptionPane.warningIcon");
SaveAction saveAction = new SaveAction("Save", saveIcon);
AddAction addAction = new AddAction("Add", addIcon);
saveAction.setNextAction(addAction);
addAction.setNextAction(saveAction);
saveAction.buttonMapPut(btnDelete, true);
saveAction.buttonMapPut(btnEdit, true);
saveAction.buttonMapPut(btnExit, true);
saveAction.buttonMapPut(btnSwitch, true);
saveAction.buttonMapPut(btnCancel, false);
addAction.buttonMapPut(btnDelete, true);
addAction.buttonMapPut(btnEdit, false);
addAction.buttonMapPut(btnExit, false);
addAction.buttonMapPut(btnSwitch, false);
addAction.buttonMapPut(btnCancel, true);
btnSaveAdd.setAction(addAction);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new GridLayout(1, 0, 5, 5));
for (JButton jButton : btns) {
add(jButton);
}
}
private static void createAndShowGui() {
MultipleButtons mainPanel = new MultipleButtons();
JFrame frame = new JFrame("MultipleButtons");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
// parent class for both SaveAction and AddAction
abstract class SaveAddAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private Map<AbstractButton, Boolean> buttonMap = new HashMap<>();
private Action nextAction; // swap to this Action
public SaveAddAction(String name, Icon icon) {
super(name, icon);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
public void buttonMapPut(AbstractButton key, Boolean value) {
buttonMap.put(key, value);
}
public void setNextAction(Action nextAction) {
this.nextAction = nextAction;
}
public void setButtonsEnabled(ActionEvent e) {
AbstractButton source = (AbstractButton) e.getSource();
if (nextAction != null) {
source.setAction(nextAction);
}
for (AbstractButton button : buttonMap.keySet()) {
button.setEnabled(buttonMap.get(button));
}
}
}
class AddAction extends SaveAddAction {
private static final long serialVersionUID = 1L;
public AddAction(String name, Icon icon) {
super(name, icon);
}
@Override
public void actionPerformed(ActionEvent e) {
setButtonsEnabled(e);
// TODO: code for adding goes here
}
}
class SaveAction extends SaveAddAction {
private static final long serialVersionUID = 1L;
public SaveAction(String name, Icon icon) {
super(name, icon);
}
@Override
public void actionPerformed(ActionEvent e) {
setButtonsEnabled(e);
// TODO: code for saving goes here
}
}