Java中的Onclick事件

时间:2018-03-29 15:35:36

标签: java swing awt

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

public class Prg_38 extends JFrame{

    JTextArea area = new JTextArea(5,10);                   // 5 rows and 10 colums
    JButton btn1 = new JButton("TextArea");             // name of JBUtton is TextArea
    JButton btn2 = new JButton("TextField");            // name of JButton is TextField
    JRadioButton bold,italic,plain,bold_italic;         // radio buttons
    JTextField fld = new JTextField(15);                // jtextfield is 15 characters long

    Font bf = new Font("Serif",Font.BOLD, 14);
    Font itf = new Font("Serif",Font.ITALIC, 14);
    Font itbf = new Font("Serif",Font.BOLD + Font.ITALIC, 14);

    GridBagConstraints gc = new GridBagConstraints();       // used to place components on GUI

    public Prg_38() {

        super("Interface");                                 // title of Dialog box
        setLayout(new GridBagLayout());         

        gc.insets = new Insets(0,0,2,2);                        // margin between components of GUI (top,left,right,bottom)

        // textarea
        area.setText("textarea");          // default text in textarea
        addComponent(area,0,0);            // method to place components on GUI

        // jbutton

        addComponent(btn1,1,0);            // textare
        addComponent(btn2,2,0);            // textfield

将组件添加到界面后,我将动作侦听器添加到每个组件,以便在单击文本区域按钮时,焦点更改为文本区域,单击文本字段按钮时,焦点将更改为文本字段。

        //radio buttons

        bold = new JRadioButton("Bold",false);              // name of radio button is Bold
        addComponent(bold,1,1);

        italic = new JRadioButton("Italic",false);          // name of radio button is Italic
        addComponent(italic,2,1);

        plain = new JRadioButton("Plain",false);                // name of radio button is Plain
        addComponent(plain,1,2);

        bold_italic = new JRadioButton("Bold and Italic",true);     // // name of radio button is Bold and Italic
        addComponent(bold_italic, 2, 2);

        ButtonGroup group = new ButtonGroup();              // it makes sure that only 1 radiobutton is enabled at a time
        group.add(bold);
        group.add(bold_italic);
        group.add(italic);
        group.add(plain);

        // when radio buttons are added to a group, only one radio button can be selected at a time.

        // jtextfield

        fld.setText("enter your name");                 // default text
        addComponent(fld,0,3);

        // action listeners

        thehandler handler = new thehandler();          // action listener class

        btn1.addActionListener(handler);                // textarea action listener
        btn2.addActionListener(handler);                // textfield 


        bold.addItemListener(new handler1(bf));
        italic.addItemListener(new handler1(itf));


        setVisible(true);               // making GUI visible
        setSize(400,400);               // pixels (x * y)
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             // Frame quits when X is clicked on top right corner

    }

    public class thehandler implements ActionListener{

        public void actionPerformed(ActionEvent e) {

            if(e.getSource() == btn1) {

                // change focus to textarea named area

                area.requestFocusInWindow();

            }


            if(e.getSource() == btn2) {

                fld.requestFocusInWindow();
            }               


        }

    }

现在我想要的是,如果单击粗体单选按钮,无论选择哪个组件(textarea和textfield),该组件的文本都会变为粗体文本

    public class handler1 implements ItemListener{

        Font font; 

        public handler1(Font f) {

            font = f;
        }

        public void itemStateChanged(ItemEvent event) {

            if((area.isFocusOwner() == true) && (event.getSource() == bold)) {

                area.setFont(font);
            }

        }
    }

    public void addComponent(Component c, int px, int py) {

        gc.gridx = px;                      // column
        gc.gridy = py;                      // row

        add(c, gc);             // add components(textarea, radio button and all other stuff) to GUI
    }

    public static void main(String[] args) {

        new Prg_38();

    }

}

1 个答案:

答案 0 :(得分:1)

对于任何JComponent,例如JTextField,请使用JComponent.requestFocusInWindow()。对于基于AWT的TextArea,请将其更改为基于Swing的JTextArea并执行相同的操作。

所以:

TextArea area = new TextArea(5,10);

变为:

JTextArea area = new JTextArea(5,10);

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == btn1) {
        // change cursor to textarea named area
    }
    if(e.getSource() == btn2) {
         // change cursor to textfield if btn2 is clicked
    }
}

变为:

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == btn1) {
        area.requestFocusInWindow();
    }
    if(e.getSource() == btn2) {
         fld.requestFocusInWindow();
    }
}