2016-06-07 138 views
0

我使用GUI對象創建了一個簡單的用戶界面對象來輸入數據,但是每當我將中心面板添加到contentPane時,我的JLabels都消失了。此外,JTextFields,JComboBoxs和JButtons不響應點擊或輸入按鍵。如果我不添加centerPanel,或者添加它並使用1和1的寬度和高度參數來啓動小程序,那麼一切正常。將中心面板添加到我的內容窗格時GUI對象消失

當我伸展屏幕時,在將中央面板添加到正常運行配置後,對象將出現在初始窗口之外。我已經在列出的代碼之前將所有對象定義爲私有實例變量,所以這不是問題。請幫忙,我很困惑!

這裏是我的代碼:

public void begin() { 
    // creates the GUI Objects for the northPanel 
    northPanel = new JPanel(); 
    northPanel.setLayout(new GridLayout(1, 4)); 

    searchBox = new JTextField(); 
    searchBoxLabel = new JLabel("Search ID #:"); 
    search = new JButton("Search"); 
    search.addActionListener(this); 

    northPanel.add(searchBoxLabel); 
    northPanel.add(searchBox); 
    northPanel.add(search); 

    // creates the GUI OBjects for the southPanel 
    southDivider = new JPanel(); 
    southDivider.setLayout(new GridLayout(2, 1)); 

    southPanel = new JPanel(); 
    southPanel.setLayout(new GridLayout(1, 3)); 

    enter = new JButton("Enter"); 
    incrementInfo = new JButton("Increment ID"); 
    setCurrentTimeDate = new JButton("Current Time/Date"); 
    findRate = new JButton("Find Yield Rate"); 

    findRate.addActionListener(this); 
    enter.addActionListener(this); 
    incrementInfo.addActionListener(this); 
    setCurrentTimeDate.addActionListener(this); 

    southPanel.add(findRate); 
    southPanel.add(setCurrentTimeDate); 
    southPanel.add(incrementInfo); 
    southPanel.add(enter); 

    messageLabel = new JLabel("Welcome to the Stringer Application"); 

    southDivider.add(southPanel); 
    southDivider.add(messageLabel); 

    // create the GUI objects on the eastPanel 
    eastPanel = new JPanel(); 
    eastPanel.setLayout(new GridLayout(5, 2)); 

    cellType = new JComboBox(); 
    cellType.addItem("Rect"); 
    cellType.addItem("Cham"); 
    cellTypeLabel = new JLabel("Cell Type:"); 

    ecaCode = new JComboBox(); 
    ecaCode.addItem("A"); 
    ecaCode.addItem("B"); 
    ecaCodeLabel = new JLabel("ECA Code:"); 

    ecaSyringeNum = new JTextField(); 
    ecaSyringeNumLabel = new JLabel("Eca Syringe #:"); 

    passFail = new JComboBox(); 
    passFail.addItem("Pass"); 
    passFail.addItem("Fail"); 
    passFailLabel = new JLabel("Pass/Fail:"); 

    operator = new JTextField(); 
    operatorLabel = new JLabel("Operator:"); 
    cellType.addActionListener(this); 
    ecaCode.addActionListener(this); 
    passFail.addActionListener(this); 

    eastPanel.add(operatorLabel); 
    eastPanel.add(operator); 
    eastPanel.add(cellTypeLabel); 
    eastPanel.add(cellType); 
    eastPanel.add(ecaCodeLabel); 
    eastPanel.add(ecaCode); 
    eastPanel.add(ecaSyringeNumLabel); 
    eastPanel.add(ecaSyringeNum); 
    eastPanel.add(passFailLabel); 
    eastPanel.add(passFail); 

    // create the GUI objects on the westPanel 
    westPanel = new JPanel(); 
    westPanel.setLayout(new GridLayout(6, 2)); 

    yieldLabel = new JLabel("Current Yield:"); 
    yieldValueLabel = new JLabel("Select Date/Times"); 
    yieldAfterDate = new JTextField(); 
    yieldAfterTime = new JTextField(); 
    yieldBeforeDate = new JTextField(); 
    yieldBeforeTime = new JTextField(); 
    yieldAfterDateLabel = new JLabel("After Date:"); 
    yieldAfterTimeLabel = new JLabel("After Time:"); 
    yieldBeforeDateLabel = new JLabel("Before Date:"); 
    yieldBeforeTimeLabel = new JLabel("Before Time:"); 
    setBeforeToCurrentLabel = new JLabel("<html>'Set to Current' for <br> Current Date/Time</html>"); 
    fillBeforeWithCurrent = new JButton("Set to Current"); 
    fillBeforeWithCurrent.addActionListener(this); 

    westPanel.add(yieldLabel); 
    westPanel.add(yieldValueLabel); 
    westPanel.add(yieldAfterDateLabel); 
    westPanel.add(yieldAfterDate); 
    westPanel.add(yieldAfterTimeLabel); 
    westPanel.add(yieldAfterTime); 
    westPanel.add(yieldBeforeDateLabel); 
    westPanel.add(yieldBeforeDate); 
    westPanel.add(yieldBeforeTimeLabel); 
    westPanel.add(yieldBeforeTime); 
    westPanel.add(setBeforeToCurrentLabel); 
    westPanel.add(fillBeforeWithCurrent); 

    // create the GUI objects for the centerPanel 
    centerPanel = new JPanel(); 
    centerPanel.setLayout(new GridLayout(3, 4)); 


    date = new JTextField(getCurrentDate()); 
    dateLabel = new JLabel("Date:"); 
    time = new JTextField(getCurrentTime()); 
    timeLabel = new JLabel("Time:"); 
    stringID = new JTextField(); 
    stringIDLabel = new JLabel("String ID:"); 
    cellLot = new JTextField(); 
    cellLotLabel = new JLabel("Cell Lot #:"); 
    cellEff = new JTextField(); 
    cellEffLabel = new JLabel("Cell Eff:"); 
    comments = new JTextField(); 
    commentsLabel = new JLabel("Comments:"); 

    centerPanel.add(dateLabel); 
    centerPanel.add(date); 
    centerPanel.add(timeLabel); 
    centerPanel.add(time); 
    centerPanel.add(stringIDLabel); 
    centerPanel.add(stringID); 
    centerPanel.add(cellLotLabel); 
    centerPanel.add(cellLot); 
    centerPanel.add(cellEffLabel); 
    centerPanel.add(cellEff); 
    centerPanel.add(commentsLabel); 
    centerPanel.add(comments); 


    // add the panel's to the contentPane 
    Container contentPane = getContentPane(); 
    contentPane.setLayout(new BorderLayout()); 

    contentPane.add(centerPanel, BorderLayout.CENTER); 
    contentPane.add(northPanel, BorderLayout.NORTH); 
    contentPane.add(southDivider, BorderLayout.SOUTH); 
    contentPane.add(eastPanel, BorderLayout.EAST); 
    contentPane.add(westPanel, BorderLayout.WEST); 

    contentPane.validate(); 

} 
+1

您發佈的代碼片段很有趣,但它不是代碼,我們可以編譯,運行,測試,修改或糾正。請創建併發布有效的[mcve]。 –

回答

1

我已經測試你的代碼,它是工作,沒有任何問題。 您最有可能將組件添加到容器的其他位置,默認情況下,任何添加的組件都會移到中心位置,這會導致舊的中央面板被移除,並且擺動框架會繪製髒區域。 enter image description here

編輯:

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class Test2 extends JFrame { 
    public Test2() { 
     begin(); 
    } 
    public void begin() { 
     // creates the GUI Objects for the northPanel 
     JPanel northPanel = new JPanel(); 
     northPanel.setLayout(new GridLayout(1, 4)); 

     JTextField searchBox = new JTextField(); 
     JLabel searchBoxLabel = new JLabel("Search ID #:"); 
     JButton search = new JButton("Search"); 

     northPanel.add(searchBoxLabel); 
     northPanel.add(searchBox); 
     northPanel.add(search); 

     // creates the GUI OBjects for the southPanel 
     JPanel southDivider = new JPanel(); 
     southDivider.setLayout(new GridLayout(2, 1)); 

     JPanel southPanel = new JPanel(); 
     southPanel.setLayout(new GridLayout(1, 3)); 

     JButton enter = new JButton("Enter"); 
     JButton incrementInfo = new JButton("Increment ID"); 
     JButton setCurrentTimeDate = new JButton("Current Time/Date"); 
     JButton findRate = new JButton("Find Yield Rate"); 

     southPanel.add(findRate); 
     southPanel.add(setCurrentTimeDate); 
     southPanel.add(incrementInfo); 
     southPanel.add(enter); 

     JLabel messageLabel = new JLabel("Welcome to the Stringer Application"); 

     southDivider.add(southPanel); 
     southDivider.add(messageLabel); 

     // create the GUI objects on the eastPanel 
     JPanel eastPanel = new JPanel(); 
     eastPanel.setLayout(new GridLayout(5, 2)); 

     JComboBox cellType = new JComboBox(); 
     cellType.addItem("Rect"); 
     cellType.addItem("Cham"); 
     JLabel cellTypeLabel = new JLabel("Cell Type:"); 

     JComboBox ecaCode = new JComboBox(); 
     ecaCode.addItem("A"); 
     ecaCode.addItem("B"); 
     JLabel ecaCodeLabel = new JLabel("ECA Code:"); 

     JTextField ecaSyringeNum = new JTextField(); 
     JLabel ecaSyringeNumLabel = new JLabel("Eca Syringe #:"); 

     JComboBox passFail = new JComboBox(); 
     passFail.addItem("Pass"); 
     passFail.addItem("Fail"); 
     JLabel passFailLabel = new JLabel("Pass/Fail:"); 

     JTextField operator = new JTextField(); 
     JLabel operatorLabel = new JLabel("Operator:"); 

     eastPanel.add(operatorLabel); 
     eastPanel.add(operator); 
     eastPanel.add(cellTypeLabel); 
     eastPanel.add(cellType); 
     eastPanel.add(ecaCodeLabel); 
     eastPanel.add(ecaCode); 
     eastPanel.add(ecaSyringeNumLabel); 
     eastPanel.add(ecaSyringeNum); 
     eastPanel.add(passFailLabel); 
     eastPanel.add(passFail); 

     // create the GUI objects on the westPanel 
     JPanel westPanel = new JPanel(); 
     westPanel.setLayout(new GridLayout(6, 2)); 

     JLabel yieldLabel = new JLabel("Current Yield:"); 
     JLabel yieldValueLabel = new JLabel("Select Date/Times"); 
     JTextField yieldAfterDate = new JTextField(); 
     JTextField yieldAfterTime = new JTextField(); 
     JTextField yieldBeforeDate = new JTextField(); 
     JTextField yieldBeforeTime = new JTextField(); 
     JLabel yieldAfterDateLabel = new JLabel("After Date:"); 
     JLabel yieldAfterTimeLabel = new JLabel("After Time:"); 
     JLabel yieldBeforeDateLabel = new JLabel("Before Date:"); 
     JLabel yieldBeforeTimeLabel = new JLabel("Before Time:"); 
     JLabel setBeforeToCurrentLabel = new JLabel("<html>'Set to Current' for <br> Current Date/Time</html>"); 
     JButton fillBeforeWithCurrent = new JButton("Set to Current"); 

     westPanel.add(yieldLabel); 
     westPanel.add(yieldValueLabel); 
     westPanel.add(yieldAfterDateLabel); 
     westPanel.add(yieldAfterDate); 
     westPanel.add(yieldAfterTimeLabel); 
     westPanel.add(yieldAfterTime); 
     westPanel.add(yieldBeforeDateLabel); 
     westPanel.add(yieldBeforeDate); 
     westPanel.add(yieldBeforeTimeLabel); 
     westPanel.add(yieldBeforeTime); 
     westPanel.add(setBeforeToCurrentLabel); 
     westPanel.add(fillBeforeWithCurrent); 

     // create the GUI objects for the centerPanel 
     JPanel centerPanel = new JPanel(); 
     centerPanel.setLayout(new GridLayout(3, 4)); 

     JTextField date = new JTextField(); 
     JLabel dateLabel = new JLabel("Date:"); 
     JTextField time = new JTextField(); 
     JLabel timeLabel = new JLabel("Time:"); 
     JTextField stringID = new JTextField(); 
     JLabel stringIDLabel = new JLabel("String ID:"); 
     JTextField cellLot = new JTextField(); 
     JLabel cellLotLabel = new JLabel("Cell Lot #:"); 
     JTextField cellEff = new JTextField(); 
     JLabel cellEffLabel = new JLabel("Cell Eff:"); 
     JTextField comments = new JTextField(); 
     JLabel commentsLabel = new JLabel("Comments:"); 

     centerPanel.add(dateLabel); 
     centerPanel.add(date); 
     centerPanel.add(timeLabel); 
     centerPanel.add(time); 
     centerPanel.add(stringIDLabel); 
     centerPanel.add(stringID); 
     centerPanel.add(cellLotLabel); 
     centerPanel.add(cellLot); 
     centerPanel.add(cellEffLabel); 
     centerPanel.add(cellEff); 
     centerPanel.add(commentsLabel); 
     centerPanel.add(comments); 

     // add the panel's to the contentPane 
     Container contentPane = getContentPane(); 
     contentPane.setLayout(new BorderLayout()); 

     contentPane.add(centerPanel, BorderLayout.CENTER); 
     contentPane.add(northPanel, BorderLayout.NORTH); 
     contentPane.add(southDivider, BorderLayout.SOUTH); 
     contentPane.add(eastPanel, BorderLayout.EAST); 
     contentPane.add(westPanel, BorderLayout.WEST); 

//  contentPane.validate(); 
     setSize(812, 514); 

    } 

    public static void main(String[] args) { 
     Test2 t=new Test2(); 

//  t.begin(); 
     t.setVisible(true); 
    } 
} 
+0

謝謝賈拉!我檢查了但我的代碼不是這樣,我只在上面的begin方法中添加組件。我想知道爲什麼它不能在我的工作中使用:/你是否介意發佈你的編譯器的完整代碼(包括類簽名,導入等)以查看這可能是否允許一些清晰? – ckoch

+0

大多數你是最受歡迎的! 如果您認爲它已解決,請接受答案。 –