黑马程序员技术交流社区
标题:
applet学习疑惑!
[打印本页]
作者:
逍林游
时间:
2013-1-30 18:30
标题:
applet学习疑惑!
为什么tf1.requestFocus();不聚焦;
为什么我写上了invalidate();validate();,还要缩放一下applet温度变化曲线才会出来!
刚学习这个,所以还不太熟悉,请帮下忙,下面是部分代码!!
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class LineGraph extends Applet implements ActionListener
{
int a1,a2,a3,a4,a5;
Label Title=new Label("Enter 5 daily temperatures and press ENTER in box 5");
Label day1=new Label("Day1");
TextField tf1=new TextField("");
Label day2=new Label("Day2");
TextField tf2=new TextField("");
Label day3=new Label("Day3");
TextField tf3=new TextField("");
Label day4=new Label("Day4");
TextField tf4=new TextField("");
Label day5=new Label("Day5");
TextField tf5=new TextField("");
public void init()
{
add(Title);
add(day1);
add(tf1);
add(day2);
add(tf2);
add(day3);
add(tf3);
add(day4);
add(tf4);
add(day5);
add(tf5);
tf5.addActionListener(this);
tf4.addActionListener(this);
tf3.addActionListener(this);
tf2.addActionListener(this);
tf1.addActionListener(this);
tf1.requestFocus();
}
public void paint(Graphics g)
{
g.drawLine(20,300,180,300); //温度变化曲线
g.drawLine(20, 70, 20, 300);
g.drawLine(20,(300-4*a1),60,(300-4*a2));
g.drawLine(60,(300-4*a2),100,(300-4*a3));
g.drawLine(100,(300-4*a3),140,(300-4*a4));
g.drawLine(140,(300-4*a4),180,(300-4*a5));
}
public void actionPerformed(ActionEvent arg0)
{
a1=Integer.parseInt(tf1.getText());
a2=Integer.parseInt(tf2.getText());
a3=Integer.parseInt(tf3.getText());
a4=Integer.parseInt(tf4.getText());
a5=Integer.parseInt(tf5.getText());
invalidate();
validate();
}
}
作者:
高境
时间:
2013-2-1 19:15
怎么没人回答的
作者:
铿锵科技
时间:
2013-2-1 20:18
因为不懂所以没人答
作者:
赵熠东(赵东东)
时间:
2013-2-16 22:03
本帖最后由 赵熠东(赵东东) 于 2013-2-16 22:06 编辑
只要将 invalidate(); validate();替换成repaint();就行了,另外我在myeclipse中运行的时候并没有出现tf1不聚焦的现象,完整代码如下
package TestJava;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class LineGraph extends Applet implements ActionListener {
int a1, a2, a3, a4, a5;
Label Title = new Label(
"Enter 5 daily temperatures and press ENTER in box 5");
Label day1 = new Label("Day1");
TextField tf1 = new TextField("");
Label day2 = new Label("Day2");
TextField tf2 = new TextField("");
Label day3 = new Label("Day3");
TextField tf3 = new TextField("");
Label day4 = new Label("Day4");
TextField tf4 = new TextField("");
Label day5 = new Label("Day5");
TextField tf5 = new TextField("");
public void init() {
add(Title);
add(day1);
add(tf1);
add(day2);
add(tf2);
add(day3);
add(tf3);
add(day4);
add(tf4);
add(day5);
add(tf5);
tf5.addActionListener(this);
tf4.addActionListener(this);
tf3.addActionListener(this);
tf2.addActionListener(this);
tf1.addActionListener(this);
tf1.requestFocus();
}
public void paint(Graphics g) {
g.drawLine(20, 300, 180, 300); // 温度变化曲线
g.drawLine(20, 70, 20, 300);
g.drawLine(20, (300 - 4 * a1), 60, (300 - 4 * a2));
g.drawLine(60, (300 - 4 * a2), 100, (300 - 4 * a3));
g.drawLine(100, (300 - 4 * a3), 140, (300 - 4 * a4));
g.drawLine(140, (300 - 4 * a4), 180, (300 - 4 * a5));
}
public void actionPerformed(ActionEvent arg0) {
a1 = Integer.parseInt(tf1.getText());
a2 = Integer.parseInt(tf2.getText());
a3 = Integer.parseInt(tf3.getText());
a4 = Integer.parseInt(tf4.getText());
a5 = Integer.parseInt(tf5.getText());
//invalidate();
//validate();
repaint();
}
}
复制代码
QQ截图20130216220011.png
(31.25 KB, 下载次数: 23)
下载附件
2013-2-16 22:01 上传
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2