开发者

How to make radio buttons change text dynamically in Java

I'm fairly new to GUI. I'm trying to make it so that depending on which radio button is selected, a JLabel changes its value. For example, if "id" is selected, it'll display "http://steamcommunity.com/id/" and if "profile" is selected, it'll display "http://steamcommunity.com/profiles/". I have some code up and running and it's nearly complete:

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);
        profile开发者_Go百科Button.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();
            }
        });
    }
}


Take a look at this SO thread.


in actionPerformed() you need to textpanel.setText() to whatever you want based on which button was clicked. I'm guessing at the method name, haven't done any UI stuff with Java for a while.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜