实际上我正在开发一个项目,按钮点击产生对话,并通过Thread.sleep(5000)在5秒内处理它 我在运行方法
下写了以下内容void showpopup(String p, String t) throws IOException {
JPanel app = new JPanel();
popup = new JFrame();
// app.setSize(300,400);
GridBagConstraints c1 = new GridBagConstraints();
app.setLayout(new GridBagLayout());
app.setVisible(true);
app.setBackground(new Color(39, 170, 225));
popup.setUndecorated(true);
popup.setSize(300, 100);
popup.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
JLabel header = new JLabel();
header.setText(p);
c1.gridx = 0;
c1.gridy = 0;
c1.weightx = 1.0f;
c1.weighty = 1.0f;
c1.insets = new Insets(5, 5, 5, 5);
c1.fill = GridBagConstraints.BOTH;
header.setFont(new Font("comfortaa", Font.BOLD, 15));
app.add(header, c1);
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 0.3f;
constraints.weighty = 0.3f;
constraints.fill = GridBagConstraints.BOTH;
popup.add(app, constraints);
BufferedImage Butico = ImageIO.read(new File("C:/Users/utkarsh/Desktop/close.png"));
JButton close = new JButton(new ImageIcon(Butico));
close.setContentAreaFilled(false);
close.setBorder(BorderFactory.createEmptyBorder());
c1.gridx = 1;
c1.weightx = 0f;
c1.weighty = 0f;
c1.insets = new Insets(5, 5, 5, 5);
c1.fill = GridBagConstraints.NONE;
c1.anchor = GridBagConstraints.NORTH;
close.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
popup.dispose();
}
});
app.add(close, c1);
JPanel cont = new JPanel(new GridBagLayout());
// cont.setSize(arg0, arg1);
String a = "hello";
JLabel text = new JLabel("<HtML>" + t);
text.setAlignmentX(Component.LEFT_ALIGNMENT);
cont.setVisible(true);
cont.setBackground(new Color(241, 242, 242));
c1.gridx = 0;
c1.gridy = 0;
c1.weightx = 1.0f;
c1.weighty = 1.0f;
c1.insets = new Insets(5, 5, 5, 5);
c1.anchor = GridBagConstraints.NORTH;
c1.fill = GridBagConstraints.BOTH;
cont.add(text, c1);
constraints.gridx = 0;
constraints.gridy += 1;
constraints.weightx = 2.5f;
constraints.weighty = 2.5f;
constraints.fill = GridBagConstraints.BOTH;
popup.add(cont, constraints);
text.setFont(new Font("Comfortaa", Font.PLAIN, 12));
text.setForeground(Color.DARK_GRAY);
header.setForeground(Color.WHITE);
popup.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new Color(188, 188, 188)));
popup.setVisible(true);
Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
Insets th = Toolkit.getDefaultToolkit().getScreenInsets(popup.getGraphicsConfiguration());
popup.setLocation(s.width - popup.getWidth() - 5, s.height - th.bottom - popup.getHeight() - 5);
popup.setAlwaysOnTop(true);
new Thread() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
popup.dispose();
}
}
}.start();
}
现在当我点击按钮一旦它工作正常但是多按钮点击多个Jdialog弹出窗口出现 并且只有最后一个在5s之后被处理
那么为了关闭每个JDialog框,我必须做出哪些改变
答案 0 :(得分:2)
Popup不是此方法的本地
popup = new JFrame();
因为这段代码不是线程安全的。
每次单击时,您都将用新创建的popup
对象替换popup.dispose();
对象,因此import android.content.Context;
import android.graphics.Matrix;
import android.widget.ImageView;
/**
* ImageView to display top-crop scale of an image view.
*
* @author Chris Arriola
*/
public class TopCropImageView extends ImageView {
public TopCropImageView(Context context) {
super(context);
setScaleType(ScaleType.MATRIX);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
recomputeImgMatrix();
}
@Override
protected boolean setFrame(int l, int t, int r, int b) {
recomputeImgMatrix();
return super.setFrame(l, t, r, b);
}
private void recomputeImgMatrix() {
final Matrix matrix = getImageMatrix();
float scale;
final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
final int drawableWidth = getDrawable().getIntrinsicWidth();
final int drawableHeight = getDrawable().getIntrinsicHeight();
if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
scale = (float) viewHeight / (float) drawableHeight;
} else {
scale = (float) viewWidth / (float) drawableWidth;
}
matrix.setScale(scale, scale);
setImageMatrix(matrix);
}
}
将丢弃最后一个。