java swing事件
我希望当我输入一个按钮时,文本显示在控制台中。我该如何结合我所熟悉的方法,有人能解释并举例说明吗。试试看java swing事件,java,swing,Java,Swing,我希望当我输入一个按钮时,文本显示在控制台中。我该如何结合我所熟悉的方法,有人能解释并举例说明吗。试试看 JButton button = new JButton("Button1"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("
JButton button = new JButton("Button1");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button1 was Clicked!");
}
});
// add button to a container
试一试
用鼠标听器。例如:
JComponent button = new JButton();
component.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered the button");
}
});
MouseAdapter是一种特殊的MouseListener,它具有MouseListener提供的所有其他方法的默认空实现,因此您不必重写它们。您可能需要查看Javadoc中的MouseAdapter、MouseListener和MouseEvent。使用MouseListener。例如:
JComponent button = new JButton();
component.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered the button");
}
});
MouseAdapter是一种特殊的MouseListener,它具有MouseListener提供的所有其他方法的默认空实现,因此您不必重写它们。您可能需要查看Javadoc中的MouseAdapter、MouseListener和MouseEvent。在按钮中添加一个
ActionListener
。在actionPerformed()
方法中,在控制台上打印文本或任何您想要的内容 向按钮添加一个ActionListener
。在actionPerformed()
方法中,在控制台上打印文本或任何您想要的内容 啊,我误解了你的问题——我以为你想知道光标是否进入了按钮。如果你不想知道按钮是否被点击,Bala R的解决方案就是。啊,我误解了你的问题——我想你想知道光标是否进入了按钮。如果你不想知道按钮是否被点击,Bala R的解决方案是可行的。你为什么不读一本教程呢?答案相当简单,在每本书和Swing教程中都很容易找到。你为什么不读一本教程呢?答案非常简单,在每本书和Swing教程中都可以找到。tnx对所有人来说,现在我了解了使用它们的主要方法:)注意,在控制台上有一个GUI来打印用户想要的任何东西是“不寻常的”。为什么不使用一个按钮和一个JTextField
或JTextArea
将输出写入?tnx对所有人来说,现在我了解了使用它们的主要方法:)注意,在控制台上有一个GUI来打印用户想要的任何东西是“不寻常的”。为什么不使用按钮和JTextField
或JTextArea
来写入输出?