如何使单选按钮在Java中动态更改文本

时间:2010-12-09 18:58:52

标签: java user-interface swing

我对GUI很新。我试图使它取决于选择哪个单选按钮,JLabel更改其值。例如,如果选择“id”,它将显示“http://steamcommunity.com/id/”,如果选择“profile”,它将显示“http://steamcommunity.com/profiles/” 。我有一些代码并且正在运行,它几乎完成了:

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();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:2)

看一下this SO帖子。

答案 1 :(得分:0)

在actionPerformed()中你需要textpanel.setText()根据你点击的按钮你想要的任何东西。我猜测方法名称,暂时没有使用Java做任何UI的东西。