Java 我创建了一个程序,但由于某些原因,出现以下错误:
每当我编译代码时,似乎都会出现以下错误: 测试不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent) 公共类测试扩展JFrame实现ActionListener ^ 首先看一看Java 我创建了一个程序,但由于某些原因,出现以下错误:,java,swing,Java,Swing,每当我编译代码时,似乎都会出现以下错误: 测试不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent) 公共类测试扩展JFrame实现ActionListener ^ 首先看一看 您的Test类实现了ActionListener接口,但没有实现合同中描述的actionPerformed方法,而是使用了一系列匿名类因为您的类实现了“ActionListener”,这是一个接口,所以会出现此错误。但是,您没有创建/实现所需的action
您的
Test
类实现了ActionListener
接口,但没有实现合同中描述的actionPerformed
方法,而是使用了一系列匿名类因为您的类实现了“ActionListener”,这是一个接口,所以会出现此错误。但是,您没有创建/实现所需的actionPerformed()
方法。Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。请改用布局管理器,或者与布局填充和边框一起使用。您可能有一个示例来说明该做什么吗?公共类测试扩展JFrame//implements ActionListener
。。。教程中的链接会给你更多的想法
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame implements ActionListener {
Font Ft = new Font("Verdana",Font.BOLD,14);
JLabel Lb1 = new JLabel("Select an add-on to your burger");
JCheckBox Egg = new JCheckBox("Egg Php10");
JCheckBox Cheese = new JCheckBox("Cheese Php10");
JCheckBox Bacon = new JCheckBox("Bacon Php15");
JButton UnCheck = new JButton("UnCheck All");
JButton CheckAll = new JButton("Check All");
JButton Submit = new JButton("Submit");
JLabel Lb2 = new JLabel("Total Amount: ");
JLabel Lb3 = new JLabel("");
public Test()//Swing Components
{
setTitle("Color Settings");
setSize(300,420);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.BLACK);
Lb1.setFont(Ft);
Lb1.setBounds(20,30,300,20);
Lb1.setForeground(Color.YELLOW);
Egg.setFont(Ft);
Egg.setBounds(40,65,300,20);
Egg.setForeground(Color.WHITE);
Egg.setBackground(Color.BLACK);
Cheese.setFont(Ft);
Cheese.setBounds(40,95,300,20);
Cheese.setForeground(Color.WHITE);
Cheese.setBackground(Color.BLACK);
Bacon.setFont(Ft);
Bacon.setBounds(40,125,300,20);
Bacon.setForeground(Color.WHITE);
Bacon.setBackground(Color.BLACK);
UnCheck.setBounds(20,160,100,20);
CheckAll.setBounds(150,160,100,20);
Submit.setBounds(70,200,100,20);
Lb2.setFont(Ft);
Lb2.setBounds(20,250,300,20);
Lb2.setForeground(Color.YELLOW);
Lb3.setFont(Ft);
Lb3.setBounds(150,250,300,20);
Lb3.setForeground(Color.YELLOW);
add(Lb1);
add(Egg);
add(Cheese);
add(Bacon);
add(UnCheck);
add(CheckAll);
add(Submit);
add(Lb2);
add(Lb3);
UnCheck.addActionListener(new ActionListener()//First button to have a separate action.
{
public void actionPerformed(ActionEvent E)
{
Egg.setSelected(false);
Cheese.setSelected(false);
Bacon.setSelected(false);
}
});
CheckAll.addActionListener(new ActionListener()//Second button to have a separate action.
{
public void actionPerformed(ActionEvent E)
{
Egg.setSelected(true);
Cheese.setSelected(true);
Bacon.setSelected(true);
}
});
Submit.addActionListener(new ActionListener()//Third button to have a separate action.
{
public void actionPerformed(ActionEvent E)
{
int price;
if (Egg.isSelected())
{
price = price + 10;
}
if (Cheese.isSelected())
{
price = price +10;
}
if (Bacon.isSelected())
{
price = price + 15;
}
Lb3.setText("Php" + price);
}
});
}
public static void main (String[] args)
{
Test Fr = new Test();
Fr.setVisible(true);
}
}