我一直在处理一个小java应用程序,我想在应用程序的根窗格的glasspane上添加一个等待的图形,下面是类:
public class WaitPanel extends JPanel {
public WaitPanel() {
this.setLayout(new BorderLayout());
JLabel label = new JLabel(new ImageIcon("spin.gif"));
this.setLayout(new BorderLayout());
this.add(label, BorderLayout.CENTER);
this.setOpaque(false);
this.setLayout(new GridBagLayout());
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
me.consume();
Toolkit.getDefaultToolkit().beep();
}
});
}
public void paintComponent(Graphics g) {
g.setColor(new Color(0, 0, 0, 140));
g.fillRect(0, 0, getWidth(), getHeight());
}}而主修班:
public class NewJFrame extends JFrame {
public NewJFrame() {
JButton button =new JButton("Click");
getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getRootPane().setGlassPane(new WaitPanel());
getRootPane().getGlassPane().setVisible(true);
}
});
}但是,当我将按钮操作更改为:
getRootPane().setGlassPane(new WaitPanel());
getRootPane().getGlassPane().setVisible(true);
Scanner sc=new Scanner(System.in);
String s=sc.next();
getRootPane().getGlassPane().setVisible(false);它不起作用。
发布于 2016-01-03 23:50:13
您的问题(其中之一)是代码使用基于System.in的扫描仪冻结Swing事件线程,这样可以防止GUI更新图形,包括它的玻璃窗格。解决办法--别那么做。如果要阻止或暂停GUI,请使用Swing计时器或JOptionPane。
例如,您可以更改
getRootPane().setGlassPane(new WaitPanel());
getRootPane().getGlassPane().setVisible(true);
Scanner sc=new Scanner(System.in);
String s=sc.next();
getRootPane().getGlassPane().setVisible(false);像这样的事情:
getRootPane().setGlassPane(new WaitPanel());
getRootPane().getGlassPane().setVisible(true);
int delay = 4 * 1000; // 4 second delay
new javax.swing.Timer(delay, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getRootPane().getGlassPane().setVisible(false);
((javax.swing.Timer) e).stop();
}
}).start();https://stackoverflow.com/questions/34582939
复制相似问题