总结:
import java.awt.*;
public class MyBall{
public static void main(String[] args){
Frame w = new Frame();
w.setSize(300, 400);
MyPanel mp = new MyPanel();
w.add(mp);
Thread t = new Thread(mp);
t.start();
w.show();
}
}
class MyPanel extends Panel implements Runnable{
int x = 30;
int y = 30;
int att = 0;
public void paint(Graphics g){
g.fillOval(x, y, 20, 20);
}
public void run(){
while(true){
if(att == 0){
x++;
y++;
}
if(att == 1){
x--;
y++;
}
if(att == 2){
x--;
y--;
}
if(att == 3){
x++;
y--;
}
if(x > 263){
if(att == 0){
att = 1;
}else{
att = 2;
}
}
if(y > 341){
if(att == 1){
att = 2;
}else{
att =3;
}
}
if(x < 0){
if(att == 2){
att = 3;
}else{
att = 0;
}
}
if(y < 0){
if(att == 3){
att = 0;
}else{
att = 1;
}
}
try{
Thread.sleep(30);
}catch(Exception e){
}
repaint();
}
}
}
运行结果:(画斜线的小球,撞墙会改变方向)
图片.pngimport java.awt.*;
import java.awt.event.*;
public class MyBall2{
public static void main(String[] args){
Frame w = new Frame();
w.setSize(300, 400);
MyPanel mp = new MyPanel();
w.add(mp);
w.addKeyListener(mp);
mp.addKeyListener(mp);
w.show();
}
}
class MyPanel extends Panel implements KeyListener{
int x = 30;
int y = 30;
public void paint(Graphics g){
g.fillOval(x, y, 20, 20);
}
@Override
public void keyPressed(KeyEvent arg0){
if(arg0.getKeyCode() == 37){
x--;
}
if(arg0.getKeyCode() == 38){
y--;
}
if(arg0.getKeyCode() == 39){
x++;
}
if(arg0.getKeyCode() == 40){
y++;
}
repaint();
}
@Override
public void keyReleased(KeyEvent arg0){
}
@Override
public void keyTyped(KeyEvent arg0){
}
}
运行结果:(键盘上的上下左右键控制小球)
图片.pngimport java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MyChar{
public static void main(String[] args){
Frame w = new Frame();
w.setSize(300, 400);
MyPanel mp = new MyPanel();
w.add(mp);
w.addKeyListener(mp);
mp.addKeyListener(mp);
Thread t = new Thread(mp);
t.start();
w.show();
}
}
class MyPanel extends Panel implements Runnable,KeyListener{
int[] x = new int[10];
int[] y = new int[10];
char[] c = new char[10];
int score = 1000;
public MyPanel(){
for(int i=0; i<10;i++){
x[i] = (int)(Math.random()*300);
y[i] = (int)(Math.random()*300);
c[i] = (char)(Math.random()*26 + 97);
}
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
for(int i=0;i<10;i++){
g.drawString(new Character(c[i]).toString(), x[i], y[i]);
}
g.setColor(Color.RED);
g.drawString("你的成绩是:"+score, 5, 15);
}
public void run(){
while(true){
for(int i=0;i<10;i++){
y[i]++;
if(y[i]>400){
y[i] = 0;
x[i] = (int)(Math.random()*300);
c[i] = (char)(Math.random()*26 + 97);
score -= 100;
}
}
try{
Thread.sleep(30);
}catch(Exception e){
}
repaint();
}
}
@Override
public void keyPressed(KeyEvent arg0){
char keyC = arg0.getKeyChar();
int nowY = -1;
int nowIndex = -1;
for(int i=0;i<10;i++){
if(keyC == c[i]){
if(y[i]>nowY){
nowY = y[i];
nowIndex = i;
}
}
}
if(nowIndex != -1){
y[nowIndex] = 0;
x[nowIndex] = (int)(Math.random()*300);
c[nowIndex] = (char)(Math.random()*26 + 97);
score += 10;
}else{
score -= 100;
}
}
@Override
public void keyReleased(KeyEvent arg0){
}
@Override
public void keyTyped(KeyEvent arg0){
}
}
运行结果:(打字游戏)
图片.pngimport java.awt.*;
public class MySnow{
public static void main(String[] args){
Frame w = new Frame();
w.setSize(1024, 768);
w.setBackground(Color.BLACK);
MyPanel mp = new MyPanel();
w.add(mp);
Thread t = new Thread(mp);
t.start();
w.show();
}
}
class MyPanel extends Panel implements Runnable{
int x[] = new int[300];
int y[] = new int[300];
public MyPanel(){
for(int i=0;i<300;i++){
x[i] = (int)(Math.random()*1024);
y[i] = (int)(Math.random()*768);
}
}
public void paint(Graphics g){
g.setColor(Color.WHITE);
for(int i=0;i<300;i++){
g.drawString("*",x[i], y[i]);
}
}
public void run(){
while(true){
try{
for(int i=0; i<300; i++){
y[i]++;
if(y[i]>768){
y[i] = 0;
}
}
Thread.sleep(30);
}catch(Exception e){
}
repaint();
}
}
}
运行结果:(下雪)
图片.pngimport java.awt.Frame;
import java.awt.*;
public class MyTest{
public static void main(String args[]){
Frame w = new Frame();
w.setSize(300, 400);
MyPanel mp = new MyPanel();
w.add(mp);
w.show();
}
}
class MyPanel extends Panel{
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.drawLine(30, 30, 100, 100);
g.fillOval(30, 30, 50, 100);
}
}
运行结果:(画斜线和椭圆)
图片.pngimport java.awt.*;
import java.lang.Math;
public class MyTest2{
public static void main(String[] args){
Frame w = new Frame();
w.setSize(1024,768);
w.setBackground(Color.BLACK);
MyPanel2 mp = new MyPanel2();
w.add(mp);
w.show();
}
}
class MyPanel2 extends Panel{
public void paint(Graphics g){
g.setColor(Color.WHITE);
for(int i = 0 ; i < 300; i++){
g.drawString("*", (int)(Math.random()*1024),
(int)(Math.random()*768));
}
}
}
运行结果:(画满天星星)
图片.pngimport java.awt.*;
import javax.swing.*;
public class MyBall{
public static void main(String[] args){
JFrame w = new JFrame();
w.setSize(300, 400);
MyPanel mp = new MyPanel();
w.add(mp);
Thread t = new Thread(mp);
t.start();
w.setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable{
int x = 30;
int y = 30;
public void paint(Graphics g){
super.paint(g);
g.fillOval(x, y, 20, 20);
}
public void run(){
while(true){
y++;
if(y>400){
y = 0;
}
try{
Thread.sleep(20);
}catch(Exception e){
}
repaint();
}
}
}
运行结果:(下落小球)
图片.pngimport java.awt.*;
import javax.swing.*;
public class MyButton{
public static void main(String[] args){
JFrame w = new JFrame();
w.setSize(300, 400);
JButton b1 = new JButton("OK1");
JButton b2 = new JButton("OK2");
JButton b3 = new JButton("OK3");
JButton b4 = new JButton("OK4");
JButton b5 = new JButton("OK5");
//设置布局
w.setLayout(new FlowLayout());
w.add(b1);
w.add(b2);
w.add(b3);
w.add(b4);
w.add(b5);
w.setVisible(true);
}
}
运行结果:(画按钮)
图片.pngpublic class MyTest{
public static void main(String args[]){
String s1 = new String("abc");
String s2 = new String("abc");
String s3 = "abc";
String s4 = "abc";
System.out.println(s1==s2);
System.out.println(s3==s4);
System.out.println("abc".hashCode());
}
}
运行结果:
图片.pngimport java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class QQLogin implements ActionListener{
public static void main(String args[]){
JFrame w = new JFrame();
w.setSize(250, 125);
//new 组件
JLabel labUser = new JLabel("用户名");
JLabel labPass = new JLabel("密码");
JTextField txtUser = new JTextField();
JPasswordField txtPass = new JPasswordField();
JButton btnLogin = new JButton("登录");
JButton btnReg = new JButton("注册");
JButton btnCancel = new JButton("取消");
//注册事件监听
QQLogin e = new QQLogin();
btnLogin.addActionListener(e);
btnReg.addActionListener(e);
btnCancel.addActionListener(e);
//布置输入面板
JPanel panInput = new JPanel();
panInput.setLayout(new GridLayout(2, 2));
panInput.add(labUser);
panInput.add(txtUser);
panInput.add(labPass);
panInput.add(txtPass);
//布置按钮面板
JPanel panButton = new JPanel();
panButton.setLayout(new FlowLayout());
panButton.add(btnLogin);
panButton.add(btnReg);
panButton.add(btnCancel);
//布置窗体
w.setLayout(new BorderLayout());
w.add(panInput, BorderLayout.CENTER);
w.add(panButton, BorderLayout.SOUTH);
w.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0){
if(arg0.getActionCommand().equals("登录")){
System.out.println("用户点击了登录");
}
if(arg0.getActionCommand().equals("注册")){
System.out.println("用户点击了注册");
}
if(arg0.getActionCommand().equals("取消")){
System.out.println("用户点击了取消");
}
}
}
运行结果:
图片.pngimport java.awt.*;
import javax.swing.*;
public class QQMain{
public static void main(String args[]){
JFrame w = new JFrame();
w.setSize(300, 400);
//new 组件
JTextField txtMess = new JTextField();
JComboBox cmbUser = new JComboBox();
JButton btnSend = new JButton("发送");
JTextArea txtContent = new JTextArea();
//设置滚动条
JScrollPane spContent = new JScrollPane(txtContent);
//布置小面板
JPanel panSmall = new JPanel();
panSmall.setLayout(new GridLayout(1,2));
panSmall.add(cmbUser);
panSmall.add(btnSend);
//布置大面板
JPanel panBig = new JPanel();
panBig.setLayout(new GridLayout(2, 1));
panBig.add(txtMess);
panBig.add(panSmall);
//布置窗体
w.setLayout(new BorderLayout());
w.add(panBig, BorderLayout.NORTH);
w.add(spContent, BorderLayout.CENTER);
w.setVisible(true);
}
}
图片.png
网友评论