看了一个周的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();
}
}
}
现在有三个定义窗口的办法,请大神们分析优劣,根据当前的编程规范给出建议,谢谢了.第一种:
//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();
}
}
}
大神还有一个看法,放下Frame去研究JFrame,不要被某些书误导
大神还有一个特点,写东西末尾不加标点
大神还有一个看法,放下Frame去研究JFrame,不要被某些书误导 //JFrame是什么?跟Frame有什么区别
大神还有一个特点,写东西末尾不加标点 //哦?
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();
}
});
}
}
好厉害..但是我又不知道import哪些包该怎么办啊
如果想做好的话 先做 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/做一些日常可以用的,一个一个组件的练。
然后就差不多了。还是要手勤,多码字