2013-12-23 72 views
0

我正在開發一個程序。我創建了一個JFrame,其中包含一組面板,按鈕,標籤和文本框,這些框架應該位於其中。出於某種原因,JFrame發生了變化,但內部沒有任何東西。代碼如下:JFrame中不會顯示圖形元素(Java)

import javax.swing.*; 
import java.awt.*; 
import java.awt.Event.*; 

public class GUI extends JFrame { 

    JButton rect,oval,tri,free,addPoint; 
    JLabel xLabel,yLabel; 
    JTextField xTextField,yTextField; 
    JPanel leftPanel,rightPanel,optionsPanel,pointsPanel; 

    public GUI(){ 
     initUI(); 
    } 

    private void initUI(){ 

     setLayout(new GridLayout(1,2,5,5)); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setTitle("Graphics Generator"); 
     setSize(500,500); 
     setVisible(true); 

     add(leftPanel); 
     add(rightPanel); 

     leftPanel.setLayout(new GridLayout(2,1,5,5)); 
     leftPanel.add(optionsPanel); 

     optionsPanel.setLayout(new GridLayout(1,4,2,2)); 

     rect = new JButton("Rectangle"); 
     oval = new JButton("Oval"); 
     tri = new JButton("Triangle"); 
     free = new JButton("Free Shape"); 

     optionsPanel.add(rect); 
     optionsPanel.add(oval); 
     optionsPanel.add(tri); 
     optionsPanel.add(free); 

     leftPanel.add(pointsPanel); 

     pointsPanel.setLayout(new GridLayout(1,5,2,2)); 

     pointsPanel.add(xLabel); 
     pointsPanel.add(xTextField); 
     pointsPanel.add(yLabel); 
     pointsPanel.add(yTextField); 
     pointsPanel.add(addPoint); 

    } 

    public static void main(String[] args) { 

     GUI gui = new GUI(); 

    } 

} 

回答

1

您的面板,文本框或標籤都沒有被初始化。我得到NullPointerException

下面的代碼將讓你的程序運行。但你真的需要尋找到一些LayoutManagers

private void initUI(){ 
    leftPanel = new JPanel(); 
    rightPanel = new JPanel(); 
    optionsPanel = new JPanel(); 
    pointsPanel = new JPanel(); 

    yLabel = new JLabel(); 
    xLabel = new JLabel(); 

    xTextField = new JTextField(); 
    yTextField = new JTextField(); 

    add(leftPanel); 
    add(rightPanel); 

    leftPanel.setLayout(new GridLayout(2,1,5,5)); 
    leftPanel.add(optionsPanel); 

    optionsPanel.setLayout(new GridLayout(1,4,2,2)); 

    rect = new JButton("Rectangle"); 
    oval = new JButton("Oval"); 
    tri = new JButton("Triangle"); 
    free = new JButton("Free Shape"); 
    addPoint = new JButton("Add Point"); 

    optionsPanel.add(rect); 
    optionsPanel.add(oval); 
    optionsPanel.add(tri); 
    optionsPanel.add(free); 

    leftPanel.add(pointsPanel); 

    pointsPanel.setLayout(new GridLayout(1,5,2,2)); 

    pointsPanel.add(xLabel); 
    pointsPanel.add(xTextField); 
    pointsPanel.add(yLabel); 
    pointsPanel.add(yTextField); 
    pointsPanel.add(addPoint); 

    setLayout(new GridLayout(1,2,5,5)); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setTitle("Graphics Generator"); 
    setSize(500,500); 
    setVisible(true); 

} 
1

你應該調用setVisible(true)大功告成加入您的組件後。除非有電話聯繫revalidate();

您還需要在嘗試使用它們之前初始化您的組件。

實施例:

leftPanel = new JPanel(); 
rightPanel = new JPanel(); 
add(leftPanel); 
add(rightPanel); 

重複用於其他組件。

5
  • JComponents不會被初始化,

  • 您添加JComponents已經可見JFrame

  • 你必須移動代碼行setVisible(true);來構造年底,

  • Swing GUI應該在Initial Thread