2016-05-06 8 views
0

Image Icons on JButtonsJButtonがにImageIconsを追加した後、ボタンはもはや

は私がそれを提出する前に持っていた残り時間となるよう、私が試してみたかった、初期のプロジェクトを完成した作品はありません。プログラム全体が素晴らしく、JButtonはプログラムされたとおりに機能します。

ImageIconをJButtonに追加した後に何か問題が発生しました。私がするJButton初期化の数を示し、各ブロックの上の単一のコメントで、元を紹介します:

/** 
* 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"); 
} 

ボタン付きのみ違いはImageIconsは、テキストではなく、今があるということです。

私の問題は次のコードにあります。 ImageIconをButtonで初めて使用したのは初めてのことです。だから私はいつも "button.getText()。equals(" Button String ");"に慣れてきました。

public void actionPerformed(ActionEvent e) { 
     // get the button that was pressed 
     JButton b = (JButton) e.getSource(); 

     // fire appropriate event 
     if (b.getText().equals("Normal Setup")) { 
      // set up for normal simulation 
      fireSimulationEvent(SimulationEvent.NORMAL_SETUP_EVENT); 
     } else if (b.getText().equals("Queen Test")) { 
      // set for testing the queen ant 
      fireSimulationEvent(SimulationEvent.QUEEN_TEST_EVENT); 
     } else if (b.getText().equals("Scout Test")) { 
      // set for testing the scout ant 
      fireSimulationEvent(SimulationEvent.SCOUT_TEST_EVENT); 
     } else if (b.getText().equals("Forager Test")) { 
      // set for testing the forager ant 
      fireSimulationEvent(SimulationEvent.FORAGER_TEST_EVENT); 
     } else if (b.getText().equals("Soldier Test")) { 
      // set for testing the soldier ant 
      fireSimulationEvent(SimulationEvent.SOLDIER_TEST_EVENT); 
     } else if (b.getText().equals("Run")) { 
      // run the simulation continuously 
      fireSimulationEvent(SimulationEvent.RUN_EVENT); 
     } else if (b.getText().equals("Step")) { 
      // run the simulation one turn at a time 
      fireSimulationEvent(SimulationEvent.STEP_EVENT); 
     } else if (b.getText().equals("Stop")) { 
      //stop everything 
      fireSimulationEvent(SimulationEvent.STOP_EVENT); 
     } 
    } 

だから、Swing Gurusは、JButtonのImageIconに基づいてどのようにイベントを発生させますか?何か助けてくれてありがとう。これがうまくいかない場合は、平易なテキストだけで元のバージョンに戻してもよろしくお願いします。

はい、画像がすべて同じ形式ではないことはわかります。彼らは最終的なイメージになることはありません。私はちょうどテストして、私が今見つけることができるどんな形式をつかんでいます。

+0

([例] http://stackoverflow.com/questions/34863894/how-to-use-actionperformed-actionevent-e-with-more-that -one-button/34864401#34864401) – MadProgrammer

答えて

2

JButtonにはテキストが設定されていないため、テキストが設定されていないとイメージに表示されます。 1. setNameメソッドを設定してテキストを追加します。 では、getTextではなくgetNameを使用してください。例については

:アクションで

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"); 

が行わ:Sandeep.K

@

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); 
      } 
...... 
+0

また、 'setActionCommand'を使用してください。これは、 – MadProgrammer

0

を私はより短い通話を好きなので、私はまだ、あなたの答えを受け入れるつもりです"通常のセットアップ"。私はあなたの答えを見る前に遠くに行った。両方とも動作しますが、あなたの方が優れています。

​​
+0

はいです。' SetName'は、短い呼び出しと文字列のチェックに役立ちます。 –

3

あなたのコードを再加工する最善の方法は、ボタンのクライアントプロパティを使用することです。

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" これはより良く見える;)

+0

これは最高です..私は認識していません'putClientProperty'メソッドは、' actionPerformed'の条件を減らすのに役立ちます –

+0

@Sergly私はそれも好きです!ありがとう! – IRGeekSauce

+0

また、 'setActionCommand'は – MadProgrammer

1

あなたはあなたがこの

を可能性があり得るかもしれない複数の方法があります...

単にあなたがオリジナルのボタンへの参照を持っている場合、これは大丈夫かもしれませんActionEvent

Action voting = new AbstractAction(){ 
    @Override 
    public void actionPerformed(ActionEvent e){ 
     if (e.getSource() == vote_up) { 
      //... 
     } else if (...) { 
      //... 
     } 
    } 
}; 

sourceを使用

あなたは可能性が...

各ボタンに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 (...) { 
      //... 
     } 
    } 
}; 

あなたは...

その後

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()); 
//... 

を使用することができます...自己は、各ボタンのアクションが含まれて、Action APIを最大限に活用し、indiviualを作ることができましたActionのプロパティに従ってボタンを設定し、ボタンがトリガーされたときにactionPerformedメソッドをトリガーします。このようにして、actionPerformedメソッドが呼び出されたときに何をすべきか/実行する必要があるかを疑問なく100%知っています。

は、より多くの詳細については、How to Use Actionsをよく見てください

+0

Hmm。各ボタンに対して異なるアクションを使用する場合は、抽象メソッド 'getEvent()'を定義し、それぞれの具体的なアクションがこのメソッドを実装して適切なイベントを返す方がよいでしょう。そのため、 'actionPerformed()'メソッドには 'fireSimulationEvent(getEvent())'という行が1つしかありません。 「if-else if」カスケードはここでは本当に悪く見えます。 –

+0

あなたが 'Action' APIについて話しているのなら、' actionPerformed'メソッドを使って 'Action'の作業を行い、それを自己完結した作業単位にすることです。これは答えですあなたが実装するすべての理論と手段は、文脈に帰着します。もちろん、「イベントタイプ」を「SimulatorAction」のインスタンスに渡すだけで、10行のコードを作成できます;) – MadProgrammer

関連する問題