在'JTextFields'中,有没有办法让用户选择使TextField可编辑。默认情况不可编辑。查看详细信息。

时间:2017-11-02 13:31:23

标签: java radio-button panel frame jtextfield

当用户按下按钮(我创建)时,它会使 TextField 可编辑。 首先,我在构造函数中使用if条件:
if(button.isSelected()) TextField.isEditable(true); else TextField.isEditable(false);
但是,这给了我一个错误。然后,我在参数ActionEvent的方法中使用相同的语句(以授予用户天气以使文本可编辑或不可编辑),其中另一个方法_由ActionListener实现。但这也给出了错误。
给出按钮上的动作之前的代码和输出: 代码如下 Output

package Radio_Button;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Radio_Buttons extends JPanel{
    private JRadioButton bold;
    private JRadioButton italic;
    private JRadioButton both;
    private JRadioButton plain;
    private ButtonGroup group;
    private JTextField TextField;
    private JButton button;

    public Radio_Buttons(){
        //Declare all radio buttons
        plain = new JRadioButton("Plain",false);
        bold = new JRadioButton("Bold", false);
        italic = new JRadioButton("Italic", false);
        both = new JRadioButton("Bold+Italic", false);
        //Declare Text Field
        TextField = new JTextField("The quick brown fox jumps over the lazy dog.",40);
        TextField.setFont(new Font("Chiller", Font.PLAIN, 30));
        //For button
        button = new JButton("Push to edit");





        //Add in panel
        add(bold);
        add(italic);
        add(both);
        add(plain);
        add(TextField);
        add(button);

        //Make a family of radiobuttons so they can understand each others. 
        group = new ButtonGroup();
        group.add(bold);
        group.add(italic);
        group.add(both);
        group.add(plain);


        RadioListener listener = new RadioListener();
        bold.addActionListener(listener);
        italic.addActionListener(listener);
        plain.addActionListener(listener);
        both.addActionListener(listener);

        setBackground(Color.yellow);
        setPreferredSize(new Dimension(800,500));
}
    private class RadioListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            int source = 0;
               if (bold.isSelected()) {source =Font.BOLD; }
               else if (italic.isSelected()) {source = Font.ITALIC; }
               else if (both.isSelected()){source = Font.BOLD+Font.ITALIC; }
               else {source = Font.PLAIN; }
               TextField.setFont(new Font("Chiller", source, 30));

    }

    }
    public static void main (String [] args){
        JFrame frame = new JFrame("Quote Options");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Radio_Buttons panel = new Radio_Buttons();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 首先是第一件事。花一些时间来了解有意义的"变量"姓名和Naming Convention。例如TextField之类的变量名称是否定号。

  2. 在您的示例中,默认情况下会启用TextField。用" editable"初始化它像下面的假:

    TextField.setEditable(false);

  3. 将监听器添加到按钮并更改Textflied的可编辑状态,如下所示
  4. button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    TextField.setEditable(true);
                }
            });