actionPerformed(ActionEvent e) 的正确语法是什么?
What is the proper syntax for actionPerformed(ActionEvent e)?
package Kinematics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
public class Display extends JFrame implements Runnable{
private static final long serialVersionUID = 938633141149262027L;
static JButton b2 = new JButton("v/t");
static JButton b1 = new JButton("d/t");
static JFrame frame = new JFrame("Kinematics Conversions");
static JFrame frame2 = new JFrame("Kinematics Conversions");
public static void main(String[] args){
frame.setVisible(true);
JPanel p = new JPanel();
frame.setPreferredSize(new Dimension(500, 500));
b1.setBounds(5, 5, 5, 5);
b1.addActionListener(ActionListener -> {
public void actionPerformed(ActionEvent e){ //says actionPerformed cannot have void return type
if(b2.getModel().isPressed()) { //however the oracle site says otherwise
frame.setVisible(false);
frame2.setVisible(true);
}
}
});
b2.setBounds(50, 50, 5, 5);
p.add(b1);
p.add(b2);
frame.add(p);
frame.pack();
}
@Override
public void run() {
if(b2.getModel().isPressed()) {
frame.setVisible(false);
frame2.setVisible(true);
}
}
}
好的,所以我有正确的语法..我想。我去 oracle 网站查看语法和正确的 return 类型,但是 ECLIPSE OXYGEN 2.0 说 actionPerformed 不能有 return 类型的 void。这是为什么?
你要拿定主意。
您可以或者使用匿名内部class(如概述here,那里有一个动作侦听器示例)。
或者您使用 java8 风格的 lambda 表达式。
您当前的代码只是两种想法的混合体。语法无效,源于将两种完全不同的方法放在一起的想法。
package Kinematics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
public class Display extends JFrame implements Runnable{
private static final long serialVersionUID = 938633141149262027L;
static JButton b2 = new JButton("v/t");
static JButton b1 = new JButton("d/t");
static JFrame frame = new JFrame("Kinematics Conversions");
static JFrame frame2 = new JFrame("Kinematics Conversions");
public static void main(String[] args){
frame.setVisible(true);
JPanel p = new JPanel();
frame.setPreferredSize(new Dimension(500, 500));
b1.setBounds(5, 5, 5, 5);
b1.addActionListener(ActionListener -> {
public void actionPerformed(ActionEvent e){ //says actionPerformed cannot have void return type
if(b2.getModel().isPressed()) { //however the oracle site says otherwise
frame.setVisible(false);
frame2.setVisible(true);
}
}
});
b2.setBounds(50, 50, 5, 5);
p.add(b1);
p.add(b2);
frame.add(p);
frame.pack();
}
@Override
public void run() {
if(b2.getModel().isPressed()) {
frame.setVisible(false);
frame2.setVisible(true);
}
}
}
好的,所以我有正确的语法..我想。我去 oracle 网站查看语法和正确的 return 类型,但是 ECLIPSE OXYGEN 2.0 说 actionPerformed 不能有 return 类型的 void。这是为什么?
你要拿定主意。
您可以或者使用匿名内部class(如概述here,那里有一个动作侦听器示例)。
或者您使用 java8 风格的 lambda 表达式。
您当前的代码只是两种想法的混合体。语法无效,源于将两种完全不同的方法放在一起的想法。