2016-05-02 9 views
2

Ich versuche, einen Bildschirm zu erstellen, um die Optionen meines Spiels zu ändern, so dass ich ein Problem hätte, dass die Verwendung eines JRadioButton in einem Fall und nicht in der anderen funktioniert, und ich nicht verstanden habe warum ... Hat jemand Wissen was passiert ist?Warum funktioniert mein JRadioButton in diesem Fall nicht?

package com.damas.screen; 

import java.awt.EventQueue; 
import java.awt.Font; 
import javax.swing.JFrame; 
import java.awt.event.ItemEvent; 
import java.awt.event.ItemListener; 
import javax.swing.JLabel; 
import javax.swing.JRadioButton; 


public class Options { 

    private JFrame frame; 
    protected com.damas.entidades.Options opcoes; 
    private JRadioButton radioPlayersNo; 
    private JRadioButton radioPlayersYes; 
    private JRadioButton radioSoundYes; 
    private JRadioButton radioSoundNo; 
    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 


      public void run() { 
       try { 
        Options window = new Options(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public Options() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     opcoes = new com.damas.entidades.Options(); 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 280, 180); 
     frame.setResizable(Boolean.FALSE); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     radioPlayersYes =new JRadioButton("Yes"); 
     radioPlayersNo = new JRadioButton("No"); 
     radioSoundYes = new JRadioButton("Yes"); 
     radioSoundNo = new JRadioButton("No"); 
     JLabel labelSound = new JLabel("Sound"); 
     labelSound.setBounds(50, 99, 86, 23); 

     radioPlayersYes.setBounds(129, 39, 55, 14); 
     radioPlayersNo.setBounds(190, 30, 55, 32); 

     /* Setting default options*/ 
     radioSoundYes.setSelected(opcoes.getSound()); 
     radioPlayersYes.setSelected(opcoes.getTwoPlayers()); 


     /* Setting labels*/ 
     JLabel lblPlayers = new JLabel("Two Players"); 
     lblPlayers.setBounds(50, 43, 73, 14); 


     JLabel lblColor = new JLabel("Color"); 
     lblColor.setBounds(50, 74, 46, 14); 


     JLabel lblOptions = new JLabel("Options"); 

     lblOptions.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 16)); 
     lblOptions.setBounds(109, 11, 46, 14); 

     /* Setting radios*/ 

     JRadioButton radioColorBlack = new JRadioButton("Black"); 
     radioColorBlack.setBounds(115, 65, 77, 32); 


     JRadioButton radioColorWhite = new JRadioButton("White"); 
     radioColorWhite.setBounds(194, 70, 61, 23); 



     JRadioButton radioSoundYes = new JRadioButton("Yes"); 
     radioSoundYes.setBounds(120, 94, 55, 32); 


     JRadioButton radioSoundNo = new JRadioButton("No"); 
     radioSoundNo.setBounds(194, 99, 61, 23); 

     addItemListeners(); 
     frame.getContentPane().add(radioPlayersYes); 
     frame.getContentPane().add(radioPlayersNo); 
     frame.getContentPane().add(radioSoundYes); 
     frame.getContentPane().add(radioSoundNo); 
     frame.getContentPane().add(labelSound); 
     frame.getContentPane().add(lblPlayers); 
     frame.getContentPane().add(lblColor); 
     frame.getContentPane().add(radioColorBlack); 
     frame.getContentPane().add(radioColorWhite); 

     frame.getContentPane().add(lblOptions); 

    } 

    private void addItemListeners(){ 

    radioPlayersYes.addItemListener(new ItemListener() { 


      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if(e.getStateChange() == 1){ 
        opcoes.setTwoPlayers(Boolean.TRUE); 
        radioPlayersNo.setSelected(Boolean.FALSE); 
       }else{ 
        opcoes.setTwoPlayers(Boolean.FALSE); 
        radioPlayersNo.setSelected(Boolean.TRUE); 
       } 
      } 
     }); 



     radioPlayersNo.addItemListener(new ItemListener(){ 

      @Override 
      public void itemStateChanged(ItemEvent e) { 
       if(e.getStateChange() == 1){ 
        opcoes.setTwoPlayers(Boolean.FALSE); 
        System.out.println(opcoes.getTwoPlayers()); 
        radioPlayersNo.setSelected(Boolean.TRUE); 
        radioPlayersYes.setSelected(Boolean.FALSE); 
       }else{ 
        opcoes.setTwoPlayers(Boolean.TRUE); 
        System.out.println(opcoes.getTwoPlayers()); 
        radioPlayersNo.setSelected(Boolean.FALSE); 
        radioPlayersYes.setSelected(Boolean.TRUE); 
       } 

      } 

     }); 


     radioSoundYes.addItemListener(new ItemListener(){ 

      @Override 
      public void itemStateChanged(ItemEvent e) { 

       if(e.getStateChange() == ItemEvent.SELECTED){ 
        radioSoundYes.setEnabled(Boolean.TRUE); 
        System.out.println("Radio Sound Yes: Enabled"); 
        radioSoundNo.setEnabled(Boolean.FALSE); 
       }else{ 
        radioSoundYes.setEnabled(Boolean.FALSE); 
        System.out.println("Radio Sound Yes: Disabled"); 
        radioSoundNo.setEnabled(Boolean.TRUE); 
       } 

      } 

     }); 


     radioSoundNo.addItemListener(new ItemListener(){ 

      @Override 
      public void itemStateChanged(ItemEvent e) { 

       if(e.getStateChange() == ItemEvent.SELECTED){ 
        radioSoundNo.setEnabled(Boolean.TRUE); 
        radioSoundYes.setEnabled(Boolean.FALSE); 

       }else{ 
        radioSoundNo.setEnabled(Boolean.FALSE); 
        radioSoundYes.setEnabled(Boolean.TRUE); 
       } 

      } 

     }); 

    } 


} 
+0

Ich denke, Sie über [ButtonBroup] lesen (https://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html) und schaut in [das] (http://stackoverflow.com/questions/17770944/how-to-add-a-radio-button-group-in-a-core-java-program-such-that-only-) Ein-Radio) – Madhan

Antwort

2

Gruppe die Optionsfelder wie

ButtonGroup playOptionsButtonGroup = new ButtonGroup(); 
playOptionsButtonGroup.add(radioPlayersNo); 
playOptionsButtonGroup.add(radioPlayersYes); 
playOptionsButtonGroup.add(radioSoundYes); 
playOptionsButtonGroup.add(radioSoundNo); 
+0

Danke. @mKorbel – PeaceIsPearl

Verwandte Themen