Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 一个计算圆的面积的程序,Double转换老搞不定,大家帮忙看看!
yzhxhwt





发贴: 27
积分: 0
于 2006-02-01 01:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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)


why edited on 2006-02-01 08:42


我有一点感想和初学者们同享(原创)

话题树型展开
人气 标题 作者 字数 发贴时间
6711 一个计算圆的面积的程序,Double转换老搞不定,大家帮忙看看! yzhxhwt 5597 2006-02-01 01:59
5317 Re:一个计算圆的面积的程序,Double转换老搞不定,大家帮忙看看! why 220 2006-02-01 08:49
5618 Re:一个计算圆的面积的程序,Double转换老搞不定,大家帮忙看看! yzhxhwt 51 2006-02-01 22:45
5722 Re:一个计算圆的面积的程序,Double转换老搞不定,大家帮忙看看! yzhxhwt 410 2006-02-01 22:52
5478 Re:一个计算圆的面积的程序,Double转换老搞不定,大家帮忙看看! why 516 2006-02-02 21:17

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923