本帖最后由 yxs1112003 于 2011-10-13 18:23:14 编辑

解决方案 »

  1.   

    这样可以了。。package com.yhy.test;import java.awt.GridLayout;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;public class mult{ public static void main(String[] args) {
    ShowGridLayout frame = new ShowGridLayout(); frame.setTitle("GirdLayout");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(200, 200); }}
    class ShowGridLayout extends JFrame {
    public ShowGridLayout() {
    setLayout(new GridLayout(2, 2, 5, 5)); add(new JTextField(5));
    add(new JTextField(5));
    add(new JButton("*"));
    add(new JButton("=")); }
    }
      

  2.   


    package com.yhy.test;import java.awt.GridLayout;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;public class mult { public static void main(String[] args) {
    mult m = new mult();
    ShowGridLayout frame = m.new ShowGridLayout();//实例化一个嵌套类的方法
    frame.setTitle("GirdLayout");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(200, 200);
    }
     public class ShowGridLayout extends JFrame {
    public ShowGridLayout() {
    setLayout(new GridLayout(2, 2, 5, 5));

    add(new JTextField(5));
    add(new JTextField(5));
    add(new JButton("*"));
    add(new JButton("="));

    }
    }
    }
      

  3.   

    说下LZ的代码,有点受力不讨好。在Mult类内定义内部类 ShowGridLayout 。但还是按照LZ的代码改成可运行的。import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    public class Mult {static public class ShowGridLayout extends JFrame
    {
    public ShowGridLayout()
    {
    setLayout(new GridLayout(2,2,5,5));add(new JTextField(5));
    add(new JTextField(5));
    add(new JButton("*"));
    add(new JButton("="));}

    public static void main(String[]args)
    {
    ShowGridLayout frame = new ShowGridLayout();frame.setTitle("GirdLayout");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(200,200);}}我觉得LZ的代码最好是改成下面这种形式:代码如下:
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import java.awt.GridLayout;public class Mult {
    public static void main(String[] args) {

    ShowGridLayout sd = new ShowGridLayout();
    sd.init();
    }
    }class ShowGridLayout extends JFrame {

    //定义一个初始化方法
        public void init() {
        
            this.setLayout(new GridLayout(2,2,5,5));
          
            this.add(new JTextField(5));
            this.add(new JTextField(5));
            this.add(new JButton("*"));
            this.add(new JButton("="));
            
            this.setSize(200,200);
            this.setTitle("GirdLayout");
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
           
        }
    }代码不明白为什么的话,可以再问我,很乐意为你解答,希望这些能帮到LZ