对象概念理解有误,求帮助!!

zcwmxn 2003-06-12 11:31:17
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyWork extends Applet {
DrawControl dc = new DrawControl();
DrawPanel dp = new DrawPanel();
public void init(){
setLayout(new BorderLayout());
add("North",dc);
add("Center",dp);
}
}

class DrawControl1 extends Panel{
Label lb1 = new Label("第一季度");
Label lb2 = new Label("第二季度");
Label lb3 = new Label("第三季度");
Label lb4 = new Label("第四季度");
TextField tf1 = new TextField("",3);
TextField tf2 = new TextField("",3);
TextField tf3 = new TextField("",3);
TextField tf4 = new TextField("",3);
public DrawControl1(){
setLayout(new FlowLayout());
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(lb3);
add(tf3);
add(lb4);
add(tf4);
}
}

class DrawControl2 extends Panel implements ActionListener{
Button bt1 = new Button("确定");
Button bt2 = new Button("关闭");
public DrawControl2(){
CheckboxGroup cbg = new CheckboxGroup();
add(new Checkbox("柱状", cbg, true));
add(new Checkbox("饼状", cbg, false));
setLayout(new FlowLayout());
add(bt1);
bt2.addActionListener(this);
add(bt2);
bt2.addActionListener(this);
}
public void actionPerformed(ActionEvent event){
if (event.getSource() == bt1){
//我想在此调用DrawPanel的repaint方法,也就是在dp里显示字符串
}
if (event.getSource() == bt2){
System.exit(0);
}

}
}

class DrawControl extends Panel{
DrawControl1 dc1 = new DrawControl1();
DrawControl2 dc2 = new DrawControl2();
public DrawControl(){
setLayout(new FlowLayout());
add(dc1);
add(dc2);

}
}

class DrawPanel extends Panel{
public DrawPanel() {
setBackground(Color.white);
}
public void paint(Graphics g){}
public void repaint(Graphics g){
g.drawString("dd",20,30);
g.drawString("333333333",30,20);
}

}
我刚开始学JAVA有许多概念还是理解的很不彻底,我现在试着写些简单的程序,希望在实践中理解,上边就是我的一个
public void actionPerformed(ActionEvent event){
if (event.getSource() == bt1){
//我想在此调用DrawPanel的repaint方法,也就是在dp里显示字符串
}
if (event.getSource() == bt2){
System.exit(0);
}

}
估计我的对象理解还不透彻,哪位前辈能帮我填写一下
...全文
25 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zcwmxn 2003-06-18
  • 打赏
  • 举报
回复
偶上周看了<thinking in java>《java编程思想》很好,深受启发!!
发现原来写的是乱七八糟。我的程序写完了,还有一点没有完成,就是如何判断输入框输入的一定是数字,还有为什么30/100 = 0.300000002 。请大家指正!
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyWork extends Applet{
TextField tf1 = new TextField("10",3);
TextField tf2 = new TextField("20",3);
TextField tf3 = new TextField("30",3);
TextField tf4 = new TextField("40",3);
Button bt1 = new Button("确定");
Button bt2 = new Button("关闭");
Checkbox cb1,cb2;
Panel p1 = new Panel();

public void init(){
Label lb1 = new Label("第一季度");
Label lb2 = new Label("第二季度");
Label lb3 = new Label("第三季度");
Label lb4 = new Label("第四季度");
p1.setLayout(new FlowLayout());
p1.add(lb1);
p1.add(tf1);
p1.add(lb2);
p1.add(tf2);
p1.add(lb3);
p1.add(tf3);
p1.add(lb4);
p1.add(tf4);
Panel p2 = new Panel();
p2.setLayout(new FlowLayout());
CheckboxGroup cbg = new CheckboxGroup();
p2.add(cb1 = new Checkbox("柱状", cbg, true));
p2.add(cb2 = new Checkbox("饼状", cbg, false));
p2.setLayout(new FlowLayout());
p2.add(bt1);
bt1.addActionListener(new B1());
p2.add(bt2);
bt2.addActionListener(new B2());
add("North", p1);
add("North", p2);
Panel p3 = new Panel();
add("Center", p3);
}

public void paint(Graphics g){
float x1,x2,x3,x4;
int i;
char temp;
int wide = getSize().width/2 ;
int high = getSize().height/2 ;
x1 = Float.parseFloat(tf1.getText().trim());
x2 = Float.parseFloat(tf2.getText().trim());
x3 = Float.parseFloat(tf3.getText().trim());
x4 = Float.parseFloat(tf4.getText().trim());
/*判断是否为数字
for(i=0;i<tf1.getText().length();i++){
?????????????????
temp = tf1.getText().charAt(i);
}
*/
float p;
int p1,p2,p3,p4;
p = x1 + x2 + x3 + x4;
//画图例
g.drawRect(50,100,100,100);
g.setColor(Color.red);
g.fillRect(55,110,10,10);
g.setColor(Color.BLACK);
g.drawString("一季度"+tf1.getText().trim()+"--"+x1/p*100+"%",70,120);
g.setColor(Color.green);
g.fillRect(55,130,10,10);
g.setColor(Color.BLACK);
g.drawString("二季度"+tf2.getText().trim()+"--"+x2/p*100+"%",70,140);
g.setColor(Color.blue);
g.fillRect(55,150,10,10);
g.setColor(Color.BLACK);
g.drawString("三季度"+tf3.getText().trim()+"--"+x3/p*100+"%",70,160);
g.setColor(Color.yellow);
g.fillRect(55,170,10,10);
g.setColor(Color.BLACK);
g.drawString("四季度"+tf4.getText().trim()+"--"+x4/p*100+"%",70,180);
if(cb1.getState()== true ){
//画柱状图
p1 = (int)((x1 / p) * 460);
p2 = (int)((x2 / p) * 460);
p3 = (int)((x3 / p) * 460);
p4 = (int)((x4 / p) * 460);
if (p1>240 || p2> 240 || p3 > 240 || p4 > 240){
p1 = (int)((x1 / p) * 240);
p2 = (int)((x2 / p) * 240);
p3 = (int)((x3 / p) * 240);
p4 = (int)((x4 / p) * 240);
}
//坐标轴
g.drawLine(wide-120,high+120,wide+230,high+120);
g.drawLine(wide+230,high+120,wide+225,high+125);
g.drawLine(wide+230,high+120,wide+225,high+115);
g.drawLine(wide-120,high+120,wide-120,high-120);
g.drawLine(wide-120,high-120,wide-125,high-115);
g.drawLine(wide-120,high-120,wide-115,high-115);
for(i=wide-120;i<wide+230;i++){
if (i%20 == 0){
g.drawLine(i,high+120,i,high+122);
}
}
for(i=high+120;i>high-120;i--){
if (i%20 == 0){
g.drawLine(wide-120,i,wide-118,i);
}
}
//柱状图
g.setColor(Color.red);
g.fillRect(wide-90,high+120-p1,50,p1);
g.setColor(Color.green);
g.fillRect(wide-10,high+120-p2,50,p2);
g.setColor(Color.blue);
g.fillRect(wide+70,high+120-p3,50,p3);
g.setColor(Color.yellow);
g.fillRect(wide+150,high+120-p4,50,p4);
}
else if(cb2.getState()== true ){
//画饼状图
p1 = (int)((x1 / p) * 360);
p2 = (int)((x2 / p) * 360);
p3 = (int)((x3 / p) * 360);
p4 = (int)((x4 / p) * 360);
g.setColor(Color.red);
g.fillArc(wide-150,high-120,320,320,0,p1);
g.setColor(Color.green);
g.fillArc(wide-150,high-120,320,320,p1,p2);
g.setColor(Color.blue);
g.fillArc(wide-150,high-120,320,320,p1+p2,p3);
g.setColor(Color.yellow);
g.fillArc(wide-150,high-120,320,320,p1+p2+p3,360-p1-p2-p3);
}
}
class B1 implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
}
}
class B2 implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
}
zhaolizu 2003-06-13
  • 打赏
  • 举报
回复
感谢你对我的信任!
昨天晚上下班了,呵呵:)
你的代码我没有仔细看,没有时间。

是不是你的模块一加载,就显示字符了?
那就想办法在加载是避免就行了,在点击按钮的时候触发。
注意画图的“g”的拥有者是谁?不要搞错了。
祝你好运:)
zcwmxn 2003-06-13
  • 打赏
  • 举报
回复
继续请求回答!!!!!!
zcwmxn 2003-06-13
  • 打赏
  • 举报
回复
偶还是不得其意啊!我哭……………………
zcwmxn 2003-06-12
  • 打赏
  • 举报
回复
是调用方法了,但是结果不对,我怀疑是不是我的paint方法不对
zhaolizu 2003-06-12
  • 打赏
  • 举报
回复
public void actionPerformed(ActionEvent event){
if (event.getSource() == bt1){
。。。。。。

不是调用点击按钮的方法吗?
zcwmxn 2003-06-12
  • 打赏
  • 举报
回复
前边两位的说法我明白,但现在我是想按下按钮以后才写出字
zcwmxn 2003-06-12
  • 打赏
  • 举报
回复
大家都是学习的,不过希望大家能够回答时候准确一些。呵呵,总是让我试试,呵呵,偶要是会的话也不老费大家了。希望各位前辈费费心,最好通过了,或准确点回答小弟,这里谢了
ye255 2003-06-12
  • 打赏
  • 举报
回复
DrawPanel.repaint();//这样不行么?
我也是刚学,来学习学习,呵呵。
zhaolizu 2003-06-12
  • 打赏
  • 举报
回复
public void actionPerformed(ActionEvent event){
if (event.getSource() == bt1){
//我想在此调用DrawPanel的repaint方法,也就是在dp里显示字符串

this.repaint();//好像是,记不清了,你试试:)
}
}
zcwmxn 2003-06-12
  • 打赏
  • 举报
回复
谢谢两位,我想问的是
public void actionPerformed(ActionEvent event){
if (event.getSource() == bt1){
//我想在此调用DrawPanel的repaint方法,也就是在dp里显示字符串
}
}
这段怎么写
Wang_com 2003-06-12
  • 打赏
  • 举报
回复
repaint()函数不能带Graphics参数的。
否则就不能重载父类的repaint()方法了。
public void paint(Graphics g){
super.paint(g);
g.drawString("dd",20,30);
g.drawString("333333333",30,20);
}
icewhite 2003-06-12
  • 打赏
  • 举报
回复
repaint();好像就可以!
public void paint(Graphics g){}
public void repaint(Graphics g){
g.drawString("dd",20,30);
g.drawString("333333333",30,20);
}
〉〉〉〉〉〉〉〉〉〉〉〉〉〉
写成
public void paint(Graphics g){
g.drawString("dd",20,30);
g.drawString("333333333",30,20);
}

记得不太清楚了,嘻嘻

62,615

社区成员

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

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