简体   繁体   English

我怎样才能使这个timer()正常工作?

[英]How can I make this timer() work as I want?

I am having a bit of trouble with the code before. 我之前的代码有点麻烦。 It is doing what I want, but I can only call the method once, if I call it more than that it won't work, essentially it only works once. 它正在执行我想要的操作,但是我只能调用一次该方法,如果我调用它的次数超出了它的工作范围,那么本质上它只会运行一次。 So right now this ballG is an object. 所以现在这个ballG是一个对象。 It starts off as White, it sets it to green, has a delay, then sets it back to white, this is perfect. 它以白色开始,将其设置为绿色,具有延迟,然后将其设置为白色,这是完美的。 The problem is since I do timerG.start(); 问题是因为我做了timerG.start(); and I don't end it, when I call it again it doesn't work. 而且我没有结束它,当我再次调用它不起作用时。 If I do timerG.stop(); 如果我做timerG.stop(); it ignores the action event and leaves it green without going back to white. 它会忽略动作事件,并使其变为绿色,而不会回到白色。 How can I make it so I can call this method multiple times??? 我该如何做才能多次调用此方法???

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.Timer;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextPane;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Font;
import java.util.concurrent.TimeUnit;

import javax.swing.JLayeredPane;
import javax.swing.JEditorPane;


public class frame extends JFrame {

private JPanel contentPane;
private JTextField textEntered;
private JTextArea displayT;
private JTextPane textPane;
private JLabel instructions;
private JLabel instructions3;
private JLabel instructions2;
private JLabel ballR;
private JLabel ballG;
private JLabel ballB;
private JPanel panel;
private JTextArea textArea;
private JLabel instructions4;

public String translator(String x)
{
    String y = "";
    for(int i = 0; i< x.length(); i++)
    {
        y = y + code(x.charAt(i)) + " ";
    }
    return y;

}

private String code(char a)
{

    switch (a) {
    case 'e' :  return "R"; 
    case 't' : return "B"; 
    case 'a' : return "G"; 
    case 'o' : return "RB"; 
    case 'i' : return "RG"; 
    case 'n':  return "BG"; 
    case 's' :  return "R-R"; 
    case 'h' :  return "R-B"; 
    case 'r' :  return "R-G"; 
    case 'd' :  return "R-RB"; 
    case 'l' :  return "R-RG"; 
    case 'c' : return "R-BG"; 
    case 'u' : return "B-R"; 
    case 'm' : return "B-B"; 
    case 'w' : return "B-G"; 
    case 'f' :  return "B-RB"; 
    case 'g' :  return "B-RG"; 
    case 'y' :  return "B-BG"; 
    case 'p' : return "G-R"; 
    case 'b' : return "G-B"; 
    case 'v':  return "G-B"; 
    case 'k' : return "G-RB"; 
    case 'j' :  return "G-RG"; 
    case 'x' :  return "G-BG"; 
    case 'q' :  return "RB-R"; 
    case 'z' :  return "RB-G"; 
    case ' ' :  return "RB-B";  
    case '0' : return "RB-RB"; 
    case '1' : return "RB-RG"; 
    case '2' :  return"RB-BG"; 
    case '3' : return "RG-R"; 
    case '4'  :return "RG-B"; 
    case '5' : return "RG-G"; 
    case '6' :  return "RG-RB"; 
    case '7' :  return "RG-RG"; 
    case '8' :  return "RG-BG"; 
    case '9' :  return "BG-R";      
    }
    return "Z";

}

//Trying to get 1/4 of these methods to work, colorRed, colrGreen, colorBlue,setWhite
private void colorRed()
{

    Timer timerR = new Timer(750, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            ballR.setForeground(Color.RED);//or RED, depending
        }
    });
    //timer.setRepeats(false);//don't repeat if you don't want to
    timerR.start();
    ballR.setForeground(Color.WHITE);

}

private void colorGreen()
{

    ballG.setForeground(Color.GREEN);
    Timer timerG = new Timer(750, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            ballG.setForeground(Color.WHITE);//or RED, depending


        }
    });
    timerG.setRepeats(false);
    timerG.start();


}

private void colorBlue()
{
    Timer timerB = new Timer(750, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            ballB.setForeground(Color.BLUE);//or RED, depending
        }
    });
    //timer.setRepeats(false);
    timerB.start();

    ballR.setForeground(Color.WHITE);
}

private void setWhite()
{
    Timer timer = new Timer(750, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
             ballB.setForeground(Color.WHITE);
             ballR.setForeground(Color.WHITE);
             ballG.setForeground(Color.WHITE);
        }
    });
    //timer.setRepeats(false);
    timer.start();

}





public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                frame frame = new frame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public frame() {
    setTitle("RBG Translator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 800, 529);
    contentPane = new JPanel();
    contentPane.setBackground(Color.GRAY);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    textEntered = new JTextField();
    textEntered.setText("Enter String...");
    textEntered.setBounds(10, 124, 261, 50);
    contentPane.add(textEntered);
    textEntered.setColumns(10);

    JButton submit = new JButton("Submit");
    submit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            ballR.setForeground(Color.WHITE);
            ballB.setForeground(Color.WHITE);
            ballG.setForeground(Color.WHITE);
            String input = textEntered.getText().toLowerCase();
            String output = translator(input);
            displayT.setText(output);
            //colorRed();
            colorGreen();

            //colorRed();
            //colorBlue();
            colorGreen();
            colorGreen();



        }
    });
    submit.setBounds(281, 125, 138, 50);
    contentPane.add(submit);

    displayT = new JTextArea();
    displayT.setBounds(10, 246, 409, 234);
    displayT.setLineWrap(true);
    contentPane.add(displayT);

    instructions = new JLabel("Please enter a word or a phrase that you would like to be transalted\r\n");
    instructions.setBounds(10, 11, 396, 14);
    contentPane.add(instructions);

    instructions3 = new JLabel("Below is a translated text of the word or phrase submitted");
    instructions3.setBounds(10, 205, 409, 34);
    contentPane.add(instructions3);

    instructions2 = new JLabel("into RBG code. Currently A-Z, 0-9, and space are permitted.");
    instructions2.setBounds(10, 24, 396, 34);
    contentPane.add(instructions2);

    panel = new JPanel();
    panel.setBounds(470, 25, 304, 169);
    contentPane.add(panel);

    ballR = new JLabel("\u2022");
    panel.add(ballR);
    ballR.setForeground(Color.RED);
    ballR.setFont(new Font("Tahoma", Font.PLAIN, 99));

    ballB = new JLabel("\u2022");
    panel.add(ballB);
    ballB.setForeground(Color.BLUE);
    ballB.setFont(new Font("Tahoma", Font.PLAIN, 99));

    ballG = new JLabel("\u2022");
    panel.add(ballG);
    ballG.setForeground(Color.GREEN);
    ballG.setFont(new Font("Tahoma", Font.PLAIN, 99));

    textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setColumns(4);
    textArea.setText("a:G   k:G-RB  u:B-R   4:RG-B            "  
            +       "b:G-B  l:R-RG  v:G-G   5:RG-G            "
            +       "c:R-BG m:B-B   w:B-G   6:RG-RB          "
            +       "d:R-RB n:BG    x:G-BG  7:RG-RG          "   
            +       "e:R    o:RB    y:B-BG  8:RG-BG          "
            +       "f:B-RB p:G-R   z:RB-G  9:BG-R            "                      
                +   "g:B-RG q:RB-R  0:RB-RB  :RB-B              "
                +   "h:R-B  r:R-G   1:RB-RG                                        "
                +   "i:RG   s:R-R   2:RB-BG                                        "
            +   "j:G-RG t:B              3:RG-R                    "   );

    textArea.setBounds(429, 246, 345, 234);
    contentPane.add(textArea);

    instructions4 = new JLabel("Key\r\n");
    instructions4.setBounds(429, 205, 345, 34);
    contentPane.add(instructions4);








}

} }

(I really don't know what your "codes" are doing, as R-RB doesn't seem to make sense, how do you display R, G and B?) (我真的不知道您的“代码”在做什么,因为R-RB似乎没有意义,您如何显示R,G和B?)

Okay, you need to start by generating some kind of sequence that you want displayed. 好的,您需要首先生成要显示的某种序列。 This will allow the Timer to act as a pseudo loop and update the UI according to the current sequence set 这将允许Timer充当伪循环并根据当前序列集更新UI

private String[] sequence;
private int index;
private Timer timer;
//...
public Test() {
    //...
    JButton submit = new JButton("Submit");
    submit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            ballR.setForeground(Color.WHITE);
            ballB.setForeground(Color.WHITE);
            ballG.setForeground(Color.WHITE);
            String input = textEntered.getText().toLowerCase();
            String output = translator(input);
            displayT.setText(output);

            timer.stop();

            System.out.println(output);
            List<String> firstPass = new ArrayList<>(Arrays.asList(output.split(" ")));
            List<String> fullPass = new ArrayList<>(25);
            for (String pass : firstPass) {
                if (pass.contains("-")) {
                    String[] parts = pass.split("-");
                    fullPass.addAll(Arrays.asList(parts));
                } else {
                    fullPass.add(pass);
                }
            }

            sequence = fullPass.toArray(new String[fullPass.size()]);
            index = 0;
            timer.start();

        }
    });

Basically, this assumes that each space is a separate sequence to be displayed 基本上,这假设每个空格都是要显示的单独序列

Next, you need to setup a single Timer which can play through the sequences 接下来,您需要设置一个可以播放序列的Timer

timer = new Timer(750, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        ballR.setForeground(Color.WHITE);
        ballG.setForeground(Color.WHITE);
        ballB.setForeground(Color.WHITE);
        if (index < sequence.length) {
            String set = sequence[index];
            System.out.println(index + " = " + set);
            for (char c : set.toCharArray()) {
                if ('R' == c) {
                    ballR.setForeground(Color.RED);
                } else if ('G' == c) {
                    ballG.setForeground(Color.GREEN);
                } else if ('B' == c) {
                    ballB.setForeground(Color.BLUE);
                }
            }
        } else {
            timer.stop();
        }
        index++;
    }
});

Okay, so, this takes the next String sequence and breaks it down into invidual elements, by spiting the String on the - character (which is why you "codes" don't make sense to me). 好的,因此,通过将String放在-字符上,这将获取下一个String序列并将其分解为单个元素(这就是为什么“编码”对我而言没有意义)。 It then loops through this set and changes the state of the "balls" accordingly. 然后,它遍历此集合并相应地更改“球”的状态。

The Timer continues` until there are no more sequences left... Timer继续进行,直到没有剩余的序列了。

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. 避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。 There are too many factors which affect the individual size of components, none of which you can control. 有太多因素会影响组件的单个大小,您无法控制。 Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来尝试纠正

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM