更新時(shí)間:2022-09-14 11:09:11 來源:動(dòng)力節(jié)點(diǎn) 瀏覽4200次
Java的應(yīng)用領(lǐng)域很多,人們可以利用java做網(wǎng)站,也可以用來做微信小程序,還可以做可視化界面,那java如何做可視化界面?下面來我們就用實(shí)例來給講解一下java做可視化界面。
先介紹Testjiemian.java中的,因?yàn)檫@個(gè)是一個(gè)自動(dòng)的,所以我們需要創(chuàng)建線程;
Thread t = new Thread(this);
t.start();
我們需要一個(gè)重載方法,然后讓線程無限循環(huán),需要進(jìn)行異常處理
public void run()
{
// TODO Auto-generated method stub
//重載run方法
while (true)
{
//線程無線循環(huán)
try
{
Thread.sleep(30);
}
catch (InterruptedException e)
{}
之后需要設(shè)置小球的縱坐標(biāo)的變化,但是之前需要有初始位置,這里小球初始位置就不寫了,后面完整代碼中有。同時(shí)使用repaint()函數(shù)在每次縱坐標(biāo)變化之后重繪之前的圖像。
//更改縱坐標(biāo)
yp += 3;
y += 5;
if (yp > 430)
yp = -80;
repaint();
if (y > 410)
y = -80;
repaint();
設(shè)置小球的顏色和大小。
g.clearRect(0,yp,this.getWidth(),this.getHeight());
g.setColor(Color.green);//設(shè)置小球的顏色
g.fillOval(60, yp, 50, 50);
xiaoqiu.java文件中,設(shè)置界面的大小以及顯示界面。
this.setTitle("空中小球");
Container c = this.getContentPane();//獲得面板
c.add(new Testjianmian());
//關(guān)閉窗口時(shí)關(guān)閉程序
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setSize(350, 480);
//設(shè)置窗口居中,但是必須放在size之后
this.setLocationRelativeTo(null);
this.setVisible(true);
下面就是附上完整代碼,首先是Testjiemian.java文件的:
package com.test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
public class Testjianmian extends JPanel implements Runnable
{
//繪制圖形線程類
//小球左上角的縱坐標(biāo)
public int yp = -80;
public int y = -80;
public int xp = -80;
//定義兩個(gè)私有成員
private Image iBf;
private Graphics gBf;
public Testjianmian()
{
//創(chuàng)建線程
Thread t = new Thread(this);
t.start();
}
@Override
public void run()
{
// TODO Auto-generated method stub
//重載run方法
while (true)
{
//線程無線循環(huán)
try
{
Thread.sleep(30);
}
catch (InterruptedException e)
{}
//更改縱坐標(biāo)
yp += 3;
y += 5;
if (yp > 430)
yp = -80;
repaint(); //重繪
if (y > 410)
y = -80;
repaint();
xp += 3;
if (xp > 300)
xp = -80;
repaint();
}
}
public void paint(Graphics g)
{
//重載繪圖方法
super.paint(g);
//清洗屏幕
g.clearRect(0, yp, this.getWidth(), this.getHeight());
g.setColor(Color.green); //設(shè)置小球的顏色
g.fillOval(60, yp, 50, 50); //坐標(biāo)及小球的大小
g.fillOval(220, xp, 50, 50);
g.setColor(Color.yellow);
g.fillOval(135, y, 60, 60);
}
}
下面是xiaoqiu.java文件的:
package com.test;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class xiaoqiu
{
public static class MyWindow extends JFrame
{
public MyWindow()
{
this.setTitle("空中小球");
Container c = this.getContentPane(); //獲得面板
c.add(new Testjianmian()); //添加到面板中
//關(guān)閉窗口時(shí)關(guān)閉程序
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setSize(350, 480); //窗口大小
//設(shè)置窗口居中,但是必須放在size之后
this.setLocationRelativeTo(null);
this.setVisible(true); //顯示窗口
}
public static void main(String[] args)
{
MyWindow DB = new MyWindow();
DB.addWindowListener(new WindowAdapter()
{
public void windowclosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
}
結(jié)果展示:
對(duì)于這個(gè)程序,主要是java可視化界面的使用,對(duì)于初學(xué)者來說,還是挺容易掌握的。因?yàn)槔锩娴拇a也很簡(jiǎn)單,基本都是基礎(chǔ),所以多多練習(xí)還是有好處的。感興趣的小伙伴也可以了解一下java界面開發(fā)工具,必備的幾款,相信大家在以后的學(xué)習(xí)中會(huì)用到。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)