import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;public class A200681010106 extends JFrame implements ActionListener
{
public float length=0,width=0,perimeter=0,area=0,length2=0,perimeter2=0,area2=0;
JLabel[] pm;
String[] str={"长方形的长:","长方形的宽","长方形的面积","长方形的周长","正方形的长","正方形的面积","正方形的周长"};
JTextField[] txt;
JPanel p1,p2;
JButton bt;
void goGui()
{
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JButton bt=new JButton("开始计算");
p1.setLayout(new GridLayout(1,7));
p2.setLayout(new GridLayout(1,7));
for(int i=0;i<str.length;i++)
{
pm[i]=new JLabel(str[i]);
txt[i]=new JTextField(10);
p1.add(pm[i]);
p2.add(txt[i]);
}
this.setLayout(new BorderLayout());
this.add(p1,BorderLayout.CENTER);
this.add(p2,BorderLayout.EAST);
this.add(bt,BorderLayout.NORTH);
bt.addActionListener(this); 
this.setSize(260,330); 
this.setResizable(false);
this.setVisible(true); 
}
  public void actionPerformed(ActionEvent e)
{
if(bt==e.getSource())
{
length=Float.parseFloat(txt[0].getText());
width=Float.parseFloat(txt[1].getText());
Rectangle myRectangle=new Rectangle();
myRectangle.go();
txt[2].setText(String.valueOf(area));
txt[3].setText(String.valueOf(perimeter));
length2=Float.parseFloat(txt[4].getText());
Square mySquare=new Square();
mySquare.go();
txt[5].setText(String.valueOf(area2));
txt[6].setText(String.valueOf(perimeter2));
}

}
public static void main(String args[])
{
A200681010106 myValue=new A200681010106();
myValue.goGui();
}}

class Box 
{
public float length=0,width=0,perimeter=0,area=0;
public float go()
{
perimeter=2*(length+width);
area=length*width;
return area;
}
}
class Rectangle extends Box implements Printable
{

public void printItMyWay()
{
}
public void printItMyWay(char ch)
{
}
}
class Square extends Box implements Printable
{
float length2=0,perimeter2=0,area2=0;
public float go()
{
length=length2;
width=length;
perimeter2=2*(length+width);
area2=length*width;
return area2;
}
public void printItMyWay()
{
}
public void printItMyWay(char ch)
{
}

}

interface Printable
{
public void printItMyWay();
public void printItMyWay(char ch);
}

解决方案 »

  1.   

    void goGui()
        {
            JPanel p1=new JPanel();
            JPanel p2=new JPanel();
            JButton bt=new JButton("开始计算");
            p1.setLayout(new GridLayout(1,7));
            p2.setLayout(new GridLayout(1,7));
            pm=new JLabel[str.length];
            txt=new JTextField[str.length];
            //这两行加进来,不一定非要放这里,声明的时候初始化也可以
            for(int i=0;i<str.length;i++)
            {
                pm[i]=new JLabel(str[i]);
                txt[i]=new JTextField(10);
                p1.add(pm[i]);
                p2.add(txt[i]);
            }
            this.setLayout(new BorderLayout());
            this.add(p1,BorderLayout.CENTER);
            this.add(p2,BorderLayout.EAST);
            this.add(bt,BorderLayout.NORTH);
            bt.addActionListener(this); 
            this.setSize(260,330); 
            this.setResizable(false);
            this.setVisible(true); 
        }