import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class cwp2
{
public static void main(String[] args)
{
MyFrame F1 = new MyFrame();
}
}
class MyFrame extends JFrame
{
MyFrame()
{
//main window
setSize(480,300);
MyPanel mainpanel = new MyPanel();
mainpanel.setLocation(300,300);
add(mainpanel);
setTitle("Super Screen & Keyboard");
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setVisible(true);
}
}
class MyPanel extends JPanel implements ActionListener
{
private JTextArea screen;
private JButton spacebar,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
MyPanel()
{
//text screen
JTextArea screen = new JTextArea();
screen.setColumns(40);
screen.setRows(9);
screen.setWrapStyleWord(true);
screen.setLineWrap(true);
//buttons and actions
spacebar = new JButton("spacebar");
spacebar.addActionListener(this);
a = new JButton("a");
a.addActionListener(this);
b = new JButton("b");
b.addActionListener(this);
c = new JButton("c");
c.addActionListener(this);
d = new JButton("d");
d.addActionListener(this);
e = new JButton("e");
e.addActionListener(this);
f = new JButton("f");
f.addActionListener(this);
g = new JButton("g");
g.addActionListener(this);
h = new JButton("h");
h.addActionListener(this);
i = new JButton("i");
i.addActionListener(this);
j = new JButton("j");
j.addActionListener(this);
k = new JButton("k");
k.addActionListener(this);
l = new JButton("l");
l.addActionListener(this);
m = new JButton("m");
m.addActionListener(this);
n = new JButton("n");
n.addActionListener(this);
o = new JButton("o");
o.addActionListener(this);
p = new JButton("p");
p.addActionListener(this);
q = new JButton("q");
q.addActionListener(this);
r = new JButton("r");
r.addActionListener(this);
s = new JButton("s");
s.addActionListener(this);
t = new JButton("t");
t.addActionListener(this);
u = new JButton("u");
u.addActionListener(this);
v = new JButton("v");
v.addActionListener(this);
w = new JButton("w");
w.addActionListener(this);
x = new JButton("x");
x.addActionListener(this);
y = new JButton("y");
y.addActionListener(this);
z = new JButton("z");
z.addActionListener(this);
add(q);
add(w);
add(e);
add(r);
add(t);
add(y);
add(u);
add(i);
add(o);
add(p);
add(a);
add(s);
add(d);
add(f);
add(g);
add(h);
add(j);
add(k);
add(l);
add(z);
add(x);
add(c);
add(v);
add(b);
add(n);
add(m);
add(spacebar);
add(screen);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == spacebar)
{
screen.append(" ");
}
else
{
screen.append(e.getSource.getActionCommand());
}
}
When I press a button I get an error message on the command prompt.
当我按一个按钮时,我在命令提示符上得到一个错误消息。
1 个解决方案
#1
1
This is wrong:
这是错误的:
e.getSource.getActionCommand()
The getSource()
method returns an object of Object type, and Object has no getActionCommand()
method.
getSource()方法返回对象类型的对象,对象没有getActionCommand()方法。
It's simply
这仅仅是
e.getActionCommand()
Since e is an ActionEvent variable, and this type in fact has this method.
因为e是一个ActionEvent变量,而这个类型实际上有这个方法。
Suggestion: next time you have a similar question, please show all complete error or exception messages and indicate in a comment which line throws it.
建议:下次你有类似的问题时,请显示所有的错误或异常信息,并在评论中指出哪一条线抛出。
Edit
编辑
Your new problem is due to your shadowing the screen variable.
你的新问题是由于你的遮蔽屏幕变量。
Your code:
你的代码:
class MyPanel extends JPanel implements ActionListener {
private JTextArea screen; // screen declared here
private JButton spacebar,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
MyPanel() {
//text screen
JTextArea screen = new JTextArea(); // but re-declared here!
This means that the screen variable that you initialize in the constructor is a local variable and not the field that was declared in the class.
这意味着在构造函数中初始化的屏幕变量是一个局部变量,而不是类中声明的字段。
This can be fixed by not redeclaring the variable:
这可以通过不重新声明变量来确定:
class MyPanel extends JPanel implements ActionListener {
private JTextArea screen; // screen declared here
private JButton spacebar,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
MyPanel() {
//text screen
// JTextArea screen = new JTextArea(); // but re-declared here!
screen = new JTextArea(); // note the difference?
#1
1
This is wrong:
这是错误的:
e.getSource.getActionCommand()
The getSource()
method returns an object of Object type, and Object has no getActionCommand()
method.
getSource()方法返回对象类型的对象,对象没有getActionCommand()方法。
It's simply
这仅仅是
e.getActionCommand()
Since e is an ActionEvent variable, and this type in fact has this method.
因为e是一个ActionEvent变量,而这个类型实际上有这个方法。
Suggestion: next time you have a similar question, please show all complete error or exception messages and indicate in a comment which line throws it.
建议:下次你有类似的问题时,请显示所有的错误或异常信息,并在评论中指出哪一条线抛出。
Edit
编辑
Your new problem is due to your shadowing the screen variable.
你的新问题是由于你的遮蔽屏幕变量。
Your code:
你的代码:
class MyPanel extends JPanel implements ActionListener {
private JTextArea screen; // screen declared here
private JButton spacebar,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
MyPanel() {
//text screen
JTextArea screen = new JTextArea(); // but re-declared here!
This means that the screen variable that you initialize in the constructor is a local variable and not the field that was declared in the class.
这意味着在构造函数中初始化的屏幕变量是一个局部变量,而不是类中声明的字段。
This can be fixed by not redeclaring the variable:
这可以通过不重新声明变量来确定:
class MyPanel extends JPanel implements ActionListener {
private JTextArea screen; // screen declared here
private JButton spacebar,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
MyPanel() {
//text screen
// JTextArea screen = new JTextArea(); // but re-declared here!
screen = new JTextArea(); // note the difference?