RT,在附件里是原题目,大家帮忙看一下!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Calc extends JFrame
{
public Calc()
{
this.setDefaultCloseOperation(3);
setLayout(new BorderLayout());
Container contentPane=getContentPane();
NorthPanel np=new NorthPanel();
contentPane.add(np,BorderLayout.NORTH);
CenterPanel cp=new CenterPanel();
contentPane.add(cp,BorderLayout.CENTER);
}
public static void main(String[] args)
{
Calc calc=new Calc();
calc.setSize(300,200);
calc.setVisible(true);
}
}
class NorthPanel extends JPanel
{
JRadioButton btn1,btn2;
public NorthPanel()
{
btn1=new JRadioButton("面积");
btn2=new JRadioButton("周长");
ButtonGroup bg=new ButtonGroup();
bg.add(btn1);
bg.add(btn2);
add(btn1);
add(btn2);
}
}
class CenterPanel extends JPanel
{
JLabel lbl1,lbl2;
JTextField jf;
JButton btn;
NorthPanel np=new NorthPanel();
public CenterPanel()
{
setLayout(new GridLayout(2,2));
lbl1=new JLabel("半径");
add(lbl1);
//lbl2=new JLabel();
jf=new JTextField(4);
String s=jf.getText();
// try
// {
final double d=Double.parseDouble(s);
// }
// catch (Exception ex)
// {
// ex.printStackTrace();
// }
jf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(jf.getText().length()>3)
{
JOptionPane.showMessageDialog(null,"输入的位数不能大于4位数","输入错误",2);
jf.setText("");
}
}
public void keyReleased(KeyEvent e)
{
if(!((e.getKeyChar()>='1') && (e.getKeyChar()<='9')))
{
JOptionPane.showMessageDialog(null,"只能输入数字","输入错误",2);
jf.setText("");
}
}
});
add(jf);
lbl2=new JLabel("这边显示计算结果");
btn=new JButton("计算");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(np.btn1.isSelected())
{
try
{
lbl2.setText("结果为:"+3.14*3.14*d);
}
catch(NumberFormatException a)
{
a.printStackTrace();
}
}
else if(np.btn2.isSelected())
{
try
{
lbl2.setText("结果为"+3.14*2.0*d);
}
catch (NumberFormatException ex)
{
ex.printStackTrace();
}
}
}
});
add(btn);
add(lbl2);
}
}
题目.rar (365.41k)