大家帮帮忙。java程序的一个问题!

importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassCalculatorexte... import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener
{
inputPanel p1=new inputPanel();
CalculatorPanel p2=new CalculatorPanel();
public Calculator()
{

super("计算机");
setSize(400,400);
getContentPane().setLayout(new BorderLayout(10,10));
p1.init();
getContentPane().add(p1,BorderLayout.CENTER);
p2.init();
getContentPane().add(p2,BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
Calculator dd=new Calculator();

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() instanceof JButton)
{
p1.tf1.setText("按钮监听成功");
}

}

}
class inputPanel extends JPanel
{
Label L1=new Label("操作数1");
Label L2=new Label("操作数2");
Label L3=new Label("结果");
TextField tf1=new TextField();
TextField tf2=new TextField();
TextField tf3=new TextField();
public void init()
{
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
setLayout(gbl);
c.fill=GridBagConstraints.BOTH;
c.weightx=1.0;
gbl.setConstraints(L1,c);
this.add(L1);
c.gridwidth=GridBagConstraints.REMAINDER;//设置添加下一个按钮时当前行最后一个按钮
gbl.setConstraints(tf1,c);
add(tf1);
c.gridwidth=GridBagConstraints.RELATIVE;//指明当前已经添加组件的下一个组件
gbl.setConstraints(L2,c);
add(L2);
c.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(tf2,c);
add(tf2);
c.gridwidth=GridBagConstraints.RELATIVE;//指明当前已经添加组件的下一个组件
gbl.setConstraints(L3,c);
add(L3);
c.gridwidth=GridBagConstraints.REMAINDER;
gbl.setConstraints(tf3,c);
add(tf3);
this.setVisible(true);

}
}
class CalculatorPanel extends JPanel
{

JRadioButton jrb1=new JRadioButton("加");
JRadioButton jrb2=new JRadioButton("减");
JRadioButton jrb3=new JRadioButton("乘");
JRadioButton jrb4=new JRadioButton("除");
ButtonGroup btg=new ButtonGroup();

Button btn=new Button("运算");

public void init()
{
setLayout(new GridLayout(1,5));

add(jrb1);
add(jrb2);
add(jrb3);
add(jrb4);
add(btn);
setVisible(true);

btg.add(jrb1);
btg.add(jrb2);
btg.add(jrb3);
btg.add(jrb4);
btn.addActionListener(new Calculator());

}

}
去掉btn.addActionListener(new Calculator());
后程序可以运行。生成一个界面,我要实现的事单击按钮后,可以根据选择计算出数据,可是如何给别的类中的按钮注册监听器呢?????????
展开
 我来答
gdsfggdf
推荐于2016-04-08 · TA获得超过840个赞
知道小有建树答主
回答量:219
采纳率:100%
帮助的人:215万
展开全部
  //看楼主的看我的眼花..一个简单的哪要那么多个class啊.
  关于你的提问是使用匿名方法. 不需要继承关系.
  ----------------------------------------
  package 娱乐;

  import javax.swing.*;

  import java.awt.*;
  import java.awt.event.*;

  public class Calculator extends JFrame {

  CalculatorPanel p2 = new CalculatorPanel();

  public Calculator() {

  super("计算机");
  setSize(400, 400);
  p2.init();
  getContentPane().add(p2, BorderLayout.CENTER);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  setVisible(true);
  }

  public static void main(String[] args) {
  Calculator dd = new Calculator();

  }
  }

  class inputPanel extends JPanel {
  Label L1 = new Label("操作数1");

  Label L2 = new Label("操作数2");

  Label L3 = new Label("结果");

  TextField tf1 = new TextField();

  TextField tf2 = new TextField();

  TextField tf3 = new TextField();

  public void init() {
  GridBagLayout gbl = new GridBagLayout();
  GridBagConstraints c = new GridBagConstraints();
  setLayout(gbl);
  c.fill = GridBagConstraints.BOTH;
  c.weightx = 1.0;
  gbl.setConstraints(L1, c);
  this.add(L1);
  c.gridwidth = GridBagConstraints.REMAINDER;// 设置添加下一个按钮时当前行最后一个按钮
  gbl.setConstraints(tf1, c);
  add(tf1);
  c.gridwidth = GridBagConstraints.RELATIVE;// 指明当前已经添加组件的下一个组件
  gbl.setConstraints(L2, c);
  add(L2);
  c.gridwidth = GridBagConstraints.REMAINDER;
  gbl.setConstraints(tf2, c);
  add(tf2);
  c.gridwidth = GridBagConstraints.RELATIVE;// 指明当前已经添加组件的下一个组件
  gbl.setConstraints(L3, c);
  add(L3);
  c.gridwidth = GridBagConstraints.REMAINDER;
  gbl.setConstraints(tf3, c);
  add(tf3);
  this.setVisible(true);

  }
  }

  class CalculatorPanel extends inputPanel { //因为inputPanel 实现了JPanel 所以不需要继承JPanel ,需要inputPanel的数据。所以继承inputPanel

  JRadioButton jrb1 = new JRadioButton("加");

  JRadioButton jrb2 = new JRadioButton("减");

  JRadioButton jrb3 = new JRadioButton("乘");

  JRadioButton jrb4 = new JRadioButton("除");

  ButtonGroup btg = new ButtonGroup();

  Button btn = new Button("运算");

  public void init() {
  setLayout(new GridLayout(2, 1));
  JPanel jp1 = new JPanel(new GridLayout(3, 2));
  jp1.add(L1);
  jp1.add(tf1);
  jp1.add(L2);
  jp1.add(tf2);
  jp1.add(L3);
  jp1.add(tf3);
  JPanel jp2 = new JPanel(new GridLayout(1, 5));
  jp2.add(jrb1);
  jp2.add(jrb2);
  jp2.add(jrb3);
  jp2.add(jrb4);
  jp2.add(btn);
  setVisible(true);
  add(jp1);
  add(jp2);
  btg.add(jrb1);
  btg.add(jrb2);
  btg.add(jrb3);
  btg.add(jrb4);
  btn.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent event) {
  if (jrb1.isSelected()) { // 判断点中的是哪个单选按钮
  int x = Integer.parseInt(tf1.getText());
  int y = Integer.parseInt(tf2.getText());
  tf3.setText(x + y + "");
  }
  if (jrb2.isSelected()) {
  int x = Integer.parseInt(tf1.getText());
  int y = Integer.parseInt(tf2.getText());
  tf3.setText(x - y + "");
  System.out.println(2);
  }
  if (jrb3.isSelected()) {
  int x = Integer.parseInt(tf1.getText());
  int y = Integer.parseInt(tf2.getText());
  tf3.setText(x * y + "");
  System.out.println(3);
  }
  if (jrb4.isSelected()) {
  int x = Integer.parseInt(tf1.getText());
  int y = Integer.parseInt(tf2.getText());
  tf3.setText(x / y + "");
  System.out.println(4);
  }
  }
  });
  }

  }
  --------------------------------------
  ----------------------------------------
  btn.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent event) {
  //按钮点击实现的功能
  }
  });
  ----------------------------------------
  //下面是我重新给你写的

  package 娱乐;

  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;

  import javax.swing.*;

  public class Text extends JFrame {
  JTextArea tf1, tf2, tf3;

  JLabel L1, L2, L3;

  JRadioButton jrb1, jrb2, jrb3, jrb4;

  ButtonGroup btg;

  JButton btn;

  public Text() {
  Container c = getContentPane();
  JPanel jp1 = new JPanel(new GridLayout(3, 2));
  Label L1 = new Label("操作数1");
  Label L2 = new Label("操作数2");
  Label L3 = new Label("结果");
  final TextField tf1 = new TextField(10);
  final TextField tf2 = new TextField(10);
  final TextField tf3 = new TextField(10);
  jp1.add(L1);
  jp1.add(tf1);
  jp1.add(L2);
  jp1.add(tf2);
  jp1.add(L3);
  jp1.add(tf3);
  c.add(jp1, BorderLayout.NORTH);
  JPanel a = new JPanel();
  jrb1 = new JRadioButton("加");
  jrb2 = new JRadioButton("减");
  jrb3 = new JRadioButton("乘");
  jrb4 = new JRadioButton("除");
  btn = new JButton("运算");
  btg = new ButtonGroup();
  btg.add(jrb1);
  btg.add(jrb2);
  btg.add(jrb3);
  btg.add(jrb4);
  a.add(jrb1);
  a.add(jrb2);
  a.add(jrb3);
  a.add(jrb4);
  a.add(btn);
  c.add(a, BorderLayout.SOUTH);
  // ---------------------------------------------------------------
  //使用匿名的方法.
  btn.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent event) {
  if (jrb1.isSelected()) { //判断点中的是哪个单选按钮
  int x = Integer.parseInt(tf1.getText());
  int y = Integer.parseInt(tf2.getText());
  tf3.setText(x + y + "");
  }
  if (jrb2.isSelected()) {
  int x = Integer.parseInt(tf1.getText());
  int y = Integer.parseInt(tf2.getText());
  tf3.setText(x - y + "");
  }
  if (jrb3.isSelected()){
  int x = Integer.parseInt(tf1.getText());
  int y = Integer.parseInt(tf2.getText());
  tf3.setText(x * y + "");
  }
  if (jrb4.isSelected()) {
  int x = Integer.parseInt(tf1.getText());
  int y = Integer.parseInt(tf2.getText());
  tf3.setText(x / y + "");
  }

  }
  // ---------------------------------------------------------
  });
  setSize(300, 150);
  setVisible(true);
  }

  public static void main(String args[]) {
  Text s = new Text();
  s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  }
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式