2013-07-19 63 views
1

好嗎?我的問題是相當簡單的,所以我懷疑生病需要添加任何代碼,但我會如果需要的話。GUI不顯示內容

每當我創建一個GUI框架,並添加了幾個板,以它和運行我的應用程序,內容不顯示,直到我可以重新調整窗口大小或將其最小化工具欄上,然後將其還原。這可能是什麼原因,我該如何解決它?

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingConstants; 
public final class Calculator extends JFrame 
{ 
    //initialise various variables for use within the program 
    //BUTTONS 
    private final JButton additionButton = new JButton("+"); 
    private final JButton subtractionButton = new JButton("-"); 
    private final JButton divisionButton = new JButton("/"); 
    private final JButton multiplicationButton = new JButton("*");  

    //PANELS 
    private JPanel operatorPanel; 
    private JPanel operandPanel; 

    //LABELS 
    private JLabel operationLabel;  

    //constructor to initialise the frame and add components into it 
    public Calculator() 
    { 
     super("Clancy's Calculator"); 
     setLayout(new BorderLayout(5, 10)); 
     setSize(370, 200); 
     setResizable(false); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setLocationRelativeTo(null); 
     setVisible(true); 

     //create a message label to display the operation that has just taken place 
     operationLabel = new JLabel("YOU HAVE PERFORMED SOME OPERATION",SwingConstants.CENTER); 

     add(getOperatorPanel(), BorderLayout.NORTH); 
     add(getOperandPanel(), BorderLayout.CENTER); 
     add(operationLabel, BorderLayout.SOUTH); 
    } 

    //setter method for the operator panel 
    public void setOperatorPanel() 
    { 
     operatorPanel = new JPanel(); 
     operatorPanel.setLayout(new FlowLayout()); 

     operatorPanel.add(additionButton); 
     operatorPanel.add(subtractionButton); 
     operatorPanel.add(multiplicationButton); 
     operatorPanel.add(divisionButton); 
    } 
    //getter method for the operator panel 
    public JPanel getOperatorPanel() 
    { 
     setOperatorPanel(); 
     return operatorPanel; 
    } 

    //setter method for operands panel 
    public void setOperandPanel() 
    { 
     operandPanel = new JPanel(); 
     operandPanel.setLayout(new GridLayout(3, 2, 5, 5)); 

     //LABELS 
     JLabel operandOneLabel = new JLabel("Enter the first Operand: "); 
     JLabel operandTwoLabel = new JLabel("Enter the second Operand: "); 
     JLabel answerLabel = new JLabel("ANSWER: "); 

     //TEXT FIELDS 
     JTextField operandOneText = new JTextField(); //retrieves one operand 
     JTextField operandTwoText = new JTextField(); //retrieves another operand 
     JTextField answerText = new JTextField(); //displays answer 

     answerText.setEditable(false); //ensure the answer field is not editable 

     operandPanel.add(operandOneLabel); 
     operandPanel.add(operandOneText); 
     operandPanel.add(operandTwoLabel); 
     operandPanel.add(operandTwoText); 
     operandPanel.add(answerLabel); 
     operandPanel.add(answerText); 

    } 
    //getter method for operand panel 
    public JPanel getOperandPanel() 
    { 
     setOperandPanel(); 
     return operandPanel; 
    } 

    /** main method */ 
    public static void main(String[] args) 
    { 
     new Calculator(); 
    } 
} 
+0

,如果你添加一些代碼... – StephenTG

+0

如果這是擺只是調用'重繪()'添加面板 – chancea

+0

後良好,沒有任何代碼,我的猜測是這可能是好的,您的JFrame尺寸不適合您添加到它的面板。 –

回答

0

我注意到你編程設置新的佈局管理器。無論何時添加,刪除或更改的Java GUI,需要調用invalidate()revalidate()使Java重新創建GUI。看看調用invalidate()setVisible(true)修復問題