如何写一些'暂停'在方法?

时间:2014-12-29 01:20:33

标签: java

这里有一个方法

public static Color pickColor(){
    final aero.colorpicker.Frame frame = new aero.colorpicker.Frame();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (!frame.isSelected()) {
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
    return frame.getColorPicked();
}

isSelected()是一个标志,让我们知道用户选择了他想要的颜色。

问题是 - 如果我喜欢它 c中的Color c = pickColor();将写出aero.colorpicker.Frame类的默认(黑色)颜色 我需要暂停,等待选择。

package aero.colorpicker;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Aero on 28.12.2014.
 */
public class Frame extends JFrame {
    //MAINFIELD
    private Color colorPicked = Color.black;
    private boolean selected = false;

    //GUIFIELDS
    private JPanel rightPanel;
    private JLabel R;
    private JLabel G;
    private JLabel B;
    private JTextField RData;
    private JTextField GData;
    private JTextField BData;
    private JPanel RPanel;
    private JPanel GPanel;
    private JPanel BPanel;
    private ColorPanel colorPanel;
    private JButton pick;
    private BufferedImage colors;
    private JLabel imageLabel;

    public Frame(){
        initFrame();
        setVisible(true);
    }

    private void initComponents(){
        rightPanel = new JPanel();
        R = new JLabel("R");
        G = new JLabel("G");
        B = new JLabel("B");
        RData = new JTextField();
        GData = new JTextField();
        BData = new JTextField();
        RPanel = new JPanel();
        GPanel = new JPanel();
        BPanel = new JPanel();
        colorPanel = new ColorPanel();
        pick = new JButton("Pick");
    }

    private void addListeners(){
        RData.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getRed();
                colorPanel.repaint();
            }
        });
        GData.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getGreen();
                colorPanel.repaint();
            }
        });
        BData.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getBlue();
                colorPanel.repaint();
            }
        });
        pick.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                getRed();
                getBlue();
                getGreen();
                Frame.this.setVisible(false);
            }
        });
        imageLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                int x, y;
                x = e.getX();
                y = e.getY();
                setColorPicked(new Color(colors.getRGB(x, y)));
                colorPanel.repaint();
                RData.setText(Integer.toString(getColorPicked().getRed()));
                BData.setText(Integer.toString(getColorPicked().getBlue()));
                GData.setText(Integer.toString(getColorPicked().getGreen()));
            }
        });
    }

    private void getRed(){
        int r;
        try {
            r = Integer.parseInt(RData.getText());
        }catch (NumberFormatException nfe){
            RData.setText("0");
            r = 0;
        }
        setColorPicked(r, getColorPicked().getGreen(), getColorPicked().getBlue());
    }

    private void getGreen(){
        int g;
        try {
            g = Integer.parseInt(GData.getText());
        }catch (NumberFormatException nfe){
            GData.setText("0");
            g = 0;
        }
        setColorPicked(getColorPicked().getRed(), g, getColorPicked().getBlue());
    }

    private void getBlue(){
        int b;
        try {
            b = Integer.parseInt(BData.getText());
        }catch (NumberFormatException nfe){
            BData.setText("0");
            b = 0;
        }
        setColorPicked(getColorPicked().getRed(), getColorPicked().getGreen(), b);
    }

    private void initFrame(){
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setLayout(new BorderLayout());
        this.setSize(369, 194);
        this.setTitle("Color picker");
        this.setResizable(false);
        initComponents();
        InputStream input = Frame.class.getClassLoader().getResourceAsStream("colorGradient.jpg");
        try {
            colors = ImageIO.read(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        imageLabel = new JLabel(new ImageIcon(colors));
        this.add(imageLabel, BorderLayout.CENTER);
        rightPanel.setLayout(new GridLayout(5, 1));
        RPanel.setLayout(new GridLayout(1, 2));
        GPanel.setLayout(new GridLayout(1, 2));
        BPanel.setLayout(new GridLayout(1, 2));
        RPanel.add(R);
        RPanel.add(RData);
        GPanel.add(G);
        GPanel.add(GData);
        BPanel.add(B);
        BPanel.add(BData);
        rightPanel.add(RPanel);
        rightPanel.add(GPanel);
        rightPanel.add(BPanel);
        rightPanel.add(colorPanel);
        rightPanel.add(pick);
        this.add(rightPanel, BorderLayout.EAST);
        this.repaint();
        addListeners();
    }

    public Color getColorPicked() {
        return colorPicked;
    }

    private void setColorPicked(Color colorPicked) {
        this.colorPicked = colorPicked;
    }

    private void setColorPicked(int r, int g, int b){
        this.colorPicked = new Color(r, g, b);
    }

    private void setSelected(){
        selected = true;
    }

    public boolean isSelected(){
        return selected;
    }

    private class ColorPanel extends JPanel{
        public void paintComponent(Graphics g){
            g.setColor(colorPicked);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

你的问题是你的方法pickColor创建了一个等待挑选颜色,启动它然后退出的线程。换句话说,pickColor不等待线程完成。线程本身完成后什么都不做。

一种解决方案是在退出方法之前等待线程完成。然而,这仍然是糟糕的设计。您(实际上很可能)在线程处理UI事件(即调度线程)中调用pickColor是可能的,这会使UI无响应。它还使用了不必要的忙等待。

如果这是您想要做的,那么您将需要了解如何创建新的事件队列。您还需要了解如何使用信号量来阻止线程。我将在这里提供代码,但我不建议在不了解您正在做的事情的情况下将其转储到您的应用程序中。这被设计为对话框类的子类,以便可以覆盖用于关闭对话框的方法以通知被阻塞的线程。

对于显示颜色选择对话框的方法:

public synchronized Color pickColor() {
    // code to create and show the dialog
    EventQueue tempEventQueue = new EventQueue();
    Toolkit.getDefaultToolkit().getSystemEventQueue().push(tempEventQueue);
    try {
        wait();
    } catch (InterruptedException ex) {
        // stop waiting on interrupt
    } finally {
        tempEventQueue.pop();
    }
    // return colour from dialog
}

对于用户关闭对话框时调用的方法:

public synchronized void closeChooser() {
    notifyAll();
    super.closeChooser(); // or whatever it's called
}

正确的解决方案是使用两种方法:一种是打开颜色选择对话框,另一种是在对话框关闭时调用的方法。我不知道aero.colorpicker.Frame有什么功能,但是如果它没有提供这个功能,那么你可以将它子类化并覆盖用户接受或取消时调用的任何方法。