這個關於java swing界面的程序是我以前寫過的代碼。現在只是更新了好多功能和改善了界面,使界面更加的友好,同時也使程序更加的穩定可靠,即使如此這個程序在運行時還是有缺陷。例如加載網絡圖片時,就顯得有點卡,不知道是不是網速的原因,而且對網絡圖片的操作也使得整個界面變得響應緩慢。如果你是個高手,我相信你完全可以自己去改善這個程序,最終使這個程序變得更完美,這個程序我還要在后續的工作中進行改善。其實java做界面,特別是本地客戶端的應用軟件,java完全表現失敗。試想一下,一個用戶為了運行一個java程序,不得不安裝一個用戶並不想安裝的顯得又大又笨拙的虛擬機!而且運行時它的速度有時候就像是狗爬!這可真是玩笑,但卻是真的,怪不得java會在本地應用方面徹底敗給C++。要是脫離了網絡這個大環境java的發展或許根本就沒有現在這么好!java現在好像也不是很春風得意,畢竟有那么多的語言在虎視眈眈,試圖取代java的地位。java的母公司SUN被甲骨文收購,真的不知道是一件好事還是壞事。閑話不多說了!這個程序比較的大,有一千多行(其實也不大啦),我把源程序制成壓縮包上傳到CSDN上面,其中CutImg類是單獨的一個文件,有興趣的朋友可以自己去下,其中要注意的是剪切功能只能實現對本地圖片,還不能實現對網絡圖片的剪切,java高手可以自己實現這個功能,下面給出轉向下載頁面的文字鏈接:
文字鏈接:http://download.csdn.net/download/zhulike2011/5524851
下面是其中的一些片段:
public void paint(Graphics g){ //小球畫圖的方法,利用Graphics類進行繪圖
g.setColor(pane.getBackground()); //這條語句用於擦除小球軌跡,讓這條語句不起作用你就知道了
g.fillOval(tempX, tempY, XSIZE, YSIZE);
g.setColor(c);
g.fillOval(x, y, XSIZE, YSIZE);
tempX=x;tempY=y;
}
public void move(Rectangle2D g){ //這個方法用於控制小球運動,用於判斷小球是否到達邊界
x += dx;
y += dy;
if(x<2){
dx = -dx;
}
if(x+XSIZE>retangleX){
dx = -dx;
}
if(y<g.getMinY()+YSIZE){
dy = -dy;
}
if(y+YSIZE+40>=retangleY){
dy = -dy;
}
}
/**
* 剪切板中是否有文本數據可供粘貼
*
* @return true為有文本數據
*/
public boolean isClipboardString() {
boolean b = false;
Clipboard clipboard = this.getToolkit().getSystemClipboard();
Transferable content = clipboard.getContents(this);
try {
if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {
b = true;
}
} catch (Exception e) {
}
return b;
}
//這個方法用於讀取圖片
try {
img=ImageIO.read(
new File("D:\\My Documents\\My Pictures\\SB.gif" ));//不能顯示動態,只是靜態的
img=img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
beiJing= new ImageIcon(img );
} catch (IOException e) {
e.printStackTrace();
}
下面是構造器方法,如果你能看懂這個構造器的作用,那這整個程序,你就可以寫出來了!
public TestDemo(){
cot=this.getContentPane();
setSize(700,500);
pane =new BeiJingPane(this.getWidth(),this.getHeight());
panBall=new XiaoQiuPane(this.getWidth(),this.getHeight());
clearityPane=new JPanel();
clearityPane.setVisible(true);
clearityPane.setOpaque(false); //透明效果
clearityPane.setBounds(new Rectangle(700,450));
panBall.setLayout(new BorderLayout());
buttonBallPane.add(changeBackColor);
buttonBallPane.add(changeBallColor);
buttonBallPane.add(start_ball);
buttonBall.setLayout(new BorderLayout());
buttonBall.setBackground(Color.red);
buttonBall.add(buttonBallPane, BorderLayout.SOUTH);
label.setSize(100, 150);
ball=new BallDemo(this.getSize().width-20,this.getSize().height-30,buttonBall);
clearityPane.add(bbb);
pane.setLayout(new BorderLayout());
bbb.setVisible(false);
panBall.add(clearityPane,BorderLayout.CENTER); //添加透明面板
buttonpane.add(changePicture);
buttonpane.add(changeColor);
buttonpane.add(start_retangle);
panBall.add(buttonpane, BorderLayout.SOUTH);
//cot.add(pane);
panButton.add(bt);
panButton.add(bt0);
panButton.add(bt1);
panButton.add(bt2);
panButton.add(bt3);
panButton.add(bt4);
menuBar.add(menuOpen);
menuBar.add(menuNative);
menuBar.add(menuNet);
menuBar.add(menuQuit);
menuBar.add(menuSave);
pane.add(panButton,BorderLayout.SOUTH);
pane.add(menuBar,BorderLayout.NORTH);
pane.add(label,BorderLayout.CENTER);
changePicture.addActionListener(this);
changeColor.addActionListener(this);
changeBallColor.addActionListener(this);
changeBackColor.addActionListener(this);
start_retangle.addActionListener(this); //給矩形小球啟動按鈕注冊監聽
start_ball.addActionListener(this); //給小球啟動按鈕注冊監聽
pane.addMouseListener(this);
bt0.addActionListener(this);
bt.addActionListener(this);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
label.addMouseListener(this);//給圖片注冊監聽
menuSave.addMouseListener(this);
menuOpen.addMouseListener(this);
menuNative.addMouseListener(this);
menuNet.addMouseListener(this);
menuQuit.addMouseListener(this);
jtp.addChangeListener(this); //給選項卡注冊變化接口監聽
jtp.add("默認面板",pane);
jtp.add("矩形小球運動", panBall);
jtp.add("小球運動", buttonBall);
cot.add(jtp);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
下面是效果圖: