工具/原料 Eclipse 方法/步骤 依次选择file--New--Project选项 选择java——>java project 在Project name后输入工程名 new——>package New——>Class 以上步骤全部完成后,就可以开始编写程序了。
//正好以前注释过一个
import javaawt;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaawteventKeyAdapter;
import javaawteventKeyEvent;
import javaawtimageMemoryImageSource;
import javautilRandom;
import javaxswingJDialog;
import javaxswingJPanel;
import javaxswingTimer;
public class CharacterRain extends JDialog implements ActionListener {
private static final long serialVersionUID = 1L;
private Random random = new Random();
private Dimension screenSize;
private JPanel graphicsPanel;
private final static int gap = 20;
private int[] posArr;
private int lines;
private int columns;
public static void main(String[] args) {
new CharacterRain();
}
public CharacterRain() {
initComponents();
}
private void initComponents() {
setLayout(new BorderLayout());
graphicsPanel = new GraphicsPanel();
add(graphicsPanel, BorderLayoutCENTER);
Toolkit defaultToolkit = ToolkitgetDefaultToolkit();
Image image = defaultToolkitcreateImage(new MemoryImageSource(0, 0,
null, 0, 0));
Cursor invisibleCursor = defaultToolkitcreateCustomCursor(image,
new Point(0, 0), "cursor");
setCursor(invisibleCursor);
KeyPressListener keyPressListener = new KeyPressListener();
thisaddKeyListener(keyPressListener);
thissetAlwaysOnTop(true);
thissetUndecorated(true);
thisgetGraphicsConfiguration()getDevice()setFullScreenWindow(this);
thissetDefaultCloseOperation(JDialogDISPOSE_ON_CLOSE);
setVisible(true);
screenSize = ToolkitgetDefaultToolkit()getScreenSize();
lines = screenSizeheight / gap; //用gap来分行
columns = screenSizewidth / gap; //用gap来分列
posArr = new int[columns + 1];
random = new Random();
for (int i = 0; i < posArrlength; i++) {
posArr[i] = randomnextInt(lines); //随机行数
}
new Timer(100, this)start();
}
private char getChr() {
return (char) (randomnextInt(94) + 33); //返回随机字符
}
public void actionPerformed(ActionEvent e) {
graphicsPanel repaint(); // Timer事件,graphicsPanel重绘
}
@SuppressWarnings("serial")
private class GraphicsPanel extends JPanel {
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d setFont (getFont () deriveFont (Font BOLD));
g2d setColor(Color BLACK);
g2d fillRect (0, 0, screenSize width, screenSize height); // 设置背景色为黑色
int currentColumn = 0;
for (int x = 0; x < screenSizewidth; x += gap) {
int endPos = posArr[currentColumn]; //获得开始行位置
g2d setColor(Color GREEN);
g2ddrawString(StringvalueOf(getChr()), x, endPos gap); //画出随机开始行的字符
int cg = 0; //初始黑色
int length = 25; //字符列的长度
for (int j = endPos -length; j < endPos; j++) { //随机行逐列向上length行,为循环开始行
cg += 255/(length + 1); //渐变色递增,0,255,0为green
if (cg > 255) {
cg = 255;
}
g2dsetColor(new Color(0, cg, 0));
g2ddrawString(StringvalueOf(getChr()), x, j gap); //画出随机行后的15行字符,颜色从黑色渐变成绿色,逐行增加
}
posArr[currentColumn] += randomnextInt(5); //下落距离最快为4, 会产生加速下落的感觉
if ((posArr[currentColumn] -5) gap > getHeight()) { //如果行数位置大于屏幕高度,从新获取合适的行
posArr[currentColumn] = randomnextInt(lines);
}
currentColumn++; //下一列获取随机行
}
}
}
private class KeyPressListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
if (egetKeyCode() ==KeyEventVK_ESCAPE) { //监听键盘事件,按ESC按键退出
Systemexit(0);
}
}
}
}
以上就是关于如何用eclipse编写java gui程序全部的内容,包括:如何用eclipse编写java gui程序、怎样用JAVA的GUI(图形用户界面)来设计一个程序!求程序和详解。、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)