| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 699 人关注过本帖
标题:GUI问题(计算器)
只看楼主 加入收藏
bondy
Rank: 2
来 自:南京
等 级:论坛游民
帖 子:10
专家分:12
注 册:2010-10-30
收藏
 问题点数:0 回复次数:1 
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 编辑 ]
搜索更多相关主题的帖子: 计算器 GUI 
2010-11-19 21:52
bondy
Rank: 2
来 自:南京
等 级:论坛游民
帖 子:10
专家分:12
注 册:2010-10-30
收藏
得分:0 
程序代码:
/*  InputPanel类  */

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

public class InputPanel extends JPanel {
    private 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;
    }

}

/* CalculatorPanel类 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CalculatorPanel extends JPanel {    
    private JRadioButton rdbPlus, rdbMinus, rdbMulti, rdbDivide;
    private ButtonGroup btg;
    public JButton bt1, bt2;    
    private InputPanel iPanel;
   
    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("运算");
        bt1.setBackground(new Color(0,255,0));
        bt2 = new JButton("重置");
        bt2.setBackground(new Color(0,255,0));

        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;
    } 

}

 

/*  CalculatorDemo类  */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import *;

public class CalculatorDemo extends JFrame implements ActionListener {  
    InputPanel iPanel ;
    CalculatorPanel cPanel;   
    private int symbol;
    double n1 = 0.0;
    double n2 = 0.0;
    double n3 = 0.0;   
   
    CalculatorDemo() {       
        super("计算器");
        setSize(400,300);       
        setLocation(300,200);        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
       
        iPanel = new InputPanel();   
        cPanel = new CalculatorPanel();   
        iPanel.init();
        cPanel.init();       
        //this.getContentPane().setLayout(new BorderLayout(0,80));   
        setLayout(new BorderLayout(0,80));
        add(iPanel,BorderLayout.CENTER);
        add(cPanel,BorderLayout.SOUTH);   
       
        cPanel.getRdbPlus().addActionListener(this);
        cPanel.getRdbMinus().addActionListener(this);
        cPanel.getRdbMulti().addActionListener(this);
        cPanel.getRdbDivide().addActionListener(this);       
        cPanel.getBt1().addActionListener(this);
        cPanel.getBt2().addActionListener(this);       
    } 

    public void actionPerformed(ActionEvent e) {       
        if(e.getSource() instanceof JRadioButton) {
            n1 = Double.parseDouble(iPanel.getTf1().getText());
            n2 = Double.parseDouble(iPanel.getTf2().getText());   
            if(e.getActionCommand().equals("加")) {   
                symbol = 1;                    
            }       
            if(e.getActionCommand().equals("减")) {   
                symbol = 2;                    
            }           
            if(e.getActionCommand().equals("乘")) {   
                symbol = 3;                   
            }           
            if(e.getActionCommand().equals("除")) {   
                symbol = 4;                            
            }   
        }   

        if(e.getSource() == cPanel.getBt1()) {
            switch(symbol)
            {
            case 1:             
                n3 = n1 + n2;                                        
                break;
            case 2:            
                n3 = n1 - n2;                                 
                break;
            case 3:            
                n3 = n1 * n2;                                          
                break;
            case 4:       
                try {
                    n3 = n1 / n2;
                } catch(ArithmeticException ae) {
                    ae.printStackTrace();                                         
                } finally {   
                    System.out.println("divide by 0,please input again!");                    
                }
                break;               
            }   
            iPanel.getTfResult().setText(String.valueOf(n3));
        }       
       
        if(e.getSource() == cPanel.getBt2()) {
           iPanel.getTf1().setText(null);
           iPanel.getTf2().setText(null);
           iPanel.getTfResult().setText(null); 
           cPanel.getRdbPlus().setSelected(false);
           cPanel.getRdbMinus().setSelected(false);
           cPanel.getRdbMulti().setSelected(false);
           cPanel.getRdbDivide().setSelected(false);
        }   
    }
    
    public static void main(String[] args){
        new CalculatorDemo().setVisible(true);    
    }
   
}


2010-11-24 22:22
快速回复:GUI问题(计算器)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.079322 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved