简体   繁体   English

单击按钮时切换JTextField

[英]Switching JTextField when clicking a button

I have got a JTextArea and a JTextField and now i want that if i click on JTextArea button the control shifts to JTextArea and when i click the JTextField button control shifts to JTextField. 我有一个JTextArea和一个JTextField,现在我想如果我单击JTextArea按钮,则控件移至JTextArea,当我单击JTextField按钮时,控件移至JTextField。 I am not getting anything to do such stuff. 我什么都做不了。 The program just changes the text in the JTextField based on the JRadioButton clicked. 该程序仅基于单击的JRadioButton更改JTextField中的文本。

   ***This is the code:***



import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;


public class prg_32 extends JPanel implements ItemListener{

    JTextArea a1;       
    JButton t1,t2;
    JRadioButton b1,b2,b3,b4;
    JTextField fld1;
    ButtonGroup grp;
    GridBagConstraints gbc = new GridBagConstraints();      //GridBagConstraints object

    public prg_32(){
        setLayout(new GridBagLayout());

        a1 = new JTextArea(3,10);       //TextArea
        gbc.gridx = 0;              
        gbc.gridy = 0;
        gbc.gridheight = 3;
        gbc.fill = GridBagConstraints.VERTICAL;
        add(a1,gbc);        //location of JTextArea

        t1 = new JButton("TextArea");
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        add(t1,gbc);        //location of JButton

        t2 = new JButton("TextField");
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        add(t2,gbc);        //location of JButton

        b1 = new JRadioButton("Bold",false);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        add(b1,gbc);

        b2 = new JRadioButton("Italic",false);
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        add(b2,gbc);

        b3 = new JRadioButton("Plain",false);
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.gridheight = 1;
        add(b3,gbc);

        b4 = new JRadioButton("Bold/Italic",true);
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.gridheight = 1;
        add(b4,gbc);

        grp = new ButtonGroup();
        grp.add(b1);
        grp.add(b2);
        grp.add(b3);
        grp.add(b4);

        fld1 = new JTextField("enter your name");
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(fld1,gbc);

        fld1.setFont(new Font("Serif",Font.BOLD + Font.ITALIC,14));

        b1.addItemListener(this);       //Event Handling
        b2.addItemListener(this);       //Event Handling
        b3.addItemListener(this);       //Event Handling
        b4.addItemListener(this);       //Event Handling
    }

    public void itemStateChanged(ItemEvent e) {
        Font font = null;

        if(b1.isSelected())
            font = new Font("Serif",Font.BOLD,14);
        else if(b2.isSelected())
            font = new Font("Serif",Font.ITALIC,14);
        else if(b3.isSelected())
            font = new Font("Serif",Font.PLAIN,14);
        else
            font = new Font("Serif",Font.BOLD + Font.ITALIC,14);

        fld1.setFont(font);

    }

    public static void main(String[] args){
        prg_32 px = new prg_32();
        JFrame jf = new JFrame();

        jf.setSize(500, 300);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(px);
    }

}

I would suggest implementing an ActionListener and using the method Component.requestFocus() in it to set the focus on the text fields. 我建议实现一个ActionListener并在其中使用方法Component.requestFocus()将焦点设置在文本字段上。

    t1 = new JButton("TextArea");
    t1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            a1.requestFocus();

        }
    });
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.gridheight = 1;
    add(t1,gbc);        //location of JButton

    t2 = new JButton("TextField");
    t2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            fld1.requestFocus();

        }
    });
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.gridheight = 1;
    add(t2,gbc);    

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

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