2016-08-03 44 views
0

私はBorderLayoutGridLayoutに基づいてGUIを作成しようとしていますが、フォームビルダーでは手書きではありません。そのため、私はコードの一部をスキップする可能性が最も高いです。ロードされていますが、見えません。Java Swing GUIが表示されない

import java.awt.*; 
import java.awt.event.*; 
import java.io.IOException; 
import java.net.InetAddress; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 


class PanelDemo { 

    JButton jbtnUpArrowFirst; 
    JButton jbtnDownArrowFirst; 
    JButton jbtnUpArrowSec; 
    JButton jbtnDownArrowSec; 
    JButton jbtnUpArrowThird; 
    JButton jbtnDownArrowThird; 

    JLabel jlblLedUpFirst; 
    JLabel jlblLedDownFirst; 
    JLabel jlblLedUpSec; 
    JLabel jlblLedDownSec; 
    JLabel jlblLedUpThird; 
    JLabel jlblLedDownThird; 

    private JComponent ui = null; 
    private Insets buttonMargin = new Insets(10,10,10,10); 

    PanelDemo() throws IOException { 

     InitComponents(); 
    } 

    private void InitComponents() { 

     if (ui != null) return; 

     ui = new JPanel(new BorderLayout(4,4)); 
     ui.setBorder(new EmptyBorder(4,4,4,4)); 

     int gap = 5; 


     JPanel jpnl = new JPanel(new GridLayout(2, 4, gap, gap)); 
     jpnl.setBorder(new EmptyBorder(0, 0, 0, 50)); 
     jpnl.setOpaque(true); 
     jpnl.setBorder(
       BorderFactory.createLineBorder(Color.BLUE)); 


     JPanel jpnl2 = new JPanel(new GridLayout(2, 2, gap, gap)); 
     jpnl2.setOpaque(true); 
     jpnl2.setBorder(
       BorderFactory.createLineBorder(Color.RED)); 





     jbtnUpArrowFirst = new JButton("upfirst" ,new ImageIcon("/Users/Jack/Desktop/up.png")); 
     jbtnUpArrowFirst.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jbtnUpArrowFirst.setHorizontalTextPosition(SwingConstants.CENTER); 

     jbtnDownArrowFirst = new JButton("downfirst" ,new ImageIcon("/Users/Jack/Desktop/down.png")); 
     jbtnDownArrowFirst.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jbtnDownArrowFirst.setHorizontalTextPosition(SwingConstants.CENTER); 


     jlblLedUpFirst = new JLabel("upledfirst", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER); 
     jlblLedUpFirst.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jlblLedUpFirst.setHorizontalTextPosition(SwingConstants.CENTER); 


     jlblLedDownFirst = new JLabel("downledfirst", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER); 
     jlblLedDownFirst.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jlblLedDownFirst.setHorizontalTextPosition(SwingConstants.CENTER); 


     //////////////////////////////////////////////////////////////////////// 
     jbtnUpArrowSec = new JButton("upsec", new ImageIcon("/Users/Jack/Desktop/up.png")); 
     jbtnUpArrowSec.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jbtnUpArrowSec.setHorizontalTextPosition(SwingConstants.CENTER); 

     jbtnDownArrowSec = new JButton("downsec" ,new ImageIcon("/Users/Jack/Desktop/down.png")); 
     jbtnDownArrowSec.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jbtnDownArrowSec.setHorizontalTextPosition(SwingConstants.CENTER); 


     jlblLedUpSec = new JLabel("upledsecond", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER); 
     jlblLedUpSec.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jlblLedUpSec.setHorizontalTextPosition(SwingConstants.CENTER); 

     jlblLedDownSec = new JLabel("downledsec", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER); 
     jlblLedDownSec.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jlblLedDownSec.setHorizontalTextPosition(SwingConstants.CENTER); 
     //////////////////////////////////////////////////////////////////////// 
     jbtnUpArrowThird = new JButton("upthirt", new ImageIcon("/Users/Jack/Desktop/up.png")); 
     jbtnUpArrowThird.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jbtnUpArrowThird.setHorizontalTextPosition(SwingConstants.CENTER); 

     jbtnDownArrowThird = new JButton("downthird" ,new ImageIcon("/Users/Jack/Desktop/down.png")); 
     jbtnDownArrowThird.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jbtnDownArrowThird.setHorizontalTextPosition(SwingConstants.CENTER); 


     jlblLedUpThird = new JLabel("upledthird", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER); 
     jlblLedUpThird.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jlblLedUpThird.setHorizontalTextPosition(SwingConstants.CENTER); 

     jlblLedDownThird = new JLabel("downledthird", new ImageIcon("/Users/Jack/Desktop/ledOff.png"), SwingConstants.CENTER); 
     jlblLedDownThird.setVerticalTextPosition(SwingConstants.BOTTOM); 
     jlblLedDownThird.setHorizontalTextPosition(SwingConstants.CENTER); 


     // Add the buttons and label to the panel. 
     jpnl.add(jbtnUpArrowFirst); 
     jpnl.add(jlblLedUpFirst); 
     jpnl.add(jbtnUpArrowSec); 
     jpnl.add(jlblLedUpSec); 
     jpnl.add(jbtnDownArrowFirst); 
     jpnl.add(jlblLedDownFirst); 
     jpnl.add(jbtnDownArrowSec); 
     jpnl.add(jlblLedDownSec); 

     jpnl2.add(jbtnUpArrowThird); 
     jpnl2.add(jlblLedUpThird); 
     jpnl2.add(jbtnDownArrowThird); 
     jpnl2.add(jlblLedDownThird); 


     ui.add(jpnl, BorderLayout.CENTER); 
     ui.add(jpnl2, BorderLayout.LINE_END); 

     JFrame jfrm = new JFrame("Use Two JPanels"); 
     jfrm.setLocationByPlatform(true); 
     jfrm.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     jfrm.setContentPane(getUI()); 
     jfrm.setMinimumSize(jfrm.getSize()); 
     jfrm.pack(); 
     jfrm.setVisible(true); 

     /*jbtnUpArrowFirst.addActionListener(e -> { 
      try { 
       test.send("ANTSW=VERT\r\n"); 

       //if (test.recv().equalsIgnoreCase("")) 

      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
     }); 

     jbtnDownArrowFirst.addActionListener(e -> { 
      try { 
       test.send("ANTSW=HORZ\r\n"); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
     });*/ 

    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String args[]) { 
     // Create the frame on the event dispatching thread. 
     SwingUtilities.invokeLater(() -> { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
        new PanelDemo(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (IllegalAccessException e) { 
        e.printStackTrace(); 
       } catch (InstantiationException e) { 
        e.printStackTrace(); 
       } catch (UnsupportedLookAndFeelException e) { 
        e.printStackTrace(); 
       } catch (ClassNotFoundException e) { 
        e.printStackTrace(); 
       } 
     }); 
    } 
} 
+0

あなたはいつかあなたのパネル上で "setVisible()"か何かを呼び出さないといけませんか? – GhostCat

+0

しかし、私はすでにJFrameの可視性を設定しているので、なぜ追加する必要がありますか? @GhostCat – swinger

+0

申し訳ありませんが、私はJFrameの部分を見落としました。 – GhostCat

答えて

4

あなたはJFrameのにすべてのコンポーネントを追加する前にpack()setVisible()を呼んでいます。そのブロックをコンストラクタの最後に移動します。

さらに、私のコンパイラは、アクションリスナを設定しているコードのどこにでもtestを見つけることができません。私はJava 8で作業していないので、何が間違っているのか分かりませんが、Eclipseはエラーなしでこれをコンパイルできるはずです。

+0

@swinger私はあなたが新しいユーザーだと見ています。あなたの質問に答えるならば、それが有益であればそれを上書きするだけでなく、答えのチェックマークをクリックするのが慣例です。こうして人々はその質問を知っていると答え、それに答えた人は信用を得ます。 – arcy

+0

このコードは私のシステムにフレームを表示します。私はeclipseのデフォルトパッケージの新しいクラスに貼り付け、クラス名を変更して、この質問から得た最初のものと衝突しないようにし、コンストラクタを修正して "debug"を押しました。 6つのボタンがあるウィンドウを表示します。あなたがこれを取得しない場合は、あなたのコード以外の何かのためです。エラーがある場合は、それらが何であるか教えてください。あなたが期待しているものと違うものが何であるかを述べれば、それはまた助けになるでしょう。最初の例は何も表示しないので、これが何かを表示したら私たちは完了したと思った。 – arcy

+0

質問にアイコンを追加した理由がわかりません。起こってはいけないことは何ですか? – arcy

関連する問題