2017-06-06 21 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

當你點擊單選按鈕,它是一個事件發生在運行時。您在結束main方法中的代碼僅在加載幀之前執行一次。爲了在每次點擊時觸發該功能,您需要註冊一個監聽器。所以,你的代碼應該是這樣的:

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 [line:36] 錯誤:類型爲javax的方法addActionListener(java.awt.event.ActionListener) .swing.AbstractButton不適用於參數(new ActionListener(){}) File:C:\ Users \ EC \ JRadioButtonTest.java [line:36] 錯誤:ActionListener無法解析爲類型 File:C :\ Users \ EC \ JRadioButtonTest.java [line: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

通過尋找可能對你有用!

相關問題