Java 将图像图标添加到JButtons后,按钮不再工作
我提前完成了一个项目,所以在提交之前剩下的时间里,我想进行实验。整个程序运行得很好,JButtons完全按照编程的方式运行 将ImageIcon添加到我的JButtons后,出现了一些错误。我将展示一些JButton初始化,并在每个块上方的单个注释中显示原始版本:Java 将图像图标添加到JButtons后,按钮不再工作,java,swing,jbutton,imageicon,Java,Swing,Jbutton,Imageicon,我提前完成了一个项目,所以在提交之前剩下的时间里,我想进行实验。整个程序运行得很好,JButtons完全按照编程的方式运行 将ImageIcon添加到我的JButtons后,出现了一些错误。我将展示一些JButton初始化,并在每个块上方的单个注释中显示原始版本: /** * create child components */ private void initComponents() { //normalSetupButton = new JButton("Normal Set
/**
* create child components
*/
private void initComponents() {
//normalSetupButton = new JButton("Normal Setup");
ImageIcon normalButtonImage = new ImageIcon("src/Images/normalIcon.png");
normalSetupButton = new JButton();
normalSetupButton.setIcon(normalButtonImage);
normalSetupButton.addActionListener(buttonHandler);
normalSetupButton.setToolTipText("Set up simulation for normal execution");
// queen test button
//queenTestButton = new JButton("Queen Test");
ImageIcon queenButtonImage = new ImageIcon("src/Images/yellowIcon.jpg");
queenTestButton = new JButton();
queenTestButton.setIcon(queenButtonImage);
queenTestButton.addActionListener(buttonHandler);
queenTestButton.setToolTipText("Set up to test Queen Lifespan or Food Levels");
// scout test button
//scoutTestButton = new JButton("Scout Test");
ImageIcon scoutButtonImage = new ImageIcon("src/Images/blueIcon.png");
scoutTestButton = new JButton();
scoutTestButton.setIcon(scoutButtonImage);
scoutTestButton.addActionListener(buttonHandler);
scoutTestButton.setToolTipText("Set up simulation for testing the Scout ant");
}
按钮的唯一区别是现在有图像图标而不是文本
我的问题在于下面的代码。这是我第一次在按钮上使用图像图标,所以我一直习惯于“button.getText().equals(“按钮字符串”);”
那么,Swing大师,如何基于JButton的ImageIcon触发事件呢?谢谢你的帮助。如果这不起作用,我很乐意用纯文本将其更改回旧版本
注意是的,我知道图像的格式不尽相同。它们不会是最终的图像。我只是在测试和抓取我现在能找到的任何格式。您没有将任何文本设置为JButton,因此它不起作用,如果您设置文本,那么它将显示在图像上,因此您可以做一件事 1.设置
setName
方法并向其添加文本。
2.在actionPerformed
中,使用getName
而不是getText
例如:
ImageIcon normalButtonImage = new ImageIcon("src/Images/normalIcon.png");
normalSetupButton = new JButton();
normalSetupButton.setIcon(normalButtonImage);
normalSetupButton.setName("Normal Setup");
normalSetupButton.addActionListener(buttonHandler);
normalSetupButton.setToolTipText("Set up simulation for normal execution");
在实际执行中:
public void actionPerformed(ActionEvent e) {
// get the button that was pressed
JButton b = (JButton) e.getSource();
// fire appropriate event
if (b.getName().equals("Normal Setup")) {
// set up for normal simulation
fireSimulationEvent(SimulationEvent.NORMAL_SETUP_EVENT);
}
......
您没有将任何文本设置为JButton,因此它不起作用,如果您设置文本,则它将显示在图像上,因此您可以做一件事 1.设置
setName
方法并向其添加文本。
2.在actionPerformed
中,使用getName
而不是getText
例如:
ImageIcon normalButtonImage = new ImageIcon("src/Images/normalIcon.png");
normalSetupButton = new JButton();
normalSetupButton.setIcon(normalButtonImage);
normalSetupButton.setName("Normal Setup");
normalSetupButton.addActionListener(buttonHandler);
normalSetupButton.setToolTipText("Set up simulation for normal execution");
在实际执行中:
public void actionPerformed(ActionEvent e) {
// get the button that was pressed
JButton b = (JButton) e.getSource();
// fire appropriate event
if (b.getName().equals("Normal Setup")) {
// set up for normal simulation
fireSimulationEvent(SimulationEvent.NORMAL_SETUP_EVENT);
}
......
@Sandeep.K
我还是会接受你的回答,因为我喜欢短一点的“正常设置”。我走了很长一段路才看到你的答案。两种方法都有效,但你的更好
public void actionPerformed(ActionEvent e) {
// get the button that was pressed
JButton b = (JButton) e.getSource();
// fire appropriate event
if(b.getToolTipText().equals("Set up simulation for normal execution")) {
fireSimulationEvent(SimulationEvent.NORMAL_SETUP_EVENT);
}
else if(b.getToolTipText().equals("Set up to test Queen Lifespan or Food Levels")) {
fireSimulationEvent(SimulationEvent.QUEEN_TEST_EVENT);
}
else if (b.getToolTipText().equals("Set up simulation for testing the Forager ant (Scouts are included)")) {
// set for testing the forager ant
fireSimulationEvent(SimulationEvent.FORAGER_TEST_EVENT);
} else if (b.getToolTipText().equals("Set up simulation for testing the Soldier ant (Scouts are included")) {
// set for testing the soldier ant
fireSimulationEvent(SimulationEvent.SOLDIER_TEST_EVENT);
} else if (b.getToolTipText().equals("Run the simulation continuously")) {
// run the simulation continuously
fireSimulationEvent(SimulationEvent.RUN_EVENT);
} else if (b.getToolTipText().equals("Step through the simulation one turn at a time")) {
// run the simulation one turn at a time
fireSimulationEvent(SimulationEvent.STEP_EVENT);
} else if (b.getToolTipText().equals("Stop or Pause the simulation")) {
//stop everything
fireSimulationEvent(SimulationEvent.STOP_EVENT);
}
@Sandeep.K
我还是会接受你的回答,因为我喜欢短一点的“正常设置”。我走了很长一段路才看到你的答案。两种方法都有效,但你的更好
public void actionPerformed(ActionEvent e) {
// get the button that was pressed
JButton b = (JButton) e.getSource();
// fire appropriate event
if(b.getToolTipText().equals("Set up simulation for normal execution")) {
fireSimulationEvent(SimulationEvent.NORMAL_SETUP_EVENT);
}
else if(b.getToolTipText().equals("Set up to test Queen Lifespan or Food Levels")) {
fireSimulationEvent(SimulationEvent.QUEEN_TEST_EVENT);
}
else if (b.getToolTipText().equals("Set up simulation for testing the Forager ant (Scouts are included)")) {
// set for testing the forager ant
fireSimulationEvent(SimulationEvent.FORAGER_TEST_EVENT);
} else if (b.getToolTipText().equals("Set up simulation for testing the Soldier ant (Scouts are included")) {
// set for testing the soldier ant
fireSimulationEvent(SimulationEvent.SOLDIER_TEST_EVENT);
} else if (b.getToolTipText().equals("Run the simulation continuously")) {
// run the simulation continuously
fireSimulationEvent(SimulationEvent.RUN_EVENT);
} else if (b.getToolTipText().equals("Step through the simulation one turn at a time")) {
// run the simulation one turn at a time
fireSimulationEvent(SimulationEvent.STEP_EVENT);
} else if (b.getToolTipText().equals("Stop or Pause the simulation")) {
//stop everything
fireSimulationEvent(SimulationEvent.STOP_EVENT);
}
我认为重新编写代码的最佳方法是使用按钮的客户端属性
private static final String EVENT_TYPE = "event_type";
// button creation
normalSetupButton = new JButton();
// set appropriate event for this button
normalSetupButton.putClientProperty(EVENT_TYPE, SimulationEvent.NORMAL_SETUP_EVENT);
// other init button routine
//next button
queenTestButton = new JButton();
queenTestButton.putClientProperty(EVENT_TYPE, SimulationEvent.QUEEN_TEST_EVENT);
// other init button routine
// same way for other buttons
public void actionPerformed(ActionEvent e) {
// get the button that was pressed
JButton b = (JButton) e.getSource();
SimulationEvent evt = (SimulationEvent) b.getClientProperty(EVENT_TYPE);
fireSimulationEvent(evt);
}
这看起来比“if-else-if”级联更好;) 我认为重新编写代码的最佳方法是使用按钮的客户端属性
private static final String EVENT_TYPE = "event_type";
// button creation
normalSetupButton = new JButton();
// set appropriate event for this button
normalSetupButton.putClientProperty(EVENT_TYPE, SimulationEvent.NORMAL_SETUP_EVENT);
// other init button routine
//next button
queenTestButton = new JButton();
queenTestButton.putClientProperty(EVENT_TYPE, SimulationEvent.QUEEN_TEST_EVENT);
// other init button routine
// same way for other buttons
public void actionPerformed(ActionEvent e) {
// get the button that was pressed
JButton b = (JButton) e.getSource();
SimulationEvent evt = (SimulationEvent) b.getClientProperty(EVENT_TYPE);
fireSimulationEvent(evt);
}
这看起来比“if-else-if”级联更好;) 实现这一目标有多种方法 你可以。。。 只需使用
ActionEvent
Action voting = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == vote_up) {
//...
} else if (...) {
//...
}
}
};
如果您有原始按钮的参考,这可能没问题
你可以。。。
为每个按钮分配一个actionCommand
JButton vote_up = new JButton(upvote);
vote_up.setActionCommand("vote.up");
JButton vote_down = new JButton(downvote);
vote_down .setActionCommand("vote.down");
//...
Action voting = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
if ("vote.up".equals(e.getActionCommand())) {
//...
} else if (...) {
//...
}
}
};
你可以。。。
充分利用Action
API,为每个按钮执行独立的操作
public class VoteUpAction extends AbstractAction {
public VoteUpAction() {
putValue(SMALL_ICON, new ImageIcon(getClass().getResource("vote_up.png")));
}
@Override
public void actionPerformed(ActionEvent e) {
// Specific action for up vote
}
}
然后你可以简单地使用
JButton vote_up = new JButton(new VoteUpAction());
//...
它将根据操作的属性配置按钮
,并在触发按钮时触发其操作执行
方法。这样,当调用actionPerformed
方法时,您100%知道应该/需要做什么,毫无疑问
请仔细查看更多详细信息您可以通过多种方式实现此目的 你可以。。。 只需使用
ActionEvent
Action voting = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
if (e.getSource() == vote_up) {
//...
} else if (...) {
//...
}
}
};
如果您有原始按钮的参考,这可能没问题
你可以。。。
为每个按钮分配一个actionCommand
JButton vote_up = new JButton(upvote);
vote_up.setActionCommand("vote.up");
JButton vote_down = new JButton(downvote);
vote_down .setActionCommand("vote.down");
//...
Action voting = new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
if ("vote.up".equals(e.getActionCommand())) {
//...
} else if (...) {
//...
}
}
};
你可以。。。
充分利用Action
API,为每个按钮执行独立的操作
public class VoteUpAction extends AbstractAction {
public VoteUpAction() {
putValue(SMALL_ICON, new ImageIcon(getClass().getResource("vote_up.png")));
}
@Override
public void actionPerformed(ActionEvent e) {
// Specific action for up vote
}
}
然后你可以简单地使用
JButton vote_up = new JButton(new VoteUpAction());
//...
它将根据操作的属性配置按钮
,并在触发按钮时触发其操作执行
方法。这样,当调用actionPerformed
方法时,您100%知道应该/需要做什么,毫无疑问
请仔细查看以了解更多详细信息这一款是最好的。。我不知道
putClientProperty
方法,它将有助于减少actionPerformed
@Sergly中的条件,我也喜欢!谢谢@是的,这也是个好主意,但有点过分,因为它需要动作命令和事件之间的映射。因此,我认为我的方法在这里稍微好一点(即使它不是一种标准方法)。您的解决方案仍然需要某种映射,事实上,使用Action API将是一个总体上更好的解决方案,并隔离责任这是最好的。。我不知道putClientProperty
方法,它将有助于减少actionPerformed
@Sergly中的条件,我也喜欢!谢谢@是的,这也是个好主意,但有点过分,因为它需要动作命令和事件之间的映射。因此,我认为我的方法在这里稍微好一点(即使它不是一种标准方法)。事实上,您的解决方案仍然需要某种映射,使用Action API将是一个整体上更好的解决方案,可以隔离责任,也可以使用它的设计目的setActionCommand
,或者使用它的设计目的setActionCommand
。当您对每个按钮使用不同的操作时,最好定义一个抽象方法getEvent()
,每个具体操作都实现此方法以返回相应的事件。因此方法actionPerformed()
只有一行:fireSimulationEvent(getEvent())
。“if-else-if”级联在这里看起来非常糟糕。如果您谈论的是操作
API,那么重点是使用actionPerformed
方法来完成操作
的工作,当然,使其成为一个自包含的工作单元,这就是答案,所有的理论和实现的方法都要归结到上下文。库尔