public Main(){ 
super(" MyCalculator"); //名前 
resultField=new JTextField("0"); 
resultField.setEditable(false); 
resultField.setHorizontalAlignment(JTextField.RIGHT); 
s1=new JButton(" 1 "); 
s2=new JButton(" 2 "); 
s3=new JButton(" 3 "); 
s4=new JButton(" 4 "); 
s5=new JButton(" 5 "); 
s6=new JButton(" 6 "); 
s7=new JButton(" 7 "); 
s8=new JButton(" 8 "); 
s9=new JButton(" 9 "); 
s0=new JButton(" 0 "); 
b1=new JButton(" + "); 
b2=new JButton(" - "); 
b3=new JButton(" * "); 
b4=new JButton(" / "); 
f1=new JButton(" . "); 
f2=new JButton(" = "); 
f3=new JButton(" C "); GridBagLayout layout = new GridBagLayout(); 
getContentPane().setLayout(layout); 
add(layout, resultField, 0, 0, 4, 1); 
add(layout, s1, 1, 0, 1, 1); 
add(layout, s2, 1, 1, 1, 1); 
add(layout, s3, 1, 2, 1, 1); 
add(layout, s4, 2, 0, 1, 1); 
add(layout, s5, 2, 1, 1, 1); 
add(layout, s6, 2, 2, 1, 1); 
add(layout, s7, 3, 0, 1, 1); 
add(layout, s8, 3, 1, 1, 1); 
add(layout, s9, 3, 2, 1, 1); 
add(layout, f1, 4, 0, 1, 1); 
add(layout, s0, 4, 1, 1, 1); 
add(layout, b1, 1, 3, 1, 1); 
add(layout, b2, 2, 3, 1, 1); 
add(layout, b3, 3, 3, 1, 1); 
add(layout, b4, 4, 3, 1, 1); 
add(layout, f2, 4, 2, 1, 1); 
add(layout, f3, 5, 0, 4, 1); 

private void add(GridBagLayout layout, Component component, int row, int col, int width, int height) 

GridBagConstraints constraints = new GridBagConstraints(); 
constraints.fill = GridBagConstraints.BOTH; 
constraints.insets = new Insets(10, 2, 10, 2); 
constraints.weightx = 100; 
constraints.weighty = 100; 
constraints.gridx = col; 
constraints.gridy = row; 
constraints.gridwidth = width; 
constraints.gridheight = height; 
layout.setConstraints(component, constraints); 
if (component instanceof JButton) 
((JButton)component).addActionListener(this); 
getContentPane().add(component); 


哪有错误呢 弹出但是画面特别小 我又设置画面的大小 为什么呢

解决方案 »

  1.   


    谢谢
    GridBagLayout layout = new GridBagLayout(); 
    getContentPane().setLayout(layout); 
    是写在这马难道代码中的constraints.insets = new Insets(10, 2, 10, 2); 
    constraints.weightx = 100; 
    constraints.weighty = 100; 
    constraints.gridx = col; 
    constraints.gridy = row; 
    constraints.gridwidth = width; 
    constraints.gridheight = height; 
    不是大小马
      

  2.   

    你贴的代码不是全部的吧,我怎么连 JFrame 都没看到?
      

  3.   

    constraints.weightx = 100; //它和下面两个变量 只有在你的容器有空闲的区域时才有用 
    constraints.weighty = 100; 
    constraints.gridx = col; //类似于坐标
    constraints.gridy = row; 
    constraints.gridwidth = width; //每行有几个单元格 下同
    constraints.gridheight = height; 设置大小的那句话在你的 Main()方法里加上即可  随便什么地方 
    this.setSize(300,300)
      

  4.   

    ba dai ma dou tie shang lai , bu quan mei ban fa tiao shi
      

  5.   

    可以,你用这个函数setBounds()非常好用的一个函数。(类 java.awt.Component 中的方法 ,子类也可用)
      

  6.   

    我把代码贴出来 请看看啊 
    现在小数点不能解决 还有像在数字的框框里控制输入数字的位数 就是不要溢出最多满格 怎么办呢package javaapplication3;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Main extends JFrame implements ActionListener{
    private JTextField resultField; //结果栏
    private JButton s1,s2,s3,s4,s5,s6,s7,s8,s9,s0,b1,b2,b3,b4,f1,f2,f3; //按键
    private boolean end,add,sub,mul,div;  // 运算符
    private String str;
    private double num1,num2;  //运算数public Main(){
    super(" MyCalculator");    //柤慜
    resultField=new JTextField("0");
    resultField.setEditable(false);
    resultField.setHorizontalAlignment(JTextField.RIGHT);
    s1=new JButton(" 1 "); 
    s2=new JButton(" 2 "); 
    s3=new JButton(" 3 "); 
    s4=new JButton(" 4 "); 
    s5=new JButton(" 5 "); 
    s6=new JButton(" 6 ");
    s7=new JButton(" 7 ");
    s8=new JButton(" 8 "); 
    s9=new JButton(" 9 "); 
    s0=new JButton(" 0 "); 
    b1=new JButton(" + "); 
    b2=new JButton(" - "); 
    b3=new JButton(" * "); 
    b4=new JButton(" / "); 
    f1=new JButton(" . "); 
    f2=new JButton(" = "); 
    f3=new JButton(" C ");GridBagLayout layout = new GridBagLayout();
    getContentPane().setLayout(layout);
    getContentPane().setSize(300, 300);
    add(layout, resultField, 0, 0, 4, 1);
    add(layout, s1, 1, 0, 1, 1);
    add(layout, s2, 1, 1, 1, 1);
    add(layout, s3, 1, 2, 1, 1);
    add(layout, s4, 2, 0, 1, 1);
    add(layout, s5, 2, 1, 1, 1);
    add(layout, s6, 2, 2, 1, 1);
    add(layout, s7, 3, 0, 1, 1);
    add(layout, s8, 3, 1, 1, 1);
    add(layout, s9, 3, 2, 1, 1);
    add(layout, f1, 4, 0, 1, 1);
    add(layout, s0, 4, 1, 1, 1);
    add(layout, b1, 1, 3, 1, 1);
    add(layout, b2, 2, 3, 1, 1);
    add(layout, b3, 3, 3, 1, 1);
    add(layout, b4, 4, 3, 1, 1);
    add(layout, f2, 4, 2, 1, 1);
    add(layout, f3, 5, 0, 4, 1);
    }
    public void num(int i){
    String s = null;
    s=String.valueOf(i);
    if(end){
    // 是的对话框为0
    resultField.setText("0");
    end=false;}
    if((resultField.getText()).equals("0")){
    //如果是0开始输入数字
    resultField.setText(s);}
    else{
    //如果是数字接着输入数字
    str = resultField.getText() + s;
    resultField.setText(str);}
    }public void actionPerformed(ActionEvent e){ //监听
    if(e.getSource()==s1)
    num(1);
    else if(e.getSource()==s2)
    num(2);
    else if(e.getSource()==s3)
    num(3);
    else if(e.getSource()==s4)
    num(4);
    else if(e.getSource()==s5)
    num(5);
    else if(e.getSource()==s6)
    num(6);
    else if(e.getSource()==s7)
    num(7);
    else if(e.getSource()==s8)
    num(8);
    else if(e.getSource()==s9)
    num(9);
    else if(e.getSource()==s0)
    num(0);//运算符
    else if(e.getSource()==b1)
    sign(1);
    else if(e.getSource()==b2)
    sign(2);
    else if(e.getSource()==b3)
    sign(3);
    else if(e.getSource()==b4)
    sign(4);
    else if(e.getSource()==f3)
    sign(5);
    //等于
    else if(e.getSource()==f1){
    str=resultField.getText();
    if(str.indexOf(".")<=1){
    str+=".";
    resultField.setText(str);
    }
    }
    else if(e.getSource()==f2){
    num2=Double.parseDouble(resultField.getText());
    if(add){
    num1=num1 + num2;}
    else if(sub){
    num1=num1 - num2;}
    else if(mul){
    num1=num1 * num2;}
    else if(div){
    num1=num1 / num2;}
    resultField.setText(String.valueOf(num1));
    end=true;
    }}
    public void sign(int s){
    if(s==1){
    add=true;
    sub=false;
    mul=false;
    div=false;
    }
    else if(s==2){
    add=false;
    sub=true;
    mul=false;
    div=false;
    }
    else if(s==3){
    add=false;
    sub=false;
    mul=true;
    div=false;
    }
    else if(s==4){
    add=false;
    sub=false;
    mul=false;
    div=true;
    }
    else if(s==5){
    add=false;
    sub=false;
    mul=false;
    div=false;
    num1 = 0;
    num2 = 0;
    resultField.setText("0");
    }
    num1=Double.parseDouble(resultField.getText());
    end=true;
    }
    public static void main(String[] args){
    Main th1=new Main();
    th1.pack();
    th1.show();
    }  private void add(GridBagLayout layout, Component component, int row, int col, int width, int height)
    {
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(10, 2, 10, 2);
    constraints.weightx = 100;
    constraints.weighty = 100;
    constraints.gridx = col;
    constraints.gridy = row;
    constraints.gridwidth = width;
    constraints.gridheight = height;
    layout.setConstraints(component, constraints);
    if (component instanceof JButton)
    ((JButton)component).addActionListener(this);
    getContentPane().add(component);
    }
    }
      

  7.   


    高手谈不上,顶多算老手。我改了一下(没改的地方就略过了):
    ...public class Main extends JFrame implements ActionListener {    ...    public void actionPerformed(ActionEvent e) {
            ...        else if (e.getSource() == f1) {
                // 这一段改了
                if (resultField.getText().indexOf(".") == -1) {         // 如果文本当中还没有小数点
                    resultField.setText(resultField.getText() + ".");   // 那么在后面添加一个
                    end = false;                                        // 并允许继续输入数字
                }
            } else if (e.getSource() == f2) {
                ...
            }    }    ...    public static void main(String[] args) {
            Main th1 = new Main();
            th1.pack();
            th1.setDefaultCloseOperation(EXIT_ON_CLOSE);    // 解决窗体关闭后进程不结束的问题
            th1.setVisible(true);                           // 这里不要用 show 方法!
        }    ...