1

我必须创建一个应用程序,它基本上必须在用户设置的未来时刻打开一个闪烁的窗口,或者在第二种情况下,延迟 1-5 秒,由用户决定。下面是我尝试编写的代码,但我坚持创建事件处理程序和操作。以及如何以时间的写入格式解析来自用户的输入。而且我不确定我是否应该使用这些线程。




import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import javax.swing.*;

import static javax.swing.JFrame.EXIT_ON_CLOSE;




public class MyTimer {
    public static void main(String[] args) {
        Object[] options = {"Settings",
                "Close"};
        JFrame f = new JFrame();

        f.setLayout(new GridLayout(6, 3));
        f.setSize(500, 800);

        f.setLocationRelativeTo(null);
        Hashtable<String, String> values = new Hashtable<>();
        values.put("  :  :  ","On time :");
        values.put("  ","Countdown (seconds)");


        ButtonsAdd.addPair(values, f);

        f.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JButton ChooseColor= new JButton("Choose Color");
        JLabel lbl1= new JLabel("No color  was selected");
        lbl1.setSize(150, 150);
        f.add(lbl1);
        f.add(ChooseColor);
        JLabel lbl2= new JLabel("Speed");
        f.add(lbl2);
        Choice speed = new Choice();
        speed.setSize(150, 150);
        speed.setBackground(Color.LIGHT_GRAY);
        speed.add("1");
        speed.add("2");
        speed.add("3");
        speed.add("4");
        speed.add("5");
        String n ;

        if (speed.getSelectedItem().equals("1")) {
            n = String.valueOf("1");
        }

        if (speed.getSelectedItem().equals("2")) {
            n = String.valueOf("2");
        }
        if (speed.getSelectedItem().equals("3")) {
            n = String.valueOf("3");
        }

        if (speed.getSelectedItem().equals("4")) {
            n = String.valueOf("4");
        }
        if (speed.getSelectedItem().equals("5")) {
            n = String.valueOf("5");
        }
        f.add(speed);
        ButtonGroup sTsT = new ButtonGroup();
        JRadioButton Start = new JRadioButton("Start");
        JRadioButton Stop  = new JRadioButton("Stop");
        Start.setSelected(true);
        Stop.setSelected(false);
        sTsT.add(Start);
        sTsT.add(Stop);


        f.add(Start);
        f.add(Stop);
        JColorChooser jcc= new JColorChooser();
        ChooseColor.setActionCommand("hello");

        JDialog dialog = JColorChooser.createDialog(null, "Choose color", true,jcc, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Color c = jcc.getColor();
                lbl1.setForeground(c);
                lbl1.setText("The color you chose");
            }
        }, null);

        Start.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                JFrame b = new JFrame();
                b.setVisible(true);
              b.setBackground(Color.LIGHT_GRAY);
                b.setSize(500, 800);


            }

        });
        ActionListener myAl = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand().equals("hello"))
                    dialog.setVisible(true);


            }
        };
        ChooseColor.addActionListener(myAl);




        int answer = JOptionPane.showOptionDialog(null, "Choose option", "Option dialog", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,null, options, options[0]);


        if (answer == 0){


            f.setVisible(true);
            f.setBackground(Color.ORANGE);}
    }}
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.*;


public class ButtonsAdd {public static void addPair(Hashtable<String,String> values, JFrame f) {
    Enumeration e = values.keys();
    while(e.hasMoreElements()) {
        Object key = e.nextElement();
        JTextField tf = new JTextField(key.toString());
        ButtonGroup myChoices = new ButtonGroup();
        JRadioButton OnTime = new JRadioButton(values.get(key));
        JRadioButton Countdown  = new JRadioButton(values.get(key));
        OnTime.setSelected(true);

        myChoices.add(OnTime);
        myChoices.add(Countdown);
        f.getContentPane().add(OnTime);
        f.getContentPane().add(tf);

    }
}
}


4

0 回答 0