问题描述:
有一些Button,我点击一个数字Button就将它上面的值传入到文本框中,我通过tx.getText()得到了此值,为什么在文 本框中不显示值
代码如下:
- Java code
package com.zhyx.swing;public class TestCalculator extends JFrame { /** * TestCalculator初始化方法 */ public TextField t1; public TestCalculator() { setLayout(new BorderLayout()); JPanel p1=new JPanel(); t1=new TextField(); JPanel p2=new JPanel(); p2.setLayout(new GridLayout(4,4)); String []text={"7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+","="}; Button btn[] = new Button[17]; int i; for(i=0;i<btn.length;i++){ btn[i]=new Button(text[i]); p2.add(btn[i]); btn[i].addActionListener(new Listener()); } btn[0].setSize(20,30); p1.setSize(300,10); p1.setBackground(Color.gray); p2.setSize(500,30); t1.setBackground(Color.RED); t1.setText("0."); add(p1,BorderLayout.NORTH); add(t1,BorderLayout.CENTER); add(p2,BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } /** * @param args */ public static void main(String[] args) { TestCalculator test=new TestCalculator(); }} class Listener implements ActionListener{ public Listener(){ } public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); TextField t1=new TextField(); if(obj instanceof Button){ //如果按钮里的String内容为int型,则返回到text上 try{ int a=Integer.parseInt(((Button) obj).getLabel()); t1.setText(""+a); t1.getText(); if(".".equals(((Button) obj).getLabel())){ t1.setText(""+a); t1.getText(); } }catch(Exception e1){ System.out.println("ddd"); } } //每一次点击完按钮后Text上的内容要清空// t1.setText(""); } }
------解决方案--------------------
对你的回复
第一,代码修改的地方比较多的时候,就无法给你标注,CSDN遇到这种情况太多。
既然你诚心在CSDN提问,那么就不会懒到让别人给你把一切弄好,从学习的角度,你自己找别人代码跟 你代码的差别在那里其实是好事,对程序理解会更深。
第二,使用内部类监听是最为方便的方法,如果要在外部调用监听,那么就额外需要传递你要监听的对象。
给你贴出来看看这样实现的代码
- Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;public class TestCalculator2 extends JFrame { public TextField getT1() { // modified return t1; } /** * TestCalculator初始化方法 */ private TextField t1; public TestCalculator2() { setLayout(new BorderLayout()); JPanel p1=new JPanel(); t1=new TextField(); JPanel p2=new JPanel(); p2.setLayout(new GridLayout(4,4)); String []text={"7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+","="}; Button btn[] = new Button[17]; int i; for(i=0;i<btn.length;i++){ btn[i]=new Button(text[i]); p2.add(btn[i]); btn[i].addActionListener(new Listener(this)); // modified } btn[0].setSize(20,30); p1.setSize(300,10); p1.setBackground(Color.gray); p2.setSize(500,30); t1.setBackground(Color.RED); t1.setText("0."); add(p1,BorderLayout.NORTH); add(t1,BorderLayout.CENTER); add(p2,BorderLayout.SOUTH); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } /** * @param args */ public static void main(String[] args) { TestCalculator2 test=new TestCalculator2(); }} class Listener implements ActionListener{ private TextField t1=null; // modified public Listener(TestCalculator2 cal){ // modified t1=cal.getT1(); } public void actionPerformed(ActionEvent e) { Object obj = e.getSource();// TextField t1=new TextField(); if(obj instanceof Button){ //如果按钮里的String内容为int型,则返回到text上 try{ int a=Integer.parseInt(((Button) obj).getLabel()); t1.setText(""+a); t1.getText(); if(".".equals(((Button) obj).getLabel())){ t1.setText(""+a); t1.getText(); } }catch(Exception e1){ System.out.println("ddd"); } } //每一次点击完按钮后Text上的内容要清空// t1.setText(""); } }