gpt4 book ai didi

java - 如何在方法中写入一些 'pause'?

转载 作者:行者123 更新时间:2023-12-02 05:07:11 26 4
gpt4 key购买 nike

这里我有一个方法

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() 是一个标志,让我们知道用户选择了他想要的颜色。

问题是 - 如果我像这样使用它Color c = pickColor(); 在 c 中将写入 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());
}
}
}

最佳答案

这里的问题是,您的方法 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 具有哪些功能,但如果它不提供此功能,那么您可以将其子类化并覆盖用户接受或取消时调用的任何方法。

关于java - 如何在方法中写入一些 'pause'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27682314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com