导航:首页 > 源码编译 > 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项目源码相关的资料

热点内容
艺术编程哪家排名好 浏览:255
递归找素数python 浏览:761
解压漂亮手机壳小视频 浏览:720
pythonimportpwd 浏览:137
word转pdf转换器110 浏览:195
pythonfor循环int 浏览:479
旧版本安卓怎么安装 浏览:835
怎么样算法定退休年龄 浏览:623
app花钱怎么退款 浏览:820
51单片机atmel 浏览:213
面板单重门槛命令 浏览:969
程序员性生活厉害吗 浏览:608
如何改服务器子网掩码 浏览:560
python写满磁盘 浏览:608
外省违章怎么交罚款app 浏览:771
如何提取wml源码 浏览:677
程序员陆漓出国了吗 浏览:991
python爬二级链接 浏览:627
程序员被老总骂 浏览:582
如何在win7下连接网络连接到服务器 浏览:129