在Linux上使用modal dialog时,显示繁忙游标的Swing渲染问题【JAVA教程】

!
也想出现在这里? 联系我们
信息

在Linux上使用modal dialog时,显示繁忙游标的Swing渲染问题,第1张

概述在Linux上使用modal dialog时,显示繁忙游标的Swing渲染问题

在closures模式对话框后,在应用程序框架的玻璃面板上设置忙碌光标时,并不总是显示忙碌光标。 有时候它是有效的(第一次大多数情况下总能正常工作),有时则不行。

更好的是,在打开对话框之前设置忙光标。 显示繁忙的光标,但在内部移动鼠标时,然后在对话框外面,忙状态光标不再显示。

请注意,我只在linux上观察到以下错误。 在Mac OS X或windows上,行为是确定性的并且一致。

另一个提示,在代码示例的第一种情况下,当鼠标不进入对话框并且使用键盘selectYES_OPTION时,总是显示繁忙的鼠标光标。 此外,在这种情况下,玻璃窗格上的“请稍候…”标签也不会被绘制。

如何使Java Swing应用程序显示Compiz鼠标插件(Ubuntu)的鼠标光标效果

让用户select应用程序来打开文件

不可resize的窗口边框和位置

从windows任务计划程序运行Java GUI应用程序不起作用

Java – Swing GUI在windows 7中呈现不正确

这里展示了这些错误的SSCCE:

import java.awt.event.*; import javax.swing.*; public class TestFrame extends JFrame { private JPanel panel; private JPanel glasspane; public TestFrame() { final Jbutton button1 = new Jbutton(new AbstractAction(\”Start activity indicator after closing the dialog\”) { @OverrIDe public voID actionPerformed(ActionEvent e) { doAction1(); } }); final Jbutton button2 = new Jbutton(new AbstractAction(\”Start activity indicator before opening the dialog\”) { @OverrIDe public voID actionPerformed(ActionEvent e) { doAction2(); } }); panel = new JPanel(); panel.add(button1); panel.add(button2); getContentPane().add(panel,borderLayout.norTH); glasspane = (JPanel) getGlasspane(); glasspane.setLayout(new borderLayout()); glasspane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); glasspane.add(new JLabel(\”Please Wait…\”),borderLayout.CENTER); setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); setSize(800,600); setVisible(true); } public voID doAction1() { System.out.println(\”IsstartingInEDT?: \”+ SwingUtilitIEs.isEventdispatchThread()); final int response = JOptionPane.showConfirmDialog(this,\”Click on the YES_OPTION,busy indicator must start (if it does,try again).\”); if (JOptionPane.YES_OPTION == response) { startActivity(); for (int i = 0; i < 5; i++) { try { Thread.sleep(200); } catch (Exception e) { e.printstacktrace(); } } stopActivity(); } } public voID doAction2() { startActivity(); System.out.println(\”IsstartingInEDT?: \”+ SwingUtilitIEs.isEventdispatchThread()); final int response = JOptionPane.showConfirmDialog(this,\”Move the mouse insIDe the dialog (me) and then outsIDe,the busy indicator is not shown anymore\”); if (JOptionPane.YES_OPTION == response) { for (int i = 0; i < 5; i++) { try { Thread.sleep(200); } catch (Exception e) { e.printstacktrace(); } } } stopActivity(); } public voID startActivity() { System.out.println(\”TestFrame.startActivity()\”); glasspane.setVisible(true); } public voID stopActivity() { System.out.println(\”TestFrame.stopActivity()\”); glasspane.setVisible(false); } /** * @param args */ public static voID main(String[] args) { new TestFrame(); } }

目前我在JavaBUG游行中没有发现任何相关的问题。 在打开一个新的之前,我会进一步search。

我也已经阅读了下面的文章,但是从一个非modal dialog中做出一个好的模态对话并不是很直接: http : //www.javaspeciaLists.eu/archive/Issue065.HTML

任何人都可以提供帮助吗? 先谢了,皮埃尔

防止closuresJava swing应用程序

为什么在linux中,Jbutton,JLabel鼠标点击丢失或被忽略? 它没有做该计划应该如何做

IE9简单的点击/滚动问题

在linux中可以使用9位串行通信吗?

在windows中创buildJava Swing接受用户inputpath(从资源pipe理器复制粘贴path)

这里有一些线程问题。

IsstartingInEDT是真的吗?

如果是的话,你做错了,因为:

你不应该睡在UI线程。 这将停止屏幕更新。

如果不是,你做错了,因为:

OptionPane.showConfirmDialog()必须从UI线程中调用。

你应该这样做:

public voID doAction1() { if (!SwingUtilitIEs.isEventdispatchThread()) { System.err.println(\”error,must be edt\”); return; } final int response = JOptionPane.showConfirmDialog(this,try again).\”); if (JOptionPane.YES_OPTION == response) { startActivity(); // change glass panel in edt // new thread for long standing task new Thread( new Runnable() { public voID run() { for (int i = 0; i < 5; i++) { try { Thread.sleep(200); } catch (Exception e) { e.printstacktrace(); } } SwingUtilitIEs.invokeAnDWait(new Runnable(){ public voID run() { // changing glass panel need edt stopActivity(); }); }).start(); } }

1。 通过使用Tread.sleep(int)很好地阻止EDT,所有关于Swing并发性的问题都被描述了出来

2.nd工作,因为JOptionPane的初始化创建一个新的EDT

下面是关于….的简单示范,请仅举例说明,并确保这是针对所有Swing规则,但在EDT期间通过用法Tread.sleep(int)示范性地锁定和解锁EDT

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class ShakeComponents1 { private JFrame frame = new JFrame(); private final String items[] = {\”One\”,\”Two\”,\”Three\”}; private Timer timer; private JPanel panel = new JPanel(); private JPanel buttonPanel = new JPanel(); private Jbutton button = new Jbutton(\” Exit \”); private boolean repeats = true; private boolean runs = false; private color clr[] = {color.red,color.blue,color.magenta}; private Insets initmargin; public static voID main(String[] args) { SwingUtilitIEs.invokelater(new Runnable() { @OverrIDe public voID run() { new ShakeComponents1().makeUI(); } }); } public voID makeUI() { buttonPanel = new JPanel(); buttonPanel.setborder(new Emptyborder(5,5,5)); buttonPanel.setLayout(new borderLayout()); button.setPreferredSize(new Dimension(100,45)); button.setForeground(color.darkGray); button.addActionListner(new ActionListner() { @OverrIDe public voID actionPerformed(ActionEvent event) { Runnable doRun = new Runnable() { @OverrIDe public voID run() { System.exit(0); } }; SwingUtilitIEs.invokelater(doRun); } }); button.addMouseListner(new java.awt.event.MouseListner() { @OverrIDe public voID mouseClicked(MouseEvent e) { } @OverrIDe public voID mousepressed(MouseEvent e) { } @OverrIDe public voID mouseReleased(MouseEvent e) { } @OverrIDe public voID mouseEntered(MouseEvent e) { if (runs) { SwingUtilitIEs.invokelater(new Runnable() { @OverrIDe public voID run() { runs = false; timer.stop(); changePnlborder(new Emptyborder(5,5)); changeBtnForegroung(color.darkGray); } }); } } @OverrIDe public voID mouseExited(MouseEvent e) { if (!runs) { timer.start(); runs = true; } } }); buttonPanel.add(button); final Insets margin = button.getmargin(); panel.add(buttonPanel); for (int i = 0; i < 2; i++) { JComboBox combo = new JComboBox(items); combo.setMinimumSize(new Dimension(50,25)); combo.setMaximumSize(new Dimension(150,25)); combo.setPreferredSize(new Dimension(100,25)); combo.addActionListner(new ShakeAction()); panel.add(combo); } frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.pack(); frame.setLocation(50,50); frame.setVisible(true); timer = new Timer(500,new ShakeAction()); timer.setRepeats(repeats); initmargin = button.getmargin(); } private class ShakeAction extends AbstractAction { private static final long serialVersionUID = 1L; private int nocolor = 0; private border border; private int count = 0; @OverrIDe public voID actionPerformed(ActionEvent e) { timer.start(); if (count > 5) { new Thread(new Runnable() { @OverrIDe public voID run() { try { Thread.sleep(500); changeBtnForegroung(color.darkGray); Thread.sleep(500); count = 0; Thread.sleep(750); } catch (Exception e) { System.out.println(e); } } }).start(); } else { new Thread(new Runnable() { @OverrIDe public voID run() { try { runs = true; if (nocolor < 2) { nocolor++; changeBtnForegroung(clr[nocolor]); } else { nocolor = 0; changeBtnForegroung(clr[nocolor]); } changeBtnmargin(new Insets(initmargin.top,initmargin.left + 10,initmargin.bottom,initmargin.right – 10)); border = new Emptyborder(0,10,5); changePnlborder(border); Thread.sleep(100); changeBtnmargin(new Insets(initmargin.top,initmargin.left – 10,initmargin.right + 10)); border = new Emptyborder(0,10); changePnlborder(border); Thread.sleep(100); changeBtnmargin(new Insets(initmargin.top,initmargin.right – 10)); border = new Emptyborder(5,0); changePnlborder(border); Thread.sleep(100); changeBtnmargin(new Insets(initmargin.top,initmargin.right + 10)); border = new Emptyborder(10,initmargin.left,initmargin.right)); border = new Emptyborder(5,5); changePnlborder(border); Thread.sleep(100); count++; } catch (Exception e) { System.out.println(e); } } }).start(); } } } private voID changePnlborder(final border b) { SwingUtilitIEs.invokelater(new Runnable() { @OverrIDe public voID run() { buttonPanel.setborder(b); buttonPanel.revalIDate(); buttonPanel.repaint(); } }); } private voID changeBtnForegroung(final color c) { SwingUtilitIEs.invokelater(new Runnable() { @OverrIDe public voID run() { button.setForeground(c); } }); } private voID changeBtnmargin(final Insets margin) { SwingUtilitIEs.invokelater(new Runnable() { @OverrIDe public voID run() { button.setmargin(margin); } }); } }

结论 – >你可以创建新的Thread作为Backgroung任务包装成Runnable ,如果你想模拟LongRunning任务和Thread.sleep(int),也许回答你的问题在这里

确定正确的方法是使用SwingWorker ,Thread.sleep(int)也是如此

总结

以上是内存溢出为你收集整理的在Linux上使用modal dialog时,显示繁忙游标的Swing渲染问题全部内容,希望文章能够帮你解决在Linux上使用modal dialog时,显示繁忙游标的Swing渲染问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

© 版权声明
THE END
喜欢就支持一下吧
点赞104 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容