我是Java新手,我正在创建一个带时区的时钟 . 我对 JComboBox
的 repaint
方法有一点问题 . 如果我将 repaint()
放在我的代码中,我在Applet窗口中看不到 JComboBox
,如果我删除了 repaint()
,那么它是可见的 . 如您所知,将repaint()放入代码中是非常必要的 .
我究竟做错了什么?
`包应用;
import java.awt . *;
import java.awt.event . *;
import java.util.Calendar;
import javax.swing . *;
公共类Alarm2扩展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();
}
}`