怎样用JAVA的GUI(图形用户界面)来设计一个程序!求程序和详解。_百度...

发布网友 发布时间:2022-04-22 22:52

我来回答

2个回答

热心网友 时间:2022-04-15 09:37

//正好以前注释过一个
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.MemoryImageSource;
import java.util.Random;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.Timer;
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, BorderLayout.CENTER);
        Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
        Image image = defaultToolkit.createImage(new MemoryImageSource(0, 0,
                null, 0, 0));
        Cursor invisibleCursor = defaultToolkit.createCustomCursor(image,
                new Point(0, 0), "cursor");
        setCursor(invisibleCursor);
        KeyPressListener keyPressListener = new KeyPressListener();
        this.addKeyListener(keyPressListener);
        this.setAlwaysOnTop(true);
        this.setUndecorated(true);
        this.getGraphicsConfiguration().getDevice().setFullScreenWindow(this);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setVisible(true);
        screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        lines = screenSize.height / gap;       //用gap来分行
        columns = screenSize.width / gap;   //用gap来分列
        posArr = new int[columns + 1];  
        random = new Random();
        for (int i = 0; i < posArr.length; i++) {
            posArr[i] = random.nextInt(lines); //随机行数
        }
        new Timer(100, this).start();
    }
    private char getChr() {
        return (char)  (random.nextInt(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 < screenSize.width; x += gap) {
                int endPos = posArr[currentColumn];      //获得开始行位置
                g2d. setColor(Color. GREEN);
                g2d.drawString(String.valueOf(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;
                    }
                    g2d.setColor(new Color(0, cg, 0));
                    g2d.drawString(String.valueOf(getChr()), x, j * gap);    //画出随机行后的15行字符,颜色从黑色渐变成绿色,逐行增加
                }
                posArr[currentColumn] += random.nextInt(5);      //下落距离最快为4, 会产生加速下落的感觉
                if ((posArr[currentColumn] -5)* gap > getHeight()) {  //如果行数位置大于屏幕高度,从新获取合适的行
                    posArr[currentColumn] = random.nextInt(lines);
                }
                currentColumn++;    //下一列获取随机行
            }
        }
    }
    private class KeyPressListener extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() ==KeyEvent.VK_ESCAPE) {   //监听键盘事件,按ESC按键退出
                System.exit(0);
            }
        }
    }
}

热心网友 时间:2022-04-15 10:55

//程序由Guess类组成,是一个猜数字的小游戏。
//多给点财富吧
//Guess.java
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Guess extends JFrame
{
public static void main(String[] args)
{
Guess g=new Guess("猜数字游戏");
}
private void reset()
{
kaishi.setEnabled(true);
jishi.setText("");
tishi.setText("");
tick=0;
}
private void processInput()
{
int n=Integer.parseInt(input.getText());

if(n<suijishu)
{
if(n>currentmin)
{
currentmin=n+1;
}
tishi.setText(String.format("小了,范围在%1$s-%2$s之间",currentmin,currentmax));
}
else if(n>suijishu)
{
if(n<currentmax)
{
currentmax=n-1;
}
tishi.setText(String.format("大了,范围在%1$s-%2$s之间",currentmin,currentmax));
}
else
{
JOptionPane.showMessageDialog(this,"恭喜你,猜对了!再来一次吧!","恭喜恭喜",JOptionPane.INFORMATION_MESSAGE);
timer1.stop();
reset();
}
}
Guess(String t)
{
super(t);
setLayout(new FlowLayout());
jishi=new JLabel();
tishi=new JLabel();
input=new JTextField(10);
kaishi=new JButton("开始猜");
timer1=new javax.swing.Timer(Guess.HaoMiao,null);
c=Calendar.getInstance();
input.getDocument().addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
//processInput();
}
public void insertUpdate(DocumentEvent e)
{
processInput();
}
public void removeUpdate(DocumentEvent e)
{
//processInput();
}
}
);
timer1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//秒数加1
tick++;
c.setTimeInMillis(1000*tick);
//提示已经用了的时间
jishi.setText(String.format("%1$s:%2$s",c.get(Calendar.MINUTE),c.get(Calendar.SECOND)));
}
});
kaishi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
kaishi.setEnabled(false);
input.setText("");
//产生一个范围在Min-Max之间的随机整数
suijishu=(int)(Math.random()*(Guess.Max-Guess.Min+1))+Guess.Min;
currentmin=Guess.Min;
currentmax=Guess.Max;
tishi.setText(String.format("请猜一个范围在%1$d-%2$d之间的数字",Guess.Min,Guess.Max));
timer1.start();
}
});
add(jishi);
add(input);
add(tishi);
add(kaishi);
setSize(500,80);
//使窗口显示在屏幕*
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private static final int Min=1;
private static final int Max=500;
private static final int HaoMiao=1000;
private JLabel jishi,tishi;
private JTextField input;
private JButton kaishi;
private javax.swing.Timer timer1;
private Calendar c;
private int suijishu,currentmin,currentmax;
private long tick=0;
}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com