借鑑了java 核心技術卷1 並參考http://www.jb51.net/article/56158.htm 添加了JTextField的使用。java
##################################################################################################框架
package com;函數
import java.awt.Dimension;
import java.awt.Graphics; //添加graphics對象.net
import javax.swing.JComponent;線程
public class NotHelloWorldComponent extends JComponent {
/**
*
*/
private static final long serialVersionUID = 1L;
//定義參數
public static final int MESSAGE_X=75;
public static final int MESSAGE_Y=100;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=500;
//繪圖方法
public void paintComponent(Graphics g){
g.drawString("Not a Hello,World program", MESSAGE_X, MESSAGE_Y);
}
//覆蓋getPreferredSize方法,返回組件大小 Dimension對象
public Dimension getPreferredSize(){
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
}orm
##################################################################################################htm
添加JFrame框架對象
package com;
import javax.swing.* ; //導入swing包
import java.awt.*; //導入awt包繼承
public class McStart extends JFrame{ //繼承JFrame
private static final long serialVersionUID = 1L;事件
public McStart(){ //構造函數
add(new NotHelloWorldComponent()); //添加JComponent組件
pack();
Toolkit kit=Toolkit.getDefaultToolkit(); //獲取Tookit
Dimension screenSize=kit.getScreenSize(); //getScreenSize方法,類型爲Dimension
int screenHight=screenSize.height; //取hight
int screenWidth=screenSize.width; //取width
//設置窗口寬和高
setSize(screenHight/2,screenWidth/2);
setLocationByPlatform(true);
//設置圖片,先new一個對象並傳入參數後賦予set函數
Image img=new ImageIcon("icon.gif").getImage();
setIconImage(img); //設置標題欄,任務欄以及切換窗口等位置顯示的圖標
}
}
#####################################################################################################
主函數:
package com;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class SizeFrameTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() //匿名內部類,實現run方法,事件分派線程
{
public void run()
{
JFrame frame=new McStart(); //實例化一個McStart對象
frame.setTitle("McStart"); //設置窗口標題欄的文字
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //響應用戶動做,關閉框架的顯示 詳情見P259
frame.setVisible(true); //顯示組件
}
});
}
}
###################################################################################################