简体   繁体   English

使用JtextField创建自定义JRadioButton吗?

[英]Creating custom JRadioButton with JtextField?

I want to make JTextField part of JRadioButton or hook them together. 我想使JTextField成为JRadioButton一部分或将它们钩在一起。 my intention is to let the user fill in the text field when he selects the radio button. 我的意图是让用户在选择单选按钮时填写文本字段。 For this reason I have created custom radio button which extends from JradioButton . 因此,我创建了自JradioButton扩展的自定义单选按钮。 I should be able to add it to buttonGroup 我应该能够将其添加到buttonGroup

ButtonGroup buttonGroup = new ButtonGroup()
RadioButtonWithTextField radio1 = RadioButtonWithTextField("only to");
RadioButtonWithTextField radio2 = RadioButtonWithTextField("not only to");
buttonGroup.add(radio1 );
buttonGroup.add(radio1 );
radio1.setSelected(true);

在此处输入图片说明

The problem is, my TextField does not adjust it's size according to what I type in it or setText to it programmatically. 问题是,我的TextField不会根据我在程序中键入的内容或setText对其的大小进行调整。

If I change the text of radio button after displaying the component it does not show all part of it. 如果在显示组件后更改单选按钮的文本,它不会显示其所有部分。

Does anyone know or have experience in custom component creation? 是否有人知道或有自定义组件创建经验?

public class RadioButtonWithTextField extends JRadioButton {
    private JTextField textField;

    public RadioButtonWithTextField(String text) {
        super(text);
        createComponents();
        layoutComponents();
    }

    private void createComponents() {
       textField = new WebTextField("",30);
       textField.setPreferredSize(new Dimension(60,20));
    }

    private void layoutComponents() {
        setLayout(new FlowLayout(FlowLayout.TRAILING,2,2));
        add(textField);
    }

    public WebTextField getTextField() {
        return textField;
    }

    public void setTextField(WebTextField textField) {
        this.textField = textField;
    }

}

I would follow @MadProgrammer's suggestion to create a JPanel class that consists of a JRadioButton and a JTextField. 我将遵循@MadProgrammer的建议来创建一个由JRadioButton和JTextField组成的JPanel类。 The below works for me. 下面为我​​工作。

public class RadioButtonPanel extends JPanel {
JRadioButton jRadioButton;
JTextField jTextField;

RadioButtonPanel(String radioButtonName) {
    jRadioButton = new JRadioButton(radioButtonName);
    jTextField = new JTextField(10);
    this.setLayout(new FlowLayout());
    this.add(jRadioButton);
    this.add(jTextField);

    jRadioButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            handleEvent();
        }
    });
}

private void handleEvent() {
    System.out.println(jRadioButton.getText() + " is selected, the customized text is " + jTextField.getText());
}

public static void main(String[] args) {
    JFrame jFrame = new JFrame();

    RadioButtonPanel radioButtonPanel1 = new RadioButtonPanel("Apple");
    RadioButtonPanel radioButtonPanel2 = new RadioButtonPanel("Banana");
    RadioButtonPanel radioButtonPanel3 = new RadioButtonPanel("Pear");
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(radioButtonPanel1.jRadioButton);
    buttonGroup.add(radioButtonPanel2.jRadioButton);
    buttonGroup.add(radioButtonPanel3.jRadioButton);

    jFrame.setLayout(new GridLayout(3, 1, 5, 5));
    jFrame.add(radioButtonPanel1);
    jFrame.add(radioButtonPanel2);
    jFrame.add(radioButtonPanel3);
    jFrame.pack();
    jFrame.setVisible(true);
}
}

A sample UI would be: 一个示例UI是:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM