GUI问题(计算器)
我把此题分为三个类来处理的,不知为何编译就是不能够通过(在下面已标出),不知哪位高手可否指导一下?!程序代码:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class InputPanel extends JPanel { public JTextField tf1, tf2, tfResult; private JLabel label1, label2, label3; public void init() { setBackground(Color.CYAN); setLayout(new GridLayout(3,1,5,8)); label1 = new JLabel("操作数1"); label2 = new JLabel("操作数2"); label3 = new JLabel("结果"); tf1 = new JTextField(); tf2 = new JTextField(); tfResult = new JTextField(); add(label1); add(tf1); add(label2); add(tf2); add(label3); add(tfResult); } public JTextField getTf1() { return tf1; } public JTextField getTf2() { return tf2; } public JTextField getTfResult() { return tfResult; } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalculatorPanel extends JPanel { private JRadioButton rdbPlus, rdbMinus, rdbMulti, rdbDivide; private ButtonGroup btg; private JButton bt1, bt2; private InputPanel iPanel; double n1, n2, n3; public void init() { setLayout(new GridLayout(1,5,5,8)); rdbPlus = new JRadioButton("加"); rdbMinus = new JRadioButton("减"); rdbMulti = new JRadioButton("乘"); rdbDivide = new JRadioButton("除"); bt1 = new JButton("运算"); bt2 = new JButton("重置"); btg = new ButtonGroup(); btg.add(rdbPlus); btg.add(rdbMinus); btg.add(rdbMulti); btg.add(rdbDivide); add(rdbPlus); add(rdbMinus); add(rdbMulti); add(rdbDivide); add(bt1); add(bt2); } public JRadioButton getRdbPlus() { return rdbPlus; } public JRadioButton getRdbMinus() { return rdbMinus; } public JRadioButton getRdbMulti() { return rdbMulti; } public JRadioButton getRdbDivide() { return rdbDivide; } public JButton getBt1() { return bt1; } public JButton getBt2() { return bt2; } } import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CalculatorDemo extends JFrame implements ActionListener { InputPanel iPanel ; CalculatorPanel cPanel; CalculatorDemo() { super("计算器"); setSize(400,300); setLocation(300,200); iPanel = new InputPanel(); cPanel = new CalculatorPanel(); cPanel.getRdbPlus().addActionListener(this); // 此处不能通过编译 cPanel.getRdbMinus().addActionListener(this); cPanel.getRdbMulti().addActionListener(this); cPanel.getRdbDivide().addActionListener(this); cPanel.getBt1().addActionListener(this); cPanel.getBt2().addActionListener(this); this.getContentPane().setLayout(new BorderLayout(0,80)); this.getContentPane().add(iPanel,BorderLayout.CENTER); this.getContentPane().add(cPanel,BorderLayout.SOUTH); iPanel.init(); cPanel.init(); } public static void main(String[] args){ CalculatorDemo frame = new CalculatorDemo(); // 还有此处不能通过编译 frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { double n1 = 0.0; double n2 = 0.0; double n3 = 0.0; if(e.getSource() instanceof JRadioButton) { if(e.getActionCommand().equals("加")) { n1 = Double.parseDouble(iPanel.getTf1().getText()); n2 = Double.parseDouble(iPanel.getTf2().getText()); } if(e.getActionCommand().equals("减")) { n1 = Double.parseDouble(iPanel.getTf1().getText()); n2 = Double.parseDouble(iPanel.getTf2().getText()); } if(e.getActionCommand().equals("乘")) { n1 = Double.parseDouble(iPanel.getTf1().getText()); n2 = Double.parseDouble(iPanel.getTf2().getText()); } if(e.getActionCommand().equals("除")) { n1 = Double.parseDouble(iPanel.getTf1().getText()); n2 = Double.parseDouble(iPanel.getTf2().getText()); } } if(e.getSource() == cPanel.getBt1()) { n3 = n1 + n2; iPanel.getTfResult().setText("" + n3); } if(e.getSource() == cPanel.getBt1()) { n3 = n1 - n2; iPanel.getTfResult().setText("" + n3); } if(e.getSource() == cPanel.getBt1()) { n3 = n1 * n2; iPanel.getTfResult().setText("" + n3); } if(e.getSource() == cPanel.getBt1()) { n3 = n1 / n2; iPanel.getTfResult().setText("" + n3); } if(e.getSource() == cPanel.getBt2()) { iPanel.getTf1().setText(null); iPanel.getTf2().setText(null); iPanel.getTfResult().setText(null); iPanel.getTf1().grabFocus(); } } }
[ 本帖最后由 bondy 于 2010-11-19 22:06 编辑 ]