首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

java se,求教解决思路

2012-01-10 
java se,求教import java.awt.GridLayoutimport java.awt.event.ActionEventimport java.awt.event.Acti

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;


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

热点排行