我在Java编程时遇到问题,出现错误,特向大家求教

gaozhenguo52 2008-04-14 03:41:29
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.io.*;

class A extends Frame implements ActionListener {


TextField t1,t2,t3;
Button b1,b2,b3,b4;
A(String n){
super(n);
t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);
b1 = new Button("+");
b2 = new Button("-");
b3 = new Button("*");
b4 = new Button("/");
setLayout(new BorderLayout());
Panel p1 = new Panel();
Panel p2 = new Panel();
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.SOUTH);
FlowLayout f1 = new FlowLayout();
p1.setLayout(f1);
FlowLayout f2 = new FlowLayout();
p2.setLayout(f2);
p1.add(t1);
p1.add(t2);
p1.add(t3);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setVisible(true);
setBounds(300,350,300,300);
pack();
validate();
}
public void actionPerformed(ActionEvent e){
int m = Integer.parseInt(t1.getText());
int n = Integer.parseInt(t2.getText());
if (e.getSource()==b1)
{ int h = m + n;
t3.setText(""+h);
}
if (e.getSource()==b2)
{ int h = m - n;
t3.setText(""+h);
}
if
(e.getSource()==b3)
{
int h =m * n;
t3.setText(""+h);
}
if
(e.getSource()==b4)
{
int h =m / n;
t3.setText(""+h);

}
}
}
public class F{
public static void main(String args[]){
A f = new A("计算");
f.setSize(600,600);
f.getContentPane().setBackground(Color.RED);
JPanel contentPane = new JPanel();

contentPane.setBackground(Color.yellow);
f.setVisible(true);


System.exit(0);
}
}


用TextPad编译时,却发现提示错误。我不知道该怎么修改。谢谢
...全文
114 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpoysy 2008-04-16
  • 打赏
  • 举报
回复
getContentPane().setBackground(Color.RED);只在JFrame类里面能用.
你如果真要用Frame,则可以直接用fsetBackground(Color.RED); 就OK了.
assdust110 2008-04-16
  • 打赏
  • 举报
回复
等我再来的时候,别人已经把我要说的话说光了
  • 打赏
  • 举报
回复
ding
学习
Inhibitory 2008-04-15
  • 打赏
  • 举报
回复
把Frame换成JFrame:
class A extends JFrame implements ActionListener {

如果不是必须, 推荐使用Swing的类, 而不是Awt
guofei_gf 2008-04-15
  • 打赏
  • 举报
回复
Frame没有getContentPane()这个方法,JFrame才有这个方法,你改成继承JFrame应该就可以了吧
gaozhenguo52 2008-04-15
  • 打赏
  • 举报
回复
你能不能具体说一下啊?我是菜鸟,刚学习Java,还不怎么理解呢。而且我耳聋学习时大部分是靠自学了。老师讲的听不见。谢谢了。
gaozhenguo52 2008-04-15
  • 打赏
  • 举报
回复
红色带下划线的。谢谢。
gaozhenguo52 2008-04-15
  • 打赏
  • 举报
回复
我是学生,刚刚接触Java,老师让我们用Panel显示。可惜我耳朵听不见,大部分的靠自学了,所以遇到的麻烦比较多。就像这一个题中的错误,老师没有太多时间,所以我也只能靠自己瞎摸了。呵呵。谢谢了,不过你这一个在运行时,不能显示按钮等控件啊。
helanpiaoxue 2008-04-15
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
//import java.io.*;

class Asdf extends Frame implements ActionListener {


TextField t1, t2, t3;
Button b1, b2, b3, b4;
Asdf(String n) {
super(n);

t1 = new TextField(10);
t2 = new TextField(10);
t3 = new TextField(10);

b1 = new Button("+");
b2 = new Button("-");
b3 = new Button("*");
b4 = new Button("/");

setLayout(new BorderLayout());

Panel p1 = new Panel();
Panel p2 = new Panel();
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);

FlowLayout f1 = new FlowLayout();
p1.setLayout(f1);
FlowLayout f2 = new FlowLayout();
p2.setLayout(f2);
p1.add(t1);
p1.add(t2);
p1.add(t3);
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);

setVisible(true);
setBounds(70, 70, 60, 200);
// pack();
validate();
}

public void actionPerformed(ActionEvent e) {
int m = Integer.parseInt(t1.getText());
int n = Integer.parseInt(t2.getText());
if (e.getSource() == b1) {
int h = m + n;
t3.setText("" + h);
}
if (e.getSource() == b2) {
int h = m - n;
t3.setText("" + h);
}
if
(e.getSource() == b3) {
int h = m * n;
t3.setText("" + h);
}
if
(e.getSource() == b4) {
int h = m / n;
t3.setText("" + h);

}
}

public static void main(String args[]) {
Asdf f = new Asdf("\u8BA1算");
f.setSize(400, 100);

// JPanel contentPane = new JPanel();
// contentPane.setBackground(Color.CYAN);
// f.add(contentPane);


f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{ System.exit(0); }
});

}
}
你加的pane是干什么用呢,以上是我该过后的,好用!
seeSkyblue 2008-04-14
  • 打赏
  • 举报
回复
报:The method getContentPane() is undefined for the type A
在类A中没有定义getContentPane()这个方法
assdust110 2008-04-14
  • 打赏
  • 举报
回复
什么错误,提示在哪一行啊

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧