小写了一个计算器的程序,原本想实现上面输入数字,下面一排是运算符的,但自己对布局管理器理解的不是很好,哪位朋友可以帮下忙,稍微设计的漂亮些~ 代码如下:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class C_count 
{
public static void main(String args[])
{
JFrame app = new JFrame("计算器程序");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(640,200);
Container c = app.getContentPane();
c.setLayout(new FlowLayout());

final JTextField[] t = {new JTextField(4),new JTextField(2),new JTextField(4),new JTextField(4)};
for(int i = 0; i < 4; i++)
   c.add(t[i]);

final JButton[] b = {new JButton("+"),new JButton("-"),new JButton("*"),new JButton("/"),new JButton("=")};
for(int j = 0; j < 5; j++)
   c.add(b[j]);

//设置获取运算符号
b[0].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t[1].setText("+");
}
}
);
b[1].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t[1].setText("-");
}
}
);
b[2].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t[1].setText("*");
}
}
);
b[3].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t[1].setText("/");
}
}
);
b[4].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
  String s1 = t[1].getText();
  char s = s1.charAt(0);
  String total;
  int result; 
  String s0 = t[0].getText();
  int a = Integer.parseInt(s0);
  String s2 = t[2].getText();
  int b = Integer.parseInt(s2);
  
  
switch(s)
{
case '+':
  result = a + b;
  total = Integer.toString(result);
    t[3].setText(total);
  break;
case '-':
  result = a - b;
  total = Integer.toString(result);
    t[3].setText(total);
  break;
case '*':
  result = a * b;
  total = Integer.toString(result);
    t[3].setText(total);
  break;
case '/':
  result = a / b;
  total = Integer.toString(result);
    t[3].setText(total);
  break;
default:
  System.out.println("There is an error");
  break;
}
}
}
);

app.setVisible(true);
}
}//谢谢~

解决方案 »

  1.   

    建议使用null布局,很简单,看下书一下就搞定
      

  2.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class C_count {
    public static void main(String args[]) {
    JFrame app = new JFrame("計算器");
    app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    app.setSize(640, 200);
    Container c = app.getContentPane();
    c.setLayout(new BorderLayout());

    JPanel jp1 = new JPanel();
    jp1.setLayout(new FlowLayout()); final JTextField[] t = { new JTextField(4), new JTextField(2),
    new JTextField(4), new JTextField(4) };
    for (int i = 0; i < 4; i++)
    jp1.add(t[i]);

    c.add(jp1,BorderLayout.NORTH); JPanel jp2 = new JPanel();
    jp2.setLayout(new FlowLayout());

    final JButton[] b = { new JButton("+"), new JButton("-"),
    new JButton("*"), new JButton("/"), new JButton("=") };
    for (int j = 0; j < 5; j++)
    jp2.add(b[j]);
    c.add(jp2,BorderLayout.CENTER);

    b[0].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    t[1].setText("+");
    }
    });
    b[1].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    t[1].setText("-");
    }
    });
    b[2].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    t[1].setText("*");
    }
    });
    b[3].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    t[1].setText("/");
    }
    });
    b[4].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String s1 = t[1].getText();
    char s = s1.charAt(0);
    String total;
    int result;
    String s0 = t[0].getText();
    int a = Integer.parseInt(s0);
    String s2 = t[2].getText();
    int b = Integer.parseInt(s2); switch (s) {
    case '+':
    result = a + b;
    total = Integer.toString(result);
    t[3].setText(total);
    break;
    case '-':
    result = a - b;
    total = Integer.toString(result);
    t[3].setText(total);
    break;
    case '*':
    result = a * b;
    total = Integer.toString(result);
    t[3].setText(total);
    break;
    case '/':
    result = a / b;
    total = Integer.toString(result);
    t[3].setText(total);
    break;
    default:
    System.out.println("There is an error");
    break;
    }
    }
    }); app.setVisible(true);
    }
    }