求一个计算周长和面积的java程序
1个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class a
{
public static void main(String []args)
{
b a=new b();
}
}
class b extends JFrame
{
private JLabel x,y,z,a;
private JTextField x1,y1,z1,a1;
private JButton bo,bo2;
public b()
{
x=new JLabel("边长: ",SwingConstants.RIGHT);
y=new JLabel("宽度: ",SwingConstants.RIGHT);
z=new JLabel("周长: ",SwingConstants.RIGHT);
a=new JLabel("面积: ",SwingConstants.RIGHT);
x1=new JTextField(10);
y1=new JTextField(10);
z1=new JTextField(10);
a1=new JTextField(10);
bo=new JButton("开始计算");
bo2=new JButton("退出程式");
setTitle("Area and Primeter of Renctangle-中软科技");
Container pane=getContentPane();
pane.setLayout(new GridLayout(5,2));
pane.add(x);
pane.add(x1);
pane.add(y);
pane.add(y1);
pane.add(z);
pane.add(z1);
pane.add(a);
pane.add(a1);
pane.add(bo);
pane.add(bo2);
setSize(400,300);
bo.addActionListener(new boHand());
bo2.addActionListener(new bo2Hand());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class boHand implements ActionListener
{
private double length,width,area,primeter;
public void actionPerformed(ActionEvent e)
{
length=Double.parseDouble(x1.getText());
width=Double.parseDouble(y1.getText());
area=length*width;
primeter=2*(width+length);
z1.setText(""+primeter+"cm");
a1.setText(""+area+"cm*cm");
}
}
private class bo2Hand implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
import java.awt.*;
import java.awt.event.*;
public class a
{
public static void main(String []args)
{
b a=new b();
}
}
class b extends JFrame
{
private JLabel x,y,z,a;
private JTextField x1,y1,z1,a1;
private JButton bo,bo2;
public b()
{
x=new JLabel("边长: ",SwingConstants.RIGHT);
y=new JLabel("宽度: ",SwingConstants.RIGHT);
z=new JLabel("周长: ",SwingConstants.RIGHT);
a=new JLabel("面积: ",SwingConstants.RIGHT);
x1=new JTextField(10);
y1=new JTextField(10);
z1=new JTextField(10);
a1=new JTextField(10);
bo=new JButton("开始计算");
bo2=new JButton("退出程式");
setTitle("Area and Primeter of Renctangle-中软科技");
Container pane=getContentPane();
pane.setLayout(new GridLayout(5,2));
pane.add(x);
pane.add(x1);
pane.add(y);
pane.add(y1);
pane.add(z);
pane.add(z1);
pane.add(a);
pane.add(a1);
pane.add(bo);
pane.add(bo2);
setSize(400,300);
bo.addActionListener(new boHand());
bo2.addActionListener(new bo2Hand());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class boHand implements ActionListener
{
private double length,width,area,primeter;
public void actionPerformed(ActionEvent e)
{
length=Double.parseDouble(x1.getText());
width=Double.parseDouble(y1.getText());
area=length*width;
primeter=2*(width+length);
z1.setText(""+primeter+"cm");
a1.setText(""+area+"cm*cm");
}
}
private class bo2Hand implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询