所以我在大学里学了一点java,并展望未来,因为我们不是在这方面,但我试图通过查看未来的考题等来自学事件处理和gui,到目前为止,我已经掌握了gui的相当方便,但事件处理还不是很多……我已经在这方面做了一段时间了,我似乎根本不能理解它们,我试图得到当按钮被按下时,它会将JTextField的length()返回到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
}}
发布于 2016-03-30 23:21:49
您需要将ActionListener传递给按钮
stringLengthButton.addActionListener(this);您可以在实现ActionListener类时使用它。现在将触发的事件是:
public void actionPerformed(ActionEvent e) {
// Do GUI manipulations
System.out.print(paramString());
}发布于 2016-03-31 00:14:16
如果你试图在你的构造函数中声明你的inputField和outputLabel,那么以后就很难引用了,最好把它声明为类成员。
在actionPerform中,尝试获取文本长度并重新标注,如下所示
@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
}
}https://stackoverflow.com/questions/36312887
复制相似问题