2017-06-12 1 views
-1

私は、通知のための私自身のクラスを書いています(コードを気にしない、それはIntelliJのGUIデザイナで生成された)JWindowのアイデアは、プログラムが通知を示すことがある正しく

public class NotificationWindow extends JWindow { 

private JLabel icon; 
private JTextArea message; 
private JLabel title; 
private JPanel mainPanel; 
private Spacer spacer1; 
private Spacer spacer2; 

public NotificationWindow(String ttl, String msg) { 

    mainPanel = new JPanel(); 
    mainPanel.setLayout(new GridLayoutManager(3, 3, new Insets(5, 5, 5, 5), -1, -1)); 

    icon = new JLabel(); 
    icon.setIcon(new ImageIcon(getClass().getResource("/resources/employee 64.png"))); 

    title = new JLabel(ttl); 
    title.setFont(
      new Font(title.getFont().getName(), Font.BOLD, title.getFont().getSize())); 


    message = new JTextArea(3, 44); 
    message.setText(msg); 
    message.setOpaque(false); 
    message.setFont(message.getFont().deriveFont(12f)); 
    message.setLineWrap(true); 
    message.setWrapStyleWord(true); 

    spacer1 = new Spacer(); 
    spacer2 = new Spacer(); 

    this.pack(); 
    this.setSize(400, 80); 
    this.setAlwaysOnTop(true); 
    this.setVisible(true); 

    this.getRootPane().setBorder(
      BorderFactory.createMatteBorder(1, 1, 1, 1, Color.DARK_GRAY)); 

    Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize(); 

    Insets toolHeight = Toolkit.getDefaultToolkit() 
      .getScreenInsets(this.getGraphicsConfiguration()); 

    this.setLocation(
     scrSize.width - this.getWidth() - 20, 
     scrSize.height - toolHeight.bottom - this.getHeight() - 20); 

    this.initUI(); 
} 

private void initUI() { 

    mainPanel.add(icon, 
     new GridConstraints(0, 0, 3, 1, 
       GridConstraints.ANCHOR_CENTER, 
       GridConstraints.FILL_NONE, 
       GridConstraints.SIZEPOLICY_FIXED, 
       GridConstraints.SIZEPOLICY_FIXED, 
       null, null, null, 0, false)); 

    mainPanel.add(title, 
     new GridConstraints(0, 1, 1, 2, 
       GridConstraints.ANCHOR_WEST, 
       GridConstraints.FILL_NONE, 
       GridConstraints.SIZEPOLICY_FIXED, 
       GridConstraints.SIZEPOLICY_FIXED, 
       null, null, null, 0, false)); 

    mainPanel.add(message, 
     new GridConstraints(1, 1, 1, 2, 
       GridConstraints.ANCHOR_WEST, 
       GridConstraints.FILL_NONE, 
       GridConstraints.SIZEPOLICY_FIXED, 
       GridConstraints.SIZEPOLICY_FIXED, 
       null, null, null, 0, false)); 

    mainPanel.add(spacer1, 
     new GridConstraints(2, 1, 1, 1, 
       GridConstraints.ANCHOR_CENTER, 
       GridConstraints.FILL_VERTICAL, 
       1, 
       GridConstraints.SIZEPOLICY_WANT_GROW, 
       null, null, null, 0, false)); 

    mainPanel.add(spacer2, 
     new GridConstraints(2, 2, 1, 1, 
       GridConstraints.ANCHOR_CENTER, 
       GridConstraints.FILL_HORIZONTAL, 
       GridConstraints.SIZEPOLICY_WANT_GROW, 
       1, null, null, null, 0, false)); 


    this.setContentPane(mainPanel); 
    this.repaint(); 

    // time after which notification will be disappeared 
    new Thread(() -> { 
     try { 
      Thread.sleep(6000); 
      dispose(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    }).start(); 
} 
    } 

レンダリングされません。 1時間ごとにユーザーに通知します。この目的のために、私はScheduledExecutorServiceクラスを使用しています。問題は、通知ウィンドウが時々正しく表示しないことである

ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); 
     ses.scheduleAtFixedRate(new Runnable() { 
      private int i = 1; 
      @Override 
      public void run() { 
       NotificationWindow notificationWindow = 
        new NotificationWindow(
         "blah blah", 
         "Please, " + 
         "specify the details of the task you're currently working on."); 
      } 
     }, 10, 20, TimeUnit.SECONDS); 

:私はこの方法(テスト通知のために20秒ごとと呼ばれる)の通知を呼び出しています。場合によっては通知ウィンドウと同じサイズのフレームだけで、要素のない白い背景のみである必要があります。 私のミスはどこですか?

+0

を使用するEDT

  • の詳細を説明するために:上のセクションがありますか?それはSwingではないAndroidのレイアウトマネージャです。別のライブラリを使用していますか?私は個人的にはスウィングレイアウトマネージャーを使用しているだけで、それに問題はありません。 – markbernard

  • +0

    @markbernard、GridLayoutManagerは、デフォルトのIntelliJ IDEA GUIデザイナのレイアウトです。私は何も変えなかった。 – whyiamhere

    +0

    これは、Swingの外部のライブラリに依存していることを意味します。これは標準のLayoutManagerではありません。私は個人的に非標準のレイアウトを使用することを躊躇しています。 – markbernard

    答えて

    2

    時にはそれだけ通知ウィンドウと同じサイズでコードがEvent Dispatch Thread (EDT)上で実行されていない場合

    ランダムな問題が発生することができます任意の要素を除いただけ白い背景である必要があり、フレームの。

    私はScheduledExecutorServiceクラスを使用しています。

    スイングコンポーネントはEDTに作成する必要があります。 ScheduledExecutorServiceから呼び出されたコードはEDTで実行されません。

    代わりに、Swing Timerを使用してイベントをスケジュールする必要があります。

    Swing tutorialをお読みください。

    1. 同時実行Swingで - どのようにスイングタイマー
    2. から来GridLayoutManagerん
    +0

    これは完全に正確ではありません。 EDTにGUIを作成する必要はありません。彼らの活動はその後、EDT(絵画、イベントの発送など)によって管理されます。 – markbernard

    +0

    @markbernardこれは一度には当てはまりましたが、もはやそうではありませんでした。 https://stackoverflow.com/questions/491323/is-it-safe-to-construct-swing-awt-widgets-not-on-the-event-dispatch-threadを参照してください。 – VGR

    +0

    @VGRありがとうございました。私はこの変化を認識していませんでした。 – markbernard

    関連する問題