//看楼主的看我的眼花..一个简单的哪要那么多个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);
}
}