책에있는 과제의 지시 사항을 따르고 있습니다. 나는 단추를 만들고 있는데, 한 단추는 "노란색"을 클릭하면 사용자가 배경을 노란색으로 바꾼다. 컴파일 오류가 발생합니다.자바 초보자가 기호 오류를 찾을 수 없습니다.
Error; cannot find symbol
add(Red, BorderLayout.Red);
same goes for add(Yellow, BorderLayout.Yellow);
add(Cyan, BorderLayout.CYAN);
add(Magenta, BorderLayout.MAGENTA);
add(White, BorderLayout.WHITE);
also error; cannot find symbol
for ButtonRed.addActionListener(this);
ButtonYellow.addActionListener(this);
ButtonCyan.addActionListener(this);
ButtonMagenta.addActionListner(this);
ButtonWhite.addActionListener(this);
여기 내 코드입니다.
/*
Chapter 6: Borders
Programmer:Jesse-le Edwards
Date:11-16-14
Filename: Buttons.java
Purpose:
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Buttons extends Frame implements ActionListener {
public void paint(Graphics g) {
setBackground(Color.red);
}
public void actionPerformed(ActionEvent e) {
String arg = e.getActionCommand();
if (arg == "Yellow") {
setBackground(Color.yellow);
}
}
public Buttons() {
//set the layout
setLayout(new BorderLayout(20, 5));
//Add buttons
Button Red = new Button("Red");
Button Yellow = new Button("Yellow");
Button Cyan = new Button("Cyan");
Button West = new Button("Magenta");
Button White = new Button("White");
add(Red, BorderLayout.RED);
add(Yellow, BorderLayout.YELLOW);
add(Cyan, BorderLayout.CYAN);
add(Magenta, BorderLayout.MAGENTA);
add(White, BorderLayout.WHITE);
ButtonRed.addActionListener(this);
ButtonYellow.addActionListener(this);
ButtonCyan.addActionListener(this);
ButtonMagenta.addActionListner(this);
ButtonWhite.addActionListener(this);
//override the windowClosing event
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
public static void main(String[] args) {
// set frame properties
Buttons f = new Buttons();
f.setTitle("Border Application");
f.setBounds(200, 200, 300, 300);
f.setVisible(true);
}
}
[BorderLayout'] (https://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html) JavaDocs에서 시작하여 필드가 없습니다. 'Red' (또는 당신이 사용하려고 시도한 다른 값)라고 불리는 것입니다. 그렇다면 [자바에서 문자열을 어떻게 비교합니까?] (http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)를보십시오. 왜냐하면'arg == "Yellow" ''String''이 Java에서 어떻게 비교되는지 ... – MadProgrammer
문자열은'foo.equals ("bar")'와 비교되어야합니다. 이것은 당신의 코드에서 arg.equals ("Yellow")'가 될 것이다. 'Button'은'JButton'이어야합니다. 변수 이름은'ActionListener'를 추가 한 행과 일치하지 않습니다. 'ButtonMagenta.addActionListner (this);는 ListA에있는 E를 유의하여 addActionListener (this) 여야합니다. – Charlie
'ButtonRed '란 무엇입니까? 나는 그것의 'Red', Red.addActionListener (this)로 변경한다고 생각한다; – Anptk