導航:首頁 > 源碼編譯 > swing項目源碼

swing項目源碼

發布時間:2025-04-02 20:18:47

Ⅰ 用java web小游戲源代碼。期末結課老師讓做,急用,謝了

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MainClass extends JFrame {
ControlSnake control;

Toolkit kit;

Dimension dimen;

public static void main(String[] args) {
new MainClass("my snake");
}

public MainClass(String s) {
super(s);
control = new ControlSnake();
control.setFocusable(true);
kit = Toolkit.getDefaultToolkit();
dimen = kit.getScreenSize();

add(control);
setLayout(new BorderLayout());
setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3
setSize(FWIDTH, FHEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}

public static final int FWIDTH = 315;

public static final int FHEIGHT = 380;
}

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class ControlSnake extends JPanel implements ActionListener {
Random rand;

ArrayList<Point> list, listBody;

String str, str1;

static boolean key;

int x, y, dx, dy, fx, fy, flag;

int snakeBody;

int speed;

public ControlSnake() {
snakeBody = 1;

str = "上下左右方向鍵控制 P鍵暫停...";
str1 = "現在的長度為:" + snakeBody;
key = true;
flag = 1;

speed = 700;
rand = new Random();
list = new ArrayList<Point>();
listBody = new ArrayList<Point>();

x = 5;
y = 5;
list.add(new Point(x, y));
listBody.add(list.get(0));

dx = 10;
dy = 0;

fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2

setBackground(Color.BLACK);
setSize(new Dimension(318, 380));

final Timer time = new Timer(speed, this);
time.start();

addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37) {
dx = -10;
dy = 0;
} else if (e.getKeyCode() == 38) {
dx = 0;
dy = -10;
} else if (e.getKeyCode() == 39) {
dx = 10;
dy = 0;
} else if (e.getKeyCode() == 40) {
dx = 0;
dy = 10;
} else if (e.getKeyCode() == 80) {
if (flag % 2 == 1) {
time.stop();
}
if (flag % 2 == 0) {
time.start();
}
flag++;
}
}
});

}

public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 400, 400);
g.setColor(Color.DARK_GRAY);
g.drawLine(3, 3, 305, 3);
g.drawLine(3, 3, 3, 305);
g.drawLine(305, 3, 305, 305);
g.drawLine(3, 305, 305, 305);
g.setColor(Color.PINK);

for (int i = 0; i < listBody.size(); i++) {
g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9);
}
g.fillRect(x, y, 9, 9);
g.setColor(Color.ORANGE);
g.fillRect(fx, fy, 9, 9);

g.setColor(Color.DARK_GRAY);
str1 = "現在的長度為:" + snakeBody;
g.drawString(str, 10, 320);
g.drawString(str1, 10, 335);
}

public void actionPerformed(ActionEvent e) {
x += dx;
y += dy;
if (makeOut() == false) {
JOptionPane.showMessageDialog(null, "重新開始......");

speed = 700;

snakeBody = 1;

x = 5;
y = 5;

list.clear();
list.add(new Point(x, y));
listBody.clear();
listBody.add(list.get(0));

dx = 10;
dy = 0;

}
addPoint(x, y);
if (x == fx && y == fy) {
speed = (int) (speed * 0.8);//速度增加參數
if (speed < 200) {
speed = 100;
}
fx = rand.nextInt(30) * 10 + 5;// 2
fy = rand.nextInt(30) * 10 + 5;// 2
snakeBody++;// 2
} // 2
repaint();
}

public void addPoint(int xx, int yy) {
// 動態的記錄最新發生的50步以內的移動過的坐標
// 並畫出最新的snakeBody
if (list.size() < 100) {//蛇身長度最長為100
list.add(new Point(xx, yy));
} else {
list.remove(0);
list.add(new Point(xx, yy));
}
if (snakeBody == 1) {
listBody.remove(0);
listBody.add(0, list.get(list.size() - 1));
} else {
listBody.clear();
if (list.size() < snakeBody) {
for (int i = list.size() - 1; i > 0; i--) {
listBody.add(list.get(i));
}
} else {
for (int i = list.size() - 1; listBody.size() < snakeBody; i--) {
listBody.add(list.get(i));
}
}
}
}

public boolean makeOut() {
if ((x < 3 || y < 3) || (x > 305 || y > 305)) {
return false;
}
for (int i = 0; i < listBody.size() - 1; i++) {
for (int j = i + 1; j < listBody.size(); j++) {
if (listBody.get(i).equals(listBody.get(j))) {
return false;
}
}
}
return true;
}
}

/*貪吃蛇代碼*/

Ⅱ Java Swing 怎麼自定義界面背景圖片

在java swing中需要為容器添加自定義圖片或者背景圖片。提兄大供兩種簡單的解棗塵氏決方案,一種利用JPanel,另一種利用JLabel,代碼如下:

1、JPanel(源代碼)

packageoo;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.io.File;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JPanel;

publicclassDrawing{

JFramejframe=newJFrame();
publicstaticJPanelGImage=null;

publicDrawing(){
initFrame();
}

//初始化窗口
publicvoidinitFrame(){
//利用JPanel添加背景圖片

GImage=newJPanel(){

protectedvoidpaintComponent(Graphicsg){
ImageIconicon=newImageIcon("image\benbenla.jpg");
Imageimg=icon.getImage();
g.drawImage(img,0,0,icon.getIconWidth(),
icon.getIconHeight(),icon.getImageObserver());
jframe.setSize(icon.getIconWidth(),icon.getIconHeight());

}

};
jframe.setTitle("測試背景圖片");
jframe.add(GImage);
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoidmain(String[]args){
newDrawing();

}

}

2、JLabel源代碼

packageswing.draw;
importjava.awt.Image;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JLabel;

/**利用JLabel來構建圖片*/
publicclassDrawing2{
JLabeljlpic=newJLabel();
JFramejframe=newJFrame();

publicDrawing2(){

init1Frame();
}

publicvoidinit1Frame(){
ImageIconicon=newImageIcon("image\benbenla.jpg");
凳散icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),
icon.getIconHeight(),Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight()+""+icon.getIconWidth());
jlpic.setBounds(0,0,1366,768);
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
jframe.setSize(1366,768);
jframe.add(jlpic);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);

}

publicstaticvoidmain(Stringargs[]){

newDrawing2();
}
}

添加控制項:

jlpic.setIcon(icon);
Containerc=newContainer();
JLabeluser=newJLabel("用戶:");
JLabelpassword=newJLabel("密碼:");
JTextFieldtxf_userName=newJTextField();
JPasswordFieldpwd_password=newJPasswordField()
{
publicvoidpaste(){
UIManager.getLookAndFeel().provideErrorFeedback(this);
}
};
user.setBounds(200,25,50,25);
password.setBounds(200,52,50,25);
txf_userName.setBounds(300,25,150,25);
pwd_password.setBounds(300,52,150,25);
c.add(user);
c.add(txf_userName);
c.add(password);
c.add(pwd_password);
c.add(jlpic);
jframe.setSize(1366,768);
//jframe.add();
jframe.add(c);

Ⅲ swing自定義控制項實現扁平化 源代碼(其他語言的源碼也行)

這個不是通過設置一個參數能做到的,目前還只有 Look and Feel 庫代碼提供的定製。JRE 目前自帶了幾種 Look and Feel 庫,你想得到一個扁平化的 UI 外觀,需要外掛一個自己的 Look and Feel 實現。


你可以在每個 Swing 組件的 getUI 方法返回的值中找到它的 UI 類,而具體運行的時候這個 UI 類是什麼則是由 Look and Feel 來自動配置的,比如在 Windows 上可能是 Windows 開頭的類名,而在 Linux 可能是 Metal 開頭的類名。

閱讀全文

與swing項目源碼相關的資料

熱點內容
bt種子能壓縮嗎 瀏覽:671
JAVA特點是什麼 瀏覽:881
字幕網app怎麼找 瀏覽:528
奧迪a4l壓縮比 瀏覽:693
iis7伺服器怎麼安裝 瀏覽:975
孩子上學壓力太大怎麼解壓 瀏覽:236
javaurl格式 瀏覽:342
勻稱app怎麼用 瀏覽:61
pythonemail中文亂碼 瀏覽:444
android訪問html 瀏覽:88
做一個app或者網頁用什麼編程 瀏覽:778
一體機伺服器運行失敗怎麼辦 瀏覽:938
longjava比較大小 瀏覽:532
clojure編程pdf 瀏覽:22
一年級數學圈一圈的演算法 瀏覽:111
完整的python 瀏覽:42
c51單片機數字鍾 瀏覽:923
apk文件反編譯後安裝包損壞 瀏覽:816
app圖標分別是什麼 瀏覽:368
linux全選命令 瀏覽:365