显示一片空白,提示未初始化的小应用
发布网友
发布时间:2024-10-13 17:45
我来回答
共1个回答
热心网友
时间:2024-12-03 15:43
import java.applet.*;
import java.awt.event.*;
import javax.swing览11次import java.awt.*;.*;
public class linxing extends JApplet{
int n;
public void init() {
Container contentPane = getContentPane();
contentPane.add(new JCheckBoxPanel(this),"Center");
}
public void paint(Graphics g)
{
String k="";
Font f = new Font("TimesRoman",Font.BOLD,20);
int i,j,m,x=40,y=50;
int R,G,B;
for(i=0;i<n;i++)
{
for(j=n-i-1;j>0;j--)
{
k = k + " ";
}
for(m=0;m<(2*i+1);m++)
{
k = k + (i+1);
}
k = k+"\r\n";
R=(int)(Math.random()*255);
G=(int)(Math.random()*255);
B=(int)(Math.random()*255);
Color c = new Color(R,G,B);
g.setFont(f);
g.setColor(c);
g.drawString(k, x, y);
y+=20;
k="";
}//i=n
for( m=0;m<n-1;m++)
{
for(j=0;j<m+1;j++)
{
k = k + " ";
}
for( int s=2*n-3-2*j;s>0;s--)
{
k = k + (i+1);
}
i++;
k=k+"\r\n";
R=(int)(Math.random()*255);
G=(int)(Math.random()*255);
B=(int)(Math.random()*255);
Color c = new Color(R,G,B);
g.setFont(f);
g.setColor(c);
g.drawString(k, x, y);
y+=20;
k="";
}
}
CheckboxGroup group=new CheckboxGroup();
}
class JCheckBoxPanel extends JPanel {
int n;
Listener listener = new Listener();
JApplet applet;
public JCheckBoxPanel(JApplet applet) {
this.applet = applet;
CheckboxGroup group=new CheckboxGroup();
Checkbox jcb1=new Checkbox("1",group,false);
Checkbox jcb2=new Checkbox("3",group,false);
Checkbox jcb3=new Checkbox("5",group,false);
Checkbox jcb4=new Checkbox("7",group,false);
Checkbox jcb5=new Checkbox("9",group,false);
jcb1.addItemListener (listener);
jcb2.addItemListener(listener);
jcb3.addItemListener (listener);
jcb4.addItemListener(listener);
jcb5.addItemListener(listener);
add(jcb1);
add(jcb2);
add(jcb3);
add(jcb4);
add(jcb5);
n=Integer.parseInt(group.getSelectedCheckbox().getLabel());
}
class Listener implements ItemListener {
public void itemStateChanged(ItemEvent event) {
repaint();
}
}
}
我自己测试时n=Integer.parseInt(group.getSelectedCheckbox().getLabel());这句的问题,但是怎么改呢?