public class P99 extends JFrame{
private int n,m,j;
private JLabel label, label2;
List<JLabel> labelList=new ArrayList<JLabel>();
List<JLabel> labelList2=new ArrayList<JLabel>();
private final int LW=25;
private final int L2W=40;
private final int LH=30;
public JLabel getLabel(int num1,int num2,int x,int y){
label=new JLabel(num2+"*"+num1);
label.setOpaque(true);
label.setBackground(Color.PINK);
label.setBounds(x, y, LW,LH );
return label;
}
public JLabel getLabel2(int x,int y){
label2=new JLabel(" ");
label2.setOpaque(true);
label2.setBackground(Color.PINK);
label2.setBounds(x, y,L2W,LH );
return label2;
}
public P99(String name){
super(name);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(300, 300, 600, 400);
//Container c=this.getContentPane();
JPanel panel=new JPanel();
panel.setLayout(null);
int y=70;
for( n=1;n<10;n++){
int x=0;
for( m=1;m<=n;m++){
System.out.print(x+" "+y+" 销则桐");
亏坦 JLabel l=getLabel(n, m, x, y);
JLabel l2=getLabel2(x+LW, y);
l.addMouseListener(new MouseListener() {
盯洞 boolean isClick=false;
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(isClick){
isClick=false;
l2.setText("");
}else{
isClick=true;
String str=l.getText().toString();
int a=Integer.parseInt(str.substring(0, 1));
int b=Integer.parseInt(str.substring(2, 3));
int sum=a*b;
l2.setText("="+String.valueOf(sum));
}
}
});
labelList.add(l);
labelList2.add(l2);
x=x+ LW+L2W;
}
System.out.println(" ");
y=y+LH;
}
for(int j=0;j<labelList.size();j++){
panel.add(labelList.get(j));
panel.add(labelList2.get(j));
}
this.add(panel);
myEvent();
//c.add(panel);
//c.add(getLabel("1", "2", 0, 0));
this.setResizable(false);
this.setVisible(true);
}
public void myEvent(){
}
public static void main(String[] args){
new P99("九九乘法表");
}
}
运行结果:
(打印算法就在程序里)
点击它就会有结果: