日期:2014-05-20  浏览次数:20859 次

java se,求教
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 {

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

}

}

------解决方案--------------------
这样可以了。。
Java code

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("="));

    }
}

------解决方案--------------------
Java code

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("="));
    
        }
    }
}

------解决方案--------------------
说下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;