我是Java的新手,我正在创建一个带时区的时钟。
我对JComboBox
repaint
方法有一点问题。
如果我在代码中添加repaint()
,则无法在Applet窗口中看到JComboBox
,如果我删除了repaint()
,则可见。如您所知,将repaint()放入代码中是非常必要的。
我做错了什么?
` 包应用程序;
import java.awt。*;
import java.awt.event。*;
import java.util.Calendar;
导入javax.swing。*;
public class Alarm2 extends JFrame实现了ItemListener,Runnable {
private Graphics dbg;
private Image dbImage;
JComboBox c=new JComboBox();
Thread t;
int h,m,s;
boolean alarm=false;
String time="";
TextField tf=new TextField("SEX:",10);
Alarm2(){
setTitle("BALVEER");
setSize(250,250);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.addItem("MALE");
c.addItem("FEMALE");
setBackground(Color.GREEN);
c.addItemListener(this);
add(c);
add(tf);
setLayout(new FlowLayout(FlowLayout.RIGHT,10,195));
setVisible(true);
}
public void aisha(){
t=new Thread(this);
t.start();
}
public void run(){
try{
while(true){
Calendar cal=Calendar.getInstance();
h=cal.get(Calendar.HOUR);
m=cal.get(Calendar.MINUTE);
s=cal.get(Calendar.SECOND);
t.sleep(1000);
time=" "+h+" : "+m+" : "+s;
repaint();
}
}
catch(Exception e){
}
}
public void itemStateChanged(ItemEvent e){
tf.setText("SEX: "+e.getItem());
}
public void paint(Graphics g){
dbImage =createImage(getWidth(),getHeight());
dbg=dbImage.getGraphics();
draw(dbg);
g.drawImage(dbImage,0,0,this);
}
public void draw(Graphics g){
g.setColor(Color.red);
g.drawString("TIME:"+time, 50, 100);
}
public static void main(String[] args) {
Alarm2 java=new Alarm2();
java.aisha();
}
}`