2010-12-09 6 views
0

Je suis assez nouveau pour GUI. J'essaie de faire en sorte qu'en fonction du bouton radio sélectionné, un JLabel change de valeur. Par exemple, si "id" est sélectionné, il affichera "http://steamcommunity.com/id/" et si "profile" est sélectionné, il affichera "http://steamcommunity.com/profiles/" . J'ai un code opérationnel et il est presque complet:Comment faire pour que les boutons radio changent le texte dynamiquement en Java

package sgt; 

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 

import javax.swing.BorderFactory; 
import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JTextField; 


public class RadioButtonPrompt extends JPanel 
          implements ActionListener { 

private static final long serialVersionUID = 1L; 
static String idString = "ID"; 
    static String profileString ="Profile"; 
    static String type = idString; 

    public RadioButtonPrompt() { 
     super(new BorderLayout()); 

     // Create radio buttons. 
     JRadioButton idButton = new JRadioButton(idString, true); 
     idButton.setMnemonic(KeyEvent.VK_I); 
     idButton.setActionCommand(idString); 

     JRadioButton profileButton = new JRadioButton(profileString); 
     profileButton.setMnemonic(KeyEvent.VK_P); 
     profileButton.setActionCommand(profileString); 

     // Group radio buttons. 
     ButtonGroup group = new ButtonGroup(); 
     group.add(idButton); 
     group.add(profileButton); 

     idButton.addActionListener(this); 
     profileButton.addActionListener(this); 

     JPanel radioPanel = new JPanel(new GridLayout(0, 1)); 
     radioPanel.add(idButton); 
     radioPanel.add(profileButton); 

     JPanel textPanel = new JPanel(); 
     JLabel URL = new JLabel(setJLabelValue()); 

     JTextField text = new JTextField("sampletextfield"); 
     text.setPreferredSize(new Dimension(100, 20)); 

     textPanel.add(URL); 
     textPanel.add(text); 

     JPanel buttonPanel = new JPanel(new GridLayout(1, 0)); 
     JButton submit = new JButton("Submit"); 
     submit.setMnemonic(KeyEvent.VK_S); 

     buttonPanel.add(submit); 


     add(radioPanel, BorderLayout.LINE_START); 
    add(textPanel, BorderLayout.CENTER); 
    add(buttonPanel, BorderLayout.PAGE_END); 

     setBorder(BorderFactory.createCompoundBorder()); 
    } 

    private String setJLabelValue() { 
    if (type.equals("ID")) { 
     return "http://steamcommunity.com/id/"; 
    } 
    return "http://steamcommunity.com/profiles/"; 

} 

    public void actionPerformed(ActionEvent e) { 

    // Returns either "Profile" or "ID" 
     type = ((JRadioButton)e.getSource()).getText(); 
     System.out.println(type); 


    } 

    private static void createAndShowGUI() { 
     JFrame frame = new JFrame("Steam Game Tracker"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JComponent newContentPane = new RadioButtonPrompt(); 
     newContentPane.setOpaque(true); //content panes must be opaque 

     frame.setContentPane(newContentPane); 

     // Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 
    } 
} 
+1

Quel est exactement le problème? – jjnguy

+0

Comment puis-je modifier la valeur du JLabel chaque fois qu'un bouton radio est sélectionné? – Alex

Répondre

3

Jetez un oeil à this fil SO. Dans actionPerformed(), vous devez textpanel.setText() pour ce que vous voulez en fonction du bouton sur lequel vous avez cliqué.

+0

Wow, j'aurais juré que j'ai vérifié pour voir si JLabel avait une méthode setText, merci pour l'aide. – Alex

+0

Si ce post résout votre problème, alors marquez-le donc;) Vous devriez être en mesure de voir un «très bon» près de chaque réponse. – npinti

0

Je devine le nom de la méthode, je n'ai pas fait d'interface utilisateur avec Java depuis un moment.

Questions connexes