import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
public class frame extends JFrame
{
JLabel lable1;
JTextArea text2;
JTextField text1;
JButton button;
/**
*
*/
private static final long serialVersionUID = 1L;
public void shapeSearch ( boolean b )
{
this.setTitle ("图形属性查询器");
this.setSize (400, 250);
this.setResizable (b);
this.setVisible (true);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public void imputDemo ()
{
JLabel lable1 = new JLabel ("图形名称:");
text2 = new JTextArea ();
text2.setText (null);
text2.setLineWrap (true);
text2.setBounds (20, 20, 300, 200);
text1 = new JTextField (10);
JButton button = new JButton ("查询");
JScrollPane scroll = new JScrollPane (text2);
FlowLayout seeText = new FlowLayout (FlowLayout.CENTER);
seeText.setHgap (20);
seeText.setVgap (60);
this.setLayout (seeText);
this.add (lable1);
this.add (text1);
this.add (button);
this.add (scroll);
ButtonHandle handle = new ButtonHandle ();
button.addActionListener (handle);
DocumentWriter dm = new DocumentWriter ();
Document doc = text1.getDocument ();
doc.addDocumentListener (dm);
this.setVisible (true);
}
class ButtonHandle implements ActionListener
{
public void actionPerformed ( ActionEvent event )
{
if (event.getActionCommand ().equals ("查询"))
{
if ("圆形".equals (text1.getText ()))
{
text2.setText ("该圆形的半径是3\n" + "周长是18.84\n" + "面积是28.26");
}
else if ("矩形".equals (text1.getText ()))
{
text2.setText ("该矩形的长是5,宽是4\n" + "周长是18\n" + "面积是20");
text2.setVisible (true);
}
else if ("三角形".equals (text1.getText ()))
{
text2.setText ("该三角形的底边是3,高是4,斜边是5\n" + "周长是12\n" + "面积是6");
text2.setVisible (true);
}
}
}
}
class DocumentWriter implements DocumentListener
{
public void changedUpdate ( DocumentEvent e )
{}
public void insertUpdate ( DocumentEvent e )
{}
public void removeUpdate ( DocumentEvent e )
{}
}
public static void main ( String[] args ) throws Exception
{
frame littleFrame = new frame ();
littleFrame.shapeSearch (false);
littleFrame.imputDemo ();
}
}