2016-08-03 102 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我看到你是一個新用戶;如果它回答你的問題,那麼通常在SO中點擊複選標記,除了在有幫助的情況下對它進行提升。這樣人們就知道這個問題得到了回答,而回答問題的人也獲得了信任。 – arcy

+0

此代碼在我的系統上顯示一個框架。我將它粘貼到eclipse中默認包中的新類中,更改了類名,以便它不會與我從此問題獲得的第一個類發生衝突,更正構造函數,然後點擊「調試」。它顯示一個帶有6個按鈕的窗口。如果你沒有得到這個,那是因爲你的代碼以外的東西。如果你有錯誤,告訴我們他們是什麼。如果你陳述你的期望和你看到的不同,這也會有所幫助。因爲第一個例子不會顯示任何東西,所以一旦顯示了一些我認爲我們已經完成的東西。 – arcy

+0

我不明白你爲什麼給這個問題添加圖標。你想發生什麼事情沒有發生? – arcy