2017-06-06 2 views
-1

私はコードを作成しようとしていますが、彼がユーザによって押されたときには正しく印刷されますが、正しいものは表示されません。私は非常に明確にこのトピックを研究してきたが、私はすべてのボディは、それが大幅にJRadioButtonをクリックしたときに正しいコードが印刷されない

import java.awt.FlowLayout; 
import javax.swing.ButtonGroup; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JRadioButton; 
import javax.swing.JOptionPane; 

public class JRadioButtonTest 
{ 

    public static void main(String[] args) 
    { 

    JFrame.setDefaultLookAndFeelDecorated(true); 
    JFrame frame = new JFrame("JRadioButton Test"); 
    frame.setLayout(new FlowLayout()); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JRadioButton button1 = new JRadioButton("0.4"); 
    JRadioButton button2 = new JRadioButton("12"); 
    JRadioButton button3 = new JRadioButton("37"); 

    ButtonGroup colorButtonGroup = new ButtonGroup(); 
    colorButtonGroup.add(button1); 
    colorButtonGroup.add(button2); 
    colorButtonGroup.add(button3); 

    frame.add(new JLabel("what is the density of a piece of rock that is 40 grams and 25cm3")); 
    frame.add(button1); 
    frame.add(button2); 
    frame.add(button3); 
    frame.pack(); 
    frame.setVisible(true); 

    if(button2.isSelected()){ 
     JOptionPane.showMessageDialog(null,"correct"); 
     } 
    } 
    } 
+0

ボタンにリスナーを追加する必要があります。現在のコードでは、フレームが表示されるとすぐにボタンをチェックします。 –

答えて

0

あなたがラジオボタンをクリックすると、それはイベントが実行時に起こるのですが理解されるであろう私を助けることができるならば正しい答えを見つけるように見えることはできません。終了メインメソッドで持っているコードは、フレームがロードされる前に一度だけ実行されます。クリックするたびにその機能を起動させるには、リスナーを登録する必要があります。だから、あなたのコードは次のようになります。

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.ButtonGroup; 
    import javax.swing.JFrame; 
    import javax.swing.JLabel; 
    import javax.swing.JRadioButton; 
    import javax.swing.JOptionPane; 

    public class JRadioButtonTest 
    { 

     public static void main(String[] args) 
     { 

     JFrame.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("JRadioButton Test"); 
     frame.setLayout(new FlowLayout()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JRadioButton button1 = new JRadioButton("0.4"); 
     JRadioButton button2 = new JRadioButton("12"); 
     JRadioButton button3 = new JRadioButton("37"); 

     ButtonGroup colorButtonGroup = new ButtonGroup(); 
     colorButtonGroup.add(button1); 
     colorButtonGroup.add(button2); 
     colorButtonGroup.add(button3); 

     frame.add(new JLabel("what is the density of a piece of rock that is 40 grams and 25cm3")); 
     frame.add(button1); 
     frame.add(button2); 
     frame.add(button3); 
     frame.pack(); 
     frame.setVisible(true); 

     button2.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       JOptionPane.showMessageDialog(null,"correct"); 

      } 
     }); 
     } 
    } 

理想的には、それはActionListenerの独自の実装を作成し、必要なすべての要素にそれを登録する方が良いでしょう。 'e'オブジェクトを使用して要素をキャプチャします。

+0

これを何度も試してみましたが、それが私に与えられるたびに – fizzlekay88

+0

ファイル:C:\ Users \ EC \ JRadioButtonTest.java [行:36] エラー:javax型のaddActionListener(java.awt.event.ActionListener) .swing.AbstractButtonは引数には適用されません(新しいActionListener(){}) ファイル:C:\ Users \ EC \ JRadioButtonTest.java [行:36] エラー:ActionListenerをタイプ に解決できません。ファイル:C :\ Users \ EC \ JRadioButtonTest.java [行:39] エラー:ActionEventをタイプ – fizzlekay88

+0

に解決できません。私の推測では、あなたは輸入品を逃しています – YuVi

1

コードでは、作成したラジオボタンの状態を直ちにチェックします。このため、setVisible(true)コールとチェックの間にあるラジオボタンを押す必要がありますが、これは実際には不可能です。

あなたが探しているものはリスナーです。これらは特定のイベントが発生した場合に実行されます。 例としてItemListenerは、ラジオボタンの値が変更されるたびに実行されるため、例に最も適しています。

button2.addItemListener(new ItemListener() 
{ 
@Override 
public void itemStateChanged(ItemEvent e) { 
    if (e.getStateChange() == ItemEvent.SELECTED) { 
    // Your selected code here. 
    } 
    else if (e.getStateChange() == ItemEvent.DESELECTED) { 
    // Your deselected code here. 
    } 
} 
} 

ハッピーコーディング!

0

これを容易にするだけヒント:

あなたはボタンのクリックで、その結果としての機能を有効にしてプロジェクトを参照することができようになるビューを実装することを可能にするいくつかのEclipseネオンパッケージがあります。リアルタイムで。それを通じて探し

ヘルプ>パッケージをインストール

http://downlods.eclipse.org/releases/neon

あなたに役に立つかもしれません!

関連する問題