java - 如何点击按钮,重新运行(我是初学者)?
PHP中文网
PHP中文网 2017-04-18 10:55:50
0
1
798

下面是我全部的代码:

package day0411;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import java.util.Random;

/**
 * Created by Administrator on 2017-4-11.
 */
public class Farme1 {
    public static void main(String[] args) {
        //创建窗口对象
        Frame f1 = new Frame();
        Button btn = new Button("OOO!");f1.setVisible(true);                //显示窗口对象。
        f1.setBounds(200,500,550,350);      //设置位置
        f1.setTitle("我的窗口程序!");         //设置窗口标题。
        f1.setIconImage(Toolkit.getDefaultToolkit().getImage("F:\\L-我的图片\\logo\\eyes-1059234.jpg"));//设置窗体左上角图标
        f1.setLayout(null);//制作界面时用的的布局函数
        btn.setBackground(Color.gray);
        btn.setSize(50, 30);
        btn.setLocation(10,30);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NewButton(f1);
            }
        });
        f1.add(btn);
        CloseFrame(f1);
    }
    public static void CreatFrame(Frame f1,Button btn){

    }
    public static int Random1(){
        //随机函数生成0-255之间的数字、
        Random rd = new Random();
        int a= rd.nextInt(256);
        return a;
    }
    public static void NewButton(Frame f1){
        int x = 50;
        int y= 100;
        int count = 0;
        for (int i=0;i<9;i++){
            Button bt1 = new Button("BT"+(i+1));
            //随机颜色。
            bt1.setBackground(new Color(Random1(),Random1(),Random1()));
            bt1.setBounds(x, y, 50, 30);
            f1.add(bt1);
            x+=60;
            count++;
            if (count==3){
                y += 50;
                count =0;
                x=50;
                //System.out.println("AAAA");
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    }
    public static void CloseFrame(Frame f1){
        f1.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

我想到的是

boolean a =false;
if(!a){
    ...
    a=flase;
}else{
    ...
    a=true
}"
但是这样好像并不对。。
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
迷茫

You can use a List to save the currently existing Buttons with random colors, and then each time you click the button, remove the Button above the Frame in the List.

/**
 * 用来保存那些颜色随机的 Button
 */
static java.util.List<Button> newButtons = new ArrayList<>();

public static void NewButton(final Frame f1) {

    // 每次添加之前,先移除原来的颜色随机按钮
    for (Button button : newButtons) {
        f1.remove(button);
    }

    int x = 50;
    int y = 100;
    int count = 0;
    for (int i = 0; i < 9; i++) {
        Button bt1 = new Button("BT" + (i + 1));
        //随机颜色。
        bt1.setBackground(new Color(Random1(), Random1(), Random1()));
        bt1.setBounds(x, y, 50, 30);
        f1.add(bt1);
        x += 60;
        count++;
        if (count == 3) {
            y += 50;
            count = 0;
            x = 50;
            //System.out.println("AAAA");
        }

        newButtons.add(bt1); // 将 bt1 加入 newButtons

        try {
            Thread.sleep(200);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!