学习事件处理



所以我在大学里学习了一些java,并展望未来,因为我们还没有学习到这一点。我试着通过查看未来的考试问题等来自学事件处理和gui。到目前为止,我已经掌握了gui相当方便的功能,但事件处理并没有那么多。。。我已经做了一段时间了,我似乎根本无法理解它们,我正在努力做到,当按下按钮时,它会将JTextField的长度()返回到JLabel中,任何帮助都将不胜感激。感谢

   import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
public class guiWithCatchBlock extends JFrame implements ActionListener, MouseListener {

guiWithCatchBlock() {
    super("Attempting Event Handling");
    Container c = getContentPane();
    JButton stringLengthButton = new JButton("Get String Length");
    JTextField inputField = new JTextField();
    JLabel outputLabel = new JLabel("String Length = ");
    stringLengthButton.addActionListener(this);
    inputField.addActionListener(this);
    outputLabel.addMouseListener(this);
    c.add(stringLengthButton,BorderLayout.NORTH);
    c.add(inputField,BorderLayout.CENTER);
    c.add(outputLabel,BorderLayout.SOUTH);
    setSize(400, 300);
    show();
}
public static void main(String args[]) {
    guiWithCatchBlock testAction = new guiWithCatchBlock();
}
public void actionPerformed(ActionEvent e) {
    System.out.print(paramString());
}
@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
}

}

您需要将ActionListener传递给按钮

stringLengthButton.addActionListener(this);

您可以在实现ActionListener类时使用它。现在将触发的事件是:

public void actionPerformed(ActionEvent e) {
    // Do GUI manipulations
    System.out.print(paramString());
}

如果您试图在构造函数中声明inputField和outputLabel,以后很难引用,最好将其声明为类成员
在您的操作Perform中,尝试获取文本长度并像这样重新标记

@Override
public void actionPerformed(ActionEvent e) {
    outputLabel.setText("String Length = " + inputField.getText().length());
}

作为完成代码

public class guiWithCatchBlock extends JFrame implements ActionListener, MouseListener {
    JTextField inputField;
    JLabel outputLabel;
    guiWithCatchBlock () {
        super("Attempting Event Handling");
        Container c = getContentPane();
        JButton stringLengthButton = new JButton("Get String Length");
        inputField = new JTextField();
        outputLabel = new JLabel("String Length = ");
        stringLengthButton.addActionListener(this);
        inputField.addActionListener(this);
        outputLabel.addMouseListener(this);
        c.add(stringLengthButton, BorderLayout.NORTH);
        c.add(inputField, BorderLayout.CENTER);
        c.add(outputLabel, BorderLayout.SOUTH);
        setSize(400, 300);
        show();
    }
    public static void main(String args[]) {
        guiWithCatchBlock testAction = new guiWithCatchBlock ();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        outputLabel.setText("String Length = " + inputField.getText().length());
    }
    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }
    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }
}

相关内容

  • 没有找到相关文章

最新更新