Topic: 帮忙找找错误在哪里(Swing:点击某个键后却毫无反应)

  Print this page

1.帮忙找找错误在哪里(Swing:点击某个键后却毫无反应) Copy to clipboard
Posted by: weoln
Posted on: 2005-11-19 15:40

这是一个模拟简单计算器的界面,我想在点击某个键的时候,就在文本框中显示该键的数值.但是我编辑的程序在点击某个键后却毫无反应,不知道是为什么.请各位高手帮忙看看,非常感谢!

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

class CalculatorFrame
{
  Frame calculatorFrame;

  Panel top;
  Panel center;
  Panel main;

  TextField txt;

  Button B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16;
  
public CalculatorFrame(String Title,int AppletWidth,int AppletHeight,Applet Calcu)
  {
   calculatorFrame=new Frame(Title);
   top=new Panel();
   center=new Panel();
   main=new Panel();
txt=new TextField(10);

//窗口设计
center.setLayout(new GridLayout(5,4));
main.setLayout(new BorderLayout());

   main.add(top,BorderLayout.NORTH);
   main.add(center,BorderLayout.CENTER);

   top.add(txt);
   center.add(B1=new Button("7"));
   center.add(B2=new Button("8"));
   center.add(B3=new Button("9"));
   center.add(B4=new Button("/"));
   center.add(B5=new Button("4"));
   center.add(B6=new Button("5"));
   center.add(B7=new Button("6"));
   center.add(B8=new Button("*"));
   center.add(B9=new Button("1"));
   center.add(B10=new Button("2"));
   center.add(B11=new Button("3"));
   center.add(B12=new Button("-"));
   center.add(B13=new Button("0"));
   center.add(B14=new Button("."));
   center.add(B15=new Button("Enter"));
   center.add(B16=new Button("+"));

center.add(Calcu,"main");

calculatorFrame.add(main,BorderLayout.CENTER);
   calculatorFrame.setResizable(false);
   calculatorFrame.setSize(AppletWidth,AppletHeight+100);
   calculatorFrame.show();

   //事件处理
   //===============实现接口===================

class GetNum implements ActionListener
   {
     public void actionPerformed(ActionEvent e)
     {
     String name =
        ((JButton)e.getSource()).getText();
      txt.setText(name);
     }
};

   GetNum getnum=new GetNum();

   //===============注册======================
   B1.addActionListener(getnum);
   B2.addActionListener(getnum);
   B3.addActionListener(getnum);
   B4.addActionListener(getnum);
   B5.addActionListener(getnum);
   B6.addActionListener(getnum);
   B7.addActionListener(getnum);
   B8.addActionListener(getnum);
   B9.addActionListener(getnum);
   B10.addActionListener(getnum);
   B11.addActionListener(getnum);
   B12.addActionListener(getnum);
   B13.addActionListener(getnum);
   B14.addActionListener(getnum);
   B15.addActionListener(getnum);
   B16.addActionListener(getnum);


  

  
  }

};

public class Calculator extends Applet
{
CalculatorFrame Cal = new CalculatorFrame("calculator",400,400,this);
};

2.Re:帮忙找找错误在哪里 [Re: weoln] Copy to clipboard
Posted by: anatoranato
Posted on: 2005-11-19 23:09

这是执行时发生的错误的LOG:
Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException: java.awt.Button
  at CalculatorFrame$1GetNum.actionPerformed(Calculator.java:65)
  at java.awt.Button.processActionEvent(Unknown Source)
  at java.awt.Button.processEvent(Unknown Source)
  at java.awt.Component.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.EventQueue.dispatchEvent(Unknown Source)
  at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.run(Unknown Source)
你添的那几个按钮不是JButton,是,Button,所以在类型转换时出错了,知道怎么改了吧.

3.Re:帮忙找找错误在哪里 [Re: anatoranato] Copy to clipboard
Posted by: weoln
Posted on: 2005-11-20 10:46

谢谢楼上的!

4.Re:帮忙找找错误在哪里 [Re: weoln] Copy to clipboard
Posted by: weoln
Posted on: 2006-02-16 15:31

请问JBotton和Botton有什么区别?

5.Re:帮忙找找错误在哪里(Swing:点击某个键后却毫无反应) [Re: weoln] Copy to clipboard
Posted by: 空心菜
Posted on: 2006-02-20 11:38

String name = ((Button) e.getSource()).getLabel();
JBotton是Swing的
Button是AWT的


   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