Swing随机抛出NullPointer @每次执行不同的时间

时间:2015-04-19 22:18:27

标签: java swing

我有一个两个类项目,一个类读取一个文件,并根据网站检查所述文件中的每个条目,并将返回数据发布到另一个文件中。

如果返回数据为真(例如),则文件中的数据点将在屏幕上闪烁。此功能有效。

我通过while循环中的以下if语句调用它。

if (!query.text().contains("unavailable") && !query.text().contains("at least 3 characters long to acquire.") && line != null) {

            HitBox h = new HitBox(line); //GUI Class.

            fos.write(query.text().getBytes());
            fos.write("\n".getBytes());

            fos.flush();

        }

以下是我的GUI类。

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;

public class HitBox {
    private Timer t;
    JWindow frame = new JWindow();
    public HitBox(String s) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager
                            .getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                frame.setAlwaysOnTop(true);

                t = new Timer(1000 * 5, new ActionListener() {
                    public void actionPerformed(ActionEvent e2) {
                        SwingUtilities.getWindowAncestor(frame.getComponent(0))
                        .dispose();
                    }
                });

            }

        });
        frame.setBackground(new Color(0, 0, 0, 0));
        TranslucentPane tp = new TranslucentPane(s);
        frame.setContentPane(tp);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        t.start();

    }


}
class TranslucentPane extends JPanel {

    public TranslucentPane(String s) {
        add(new JLabel(s));
        setOpaque(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setComposite(AlphaComposite.SrcOver.derive(0.85f));
        g2d.setColor(getBackground());
        g2d.fillRect(0, 0, getWidth(), getHeight());


    }

}

使用同一组输入数据会发生这种情况,即使我覆盖了Web查询,只返回一个设定值,@在运行时的随机点,JWindow将出现,将抛出一个nullpointer(@ my call of我的计时器对象的start方法。)

这让我相信我错误地实施了计时器;我对如何使用一致的数据和返回感兴趣,它会引发空指针的变化。

Exception in thread "main" java.lang.NullPointerException
at HitBox.<init>(HitBox.java:51)
at OriginalGangster.main(OriginalGangster.java:38)

1 个答案:

答案 0 :(得分:4)

您没有在GUI线程上启动GUI。您需要将其移动到Runnable中,并确保在构建之后启动Timer

如,

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.Timer;
import javax.swing.UIManager;

public class TestHitBox {
   public static void main(String[] args) {
      String text = "Hello world! This is Hovercraft!";
      int seconds = 5;
      float composite = 0.85f;
      float points = 48f;
      HitBox.showMessage(text, seconds, composite, points);
   }
}

class HitBox {

   public static void showMessage(final String text, final int seconds, final float composite, final float points) {
      EventQueue.invokeLater(new Runnable() {
         @Override
         public void run() {
            try {
               UIManager.setLookAndFeel(UIManager
                     .getSystemLookAndFeelClassName());
            } catch (Exception ex) {
               ex.printStackTrace();
            }
            final JWindow frame = new JWindow();
            frame.setBackground(new Color(0, 0, 0, 0));
            TranslucentPane tp = new TranslucentPane(text, composite, points);
            frame.setContentPane(tp);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            frame.setAlwaysOnTop(true);
            new Timer(1000 * seconds, new TimerListener(frame)).start();
         }
      });
   }
}

class TimerListener implements ActionListener {
   private JWindow frame;

   public TimerListener(JWindow frame) {
      this.frame = frame;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      frame.dispose();
      ((Timer) e.getSource()).stop();
   }
}

@SuppressWarnings("serial")
class TranslucentPane extends JPanel {
   private float composite;

   public TranslucentPane(String s, float composite, float points) {
      this.composite = composite;
      JLabel label = new JLabel(s);
      label.setFont(label.getFont().deriveFont(Font.BOLD, points));
      add(label);
      setOpaque(false); // this breaks a rule
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g.create();
      g2d.setComposite(AlphaComposite.SrcOver.derive(composite));
      g2d.setColor(getBackground());
      g2d.fillRect(0, 0, getWidth(), getHeight());
      g2d.dispose(); // dispose of any graphics we create
   }
}

代码已更新。

  • 主代码已被移动到公共静态方法,因为这似乎是向用户显示消息而不与任何其他代码共享状态的代码,类似于JOptionPane消息,所以我正在制作它表现得像一个JOptionPane。
  • Timer的ActionListener移出构造函数以保持清洁。
  • 添加了字母组合,显示时间和消息字体磅值的参数。