看了一个周的java了,现在看到GUI了,我表示我已经忘记昨天看过的东西了.
现在有三个定义窗口的办法,请大神们分析优劣,根据当前的编程规范给出建议,谢谢了.第一种:
//frame1.java
import java.awt.*;
import java.awt.event.*;
public class frame1
{
public static void main(String args[])
{
Frame f =new Frame("第一个窗口");
f.setSize(400,300);
//设置框架窗体的事件监听(关闭窗体)
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
//正常退出JVM
System.exit(0);
}
});
f.setVisible(true);
}}第二种:
//frame2.java
import java.awt.*;
import java.awt.event.*;
public class frame2 extends Frame
{

public frame2(String str)
{
super(str);
}
//程序的入口方法
public static void main(String args[])
{
WinClose wc= new WinClose();
frame2 f=new frame2("第二个窗口");
f.addWindowListener(wc);
f.setSize(400,300);
f.setVisible(true);
}
}
class WinClose extends WindowAdapter
{
public  void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
第三种:
//frame3.java
import java.awt.*;
import java.awt.event.*;
public class frame3 extends Frame //implements ActionListener
{
static frame3 f=new frame3();
static WinClose wc=new WinClose();
public static void main(String args[])
{
f.setTitle("第三个窗口");
f.addWindowListener(wc);
f.setSize(400,300);
f.setVisible(true);
}
static class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
f.dispose();
}
}
}

解决方案 »

  1.   

    首先,没用过awt = =但是,让我选,我肯定不会用第三种 静态神马的不喜欢 = =没有特殊需求的话(自定义Frame),1,2两种差不多,2更适合那些有洁癖的程序员 = = 我就是酱紫。。
      

  2.   

    如果你把那些设置Frame的语句写到构造函数中去
      

  3.   

    大神的看法是没有优劣,在不同的应用场景有不同的写法
    大神还有一个看法,放下Frame去研究JFrame,不要被某些书误导
    大神还有一个特点,写东西末尾不加标点
      

  4.   

    大神的看法是没有优劣,在不同的应用场景有不同的写法               //哦!
    大神还有一个看法,放下Frame去研究JFrame,不要被某些书误导        //JFrame是什么?跟Frame有什么区别  
    大神还有一个特点,写东西末尾不加标点                             //哦?
      

  5.   

    不要用 awt 用 swing 或者 swt 去import java.awt.Color;
    import java.awt.Dimension;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenuBar;public class HelloSwing {

    public static void createUI() {
    JFrame frame = new JFrame("Hello JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // above line is import to exit correct

    JLabel label = new JLabel("Hello World");
    frame.getContentPane().add(label);

    JMenuBar menuBar = new JMenuBar();
    menuBar.setOpaque(true);
    menuBar.setBackground(new Color(154,165,127));
    menuBar.setPreferredSize(new Dimension(200, 20));

    frame.setJMenuBar(menuBar);



    // deploy the window
    frame.pack();
    frame.setVisible(true);

    }

    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
    public void run(){
    createUI();
    }
    });
    }

    }
      

  6.   


    好厉害..但是我又不知道import哪些包该怎么办啊
      

  7.   

    多打 gui界面是 java 诟病。
    如果想做好的话 先做 100个 demo出来再说多看别人的代码http://download.oracle.com/javase/tutorial/uiswing/components/combobox.htmlhttp://download.oracle.com/javase/tutorial/uiswing/examples/components/index.html#CustomComboBoxDemohttp://www.ibm.com/developerworks/cn/java/j-gui/做一些日常可以用的,一个一个组件的练。
    然后就差不多了。还是要手勤,多码字