最后一个小列子,说明swing与awt的一个小小的对比:
package gui.test11;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;
/**
* JFrame对象上面有一个唯一的组件:JRootPane,通过调用.getContentPane()方法可返回这个值
* @author junlas
*
*/
public class TestJFrame extends JFrame {
public TestJFrame() {
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//(3:dialog的例子)
JOptionPane.showMessageDialog(null, "程序开始");
/*(1:JFame的直接子容器JPanel)
this.getContentPane();
JButton btn = new JButton("ok");
add(btn);
System.out.println(btn.getParent());//输出为:javax.swing.JPanel
*/
/*(2:JScrollPane的直接子容器JViewport)
JScrollPane sp = new JScrollPane();
JTextArea ta = new JTextArea(50,50);
sp.getViewport().add(ta);//JScrollPane中有一个JViewport容器
System.out.println(ta.getParent());
add(sp);
*/
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if(JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(TestJFrame.this, "真的要退出吗?","结束程序"
,JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)){
dispose();
System.exit(0);
}else{
//什么也不做...
}
}
});
}
public static void main(String[] args) {
TestJFrame mainFrame = new TestJFrame();
mainFrame.setSize(400, 400);
mainFrame.setTitle("TestJFrame");
mainFrame.setVisible(true);
}
}
//-------------------------------------------------------------------------------------
GUI基础先告一段落~~···。