java吧 关注:1,249,850贴子:12,732,142
  • 17回复贴,共1

新人求教,刚刚学习了Swing容器,各种不懂

只看楼主收藏回复

这几天学习了Swing,课后还有作业。要求设计一个计算器,毫无头绪求高手赐教
还有提示




1楼2013-05-13 21:45回复

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class JiSuanQi {
    public static void main(String[] args) {
    JieMian j = new JieMian();
    j.getJieMian();
    }
    }
    class JieMian{
    public static StringBuffer sb = new StringBuffer();//定义全局变量,存储要输入的数据
    public static int flag ;
    public static int flag2 ;
    public static double y=-1;
    public static double x=-1;
    public static int sum;//如果没有按数字,直接按了符号键会抛出很多异常,所以添加一个标记用来记录键入数字的个数,只有在个数大于零的时候,各个符号键才运行。
    public JTextField text=new JTextField();
    public void getJieMian(){
    JFrame frame = new JFrame("计算器") ;
    frame.setResizable(false);//关键代码:设置窗体能否被任意拖动大小。
    frame.setVisible(true);
    frame.setSize(500,300);
    frame.setLocation(300, 300);
    frame.setLayout(null);//这一句是非常关键的,如果不加这一句,组件就会充满整个窗口!!!
    text.setVisible(true);
    text.setBackground(Color.lightGray);
    text.setBounds(0,230,300,30);
    JPanel p1 = new JPanel();
    p1.setBounds(0, 0,200,300);
    p1.setSize(300,180);
    p1.setVisible(true);
    p1.setLayout(new GridLayout(4,3,3,3));
    final JButton but1 = new JButton("1");
    p1.add(but1);
    final JButton but2 = new JButton("2");
    p1.add(but2);
    final JButton but3 = new JButton("3");
    p1.add(but3);
    final JButton but4 = new JButton("4");
    p1.add(but4);
    final JButton but5 = new JButton("5");
    p1.add(but5);
    final JButton but6 = new JButton("6");
    p1.add(but6);
    final JButton but7 = new JButton("7");
    p1.add(but7);
    final JButton but8 = new JButton("8");
    p1.add(but8);
    final JButton but9 = new JButton("9");
    p1.add(but9);
    final JButton zero = new JButton("0");
    final JButton dot = new JButton(".");
    final JButton tui = new JButton("归零");
    p1.add(zero);
    p1.add(dot);
    p1.add(tui);
    JPanel p2 = new JPanel();
    p2.setSize(300,50);
    p2.setVisible(true);
    p2.setBounds(0,180, 300, 50);
    p2.setLayout(new GridLayout(1,5,3,3));
    final JButton jia = new JButton("+");
    final JButton jian = new JButton("-");
    final JButton cheng = new JButton("×");
    final JButton chu = new JButton("/");
    final JButton deng = new JButton("=");
    p2.add(jia);
    p2.add(jian);
    p2.add(cheng);
    p2.add(chu);
    p2.add(deng);
    frame.add(p1);
    frame.add(p2);
    frame.add(text);
    //下面就是运算部分的代码,进行监听器的添加和各个按钮具体功能的实现。
    but1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("1");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    but2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("2");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    but3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
    sb = sb.append("3");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    but4.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("4");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    but5.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("5");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    but6.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("6");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    but7.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("7");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    but8.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("8");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    but9.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("9");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    zero.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sb = sb.append("0");
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    });
    dot.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if ((e.getSource()==dot)&&(flag2!=7)&&(sb.length()>0)){
    sb = sb.append(".");
    flag2 = 7 ;
    sum++;
    x = Double.valueOf(sb.toString());
    text.setText(""+sb);
    }
    }
    });
    tui.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if(sb!=null){
    sb = sb.delete(0, sb.length());
    x = 0;
    y = 0;
    flag = 5;
    flag2 = 6 ;
    }
    text.setText("0");
    }
    });
    jia.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    while (sum!=0){
    y= Double.valueOf(sb.toString());
    x= y;
    flag = 1 ;
    sb.delete(0, sb.length());
    text.setText("+");
    sum=0;
    }
    }
    });
    jian.addActionListener (new ActionListener() {
    public void actionPerformed (ActionEvent e) {
    while(sum!=0){
    x= Double.valueOf(sb.toString());
    y= x;
    flag= 2 ;
    sb.delete(0, sb.length());
    sum=0;
    text.setText("-");
    }
    }
    });
    cheng.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    while(sum!=0){
    x= Double.valueOf(sb.toString());
    y= x;
    flag = 3 ;
    sb.delete(0, sb.length());
    sum = 0 ;
    text.setText("x");
    }
    }
    });
    chu.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    while (sum!=0){
    x= Double.valueOf(sb.toString());
    y= x;
    flag = 4 ;
    sb.delete(0, sb.length());
    sum = 0 ;
    text.setText("/");
    };
    }
    });
    deng.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    while(sum != 0){
    if(sb.length()!=1)
    x= Double.valueOf((sb.toString()));
    sb.delete(0, sb.length());
    sum = 0 ;
    if(y<0)text.setText(""+x);
    if(flag==1)
    text.setText(""+(y+x));
    if(flag==2)
    text.setText(""+(y-x));
    if(flag==3)
    text.setText(""+(y*x));
    if(flag==4){
    if(x==0)
    text.setText("被除数不能为零");
    else text.setText(" "+(y/x));
    }
    flag = 6 ;
    flag2 = 6 ;
    }
    }
    });
    }
    }


    2楼2013-05-13 21:56
    回复
      这是我之前写的计算器,界面很粗糙,代码很凌乱
      用起来有种怪怪的感觉,如果你实在写不出代码,可以拿去凑合应付一下。
      记事本啥的我就无能为力了。文本域的操作我没经验,也没时间学。计算器,我是用swing写的,不是awt。


      3楼2013-05-13 21:58
      收起回复
        俺的代码跟你一楼的比起来简直就是渣滓,但这时候俺辛辛苦苦纯靠自己的脑子想出来的代码,没依靠任何人的帮助。花了两天呢。
        事件监听的添加实在没有好办法,只能一个个添加。看到你的代码,俺顿感豁然开朗,多谢指教!


        4楼2013-05-13 22:03
        收起回复
          想知道怎么进行运算的,判断的,搞清楚这点应该好办了,以前写一半没弄出来


          IP属地:湖北5楼2013-05-13 22:37
          回复
            记事本我也写过了、实现了修改字体的各种状态!


            本楼含有高级字体7楼2013-05-14 08:34
            收起回复