我的这个java程序怎么在myeclipse里面运行不了?没有结果啊

package jbutton;
import java.awt.*;
import javax.swing.*;
public final class MyFrame extends JFrame {
JButton clear;
JButton save;
JButton exit;
JButton red;
JButton green;
JButton black;
JPanel p,m;
JTextArea t;
public MyFrame(){
super("JPanel Test");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
testInit();
Container cc=getContentPane();
cc.setLayout(new BorderLayout());
cc.add(p,"West");
cc.add(m,"South");
cc.add(t,"Center");
}

void testInit(){
clear=new JButton("Clear");
save=new JButton("Save");
exit=new JButton("Exit");
red=new JButton("Red");
green=new JButton("Green");
black=new JButton("Black");
t=new JTextArea(10,10);
p=new JPanel(new GridLayout(3,1,5,5));
p.add(clear);
p.add(save);
p.add(exit);
m=new JPanel(new GridLayout(1,3,5,5));
m.add(red);
m.add(green);
m.add(black);
}

public static void main(String[] args) {
new MyFrame();

}

}

JFrame窗体在没有指定大小,和让它显示的情况下,系统会给它分配一个默认值,但是这是看不见的,你必须重新指定一下JFrame的大小和,显示,不然运行不出来,还有就是,你在主函数里虽然创建了对像,但是没有指向它的一个引用,修改代码如下:

package jbutton;

import java.awt.*;

import javax.swing.*;

public final class MyFrame  extends JFrame {

JButton clear;

JButton save;

JButton exit;

JButton red;

JButton green;

JButton black;

JPanel p,m;

JTextArea t;

public MyFrame(){

super("JPanel Test");

testInit();

Container cc=getContentPane();

cc.setLayout(new BorderLayout());

cc.add(p,"West");

cc.add(m,"South");

cc.add(t,"Center");

                          //设置窗体

this.setSize(400,300);

this.setLocation(200, 100);

this.setResizable(false);//禁止改变窗体大小

this.setVisible(true);//显示窗体(这句代码必须得写,不然还是不会显示的)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

   void testInit(){

   clear=new JButton("Clear");

   save=new JButton("Save");

   exit=new JButton("Exit");

  red=new JButton("Red");

   green=new JButton("Green");

   black=new JButton("Black");

   t=new JTextArea(10,10);

   p=new JPanel(new GridLayout(3,1,5,5));

   p.add(clear);

   p.add(save);

   p.add(exit);

   m=new JPanel(new GridLayout(1,3,5,5));

   m.add(red);

   m.add(green);

   m.add(black);

   }   

public static void main(String[] args) {

MyFrame myframe=new MyFrame();

}

}

运行结果如图:

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-09-10
奥 这个该死的JFrame默认是不可见的,你需要修改一下,如下:
public static void main(String[] args) {
JFrame frame = new MyFrame();
frame.setSize(400, 400);
frame.setVisible(true);
}
或者,在MyFrame的构造函数里添加如下2行:
public MyFrame(){
.......
setSize(400, 400);
setVisible(true);
}
第2个回答  2011-09-10
同上
相似回答