2016-07-17 137 views
0

我知道这已被问到其他线程,但我看不出我的代码是什么问题。我的JPanel没有显示在我的JFrame中。请协助。JFrame不会显示

这是一个入门级的计算机科学学校作业,所以有些愚蠢的东西是我无法控制的。

谢谢。

package view; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 

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

public class DequeueView { 

    //main JFrame 
    private JFrame mainFrame ; 

    //Containers 
    private JPanel display; 
    private JPanel buttons; 

    //Buttons 
    private JButton addFront; 
    private JButton dequeue; 
    private JButton enqueue; 
    private JButton removeRear; 

    //Textbox and label 
    private JTextField displayQueue; 
    private JLabel greeting; 

    /** 
    * Constructor for the main application with the title bar set to 
    * a title. 
    */ 
    public DequeueView(){ 

     /* 
     * The hierarchy is as follows: 
     * 
     * Container JPanel- to display the message "Hello" and to display queue. 
     *  Label , textbox 
     * Container JPanel- to put the buttons. 
     *  JButtons 
     */ 

     JPanelDisplayHelper();  //formats the displays. 
     JPanelButtonHelper();  //formats the buttons. 
     JFrameHelper();    //formats the JFrame. 

    } 

    /** 
    * Sets up the JFrame that will house the 4 buttons. 
    */ 
    private void JPanelButtonHelper(){ 

     addFront = new JButton("Add to Front"); 
     dequeue = new JButton("Take from Front"); 
     enqueue = new JButton("Add to Back"); 
     removeRear = new JButton("Take from Back"); 

     buttons = new JPanel(); 
     buttons.add(addFront); 
     buttons.add(dequeue); 
     buttons.add(enqueue); 
     buttons.add(removeRear);   
    } 

    /** 
    * Sets up the JFrame that will house everything. 
    */ 
    private void JFrameHelper(){ 

     String title = new String ("Queue Application"); 

     mainFrame = new JFrame(); 
     mainFrame.setLocation(200,150); 
     mainFrame.setSize(500, 500); 
     mainFrame.setResizable(false); 
     mainFrame.setTitle(title); 
     mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     mainFrame.add(display); 
     mainFrame.add(buttons); 
     mainFrame.setLayout(new BorderLayout()); 
     mainFrame.setVisible(true); 

    } 

    /** 
    * Sets up the JPanel that will display the greeting and the queue in 
    * String form. 
    */ 
    private void JPanelDisplayHelper(){ 

     String text = new String("Hello, this app displays a queue. Enjoy!"); 

     //formats the label 
     greeting = new JLabel(text, JLabel.LEFT); 
     greeting.setVisible(true); 


     //formats the JTextField. 
     displayQueue = new JTextField(10); 
     displayQueue.setText("Your Queue will display here!"); 
     displayQueue.setBackground(Color.WHITE); 
     displayQueue.setEditable(false); 
     displayQueue.setVisible(true); 
     displayQueue.validate(); 

     display = new JPanel(); 
     display.add(greeting); 
     display.add(displayQueue); 
     display.setLayout(new BorderLayout()); 


    } 

} 

回答

0

你是否像这样在你的主要方法中实例化了类?它不知道它在你的程序的主要方法,除非你真正定义它

public static void main(String[] args) { 
    new DequeueView(); 
} 

PS:你可以添加标题参数的JFrame框架内=新的JFrame(“标题”)

+0

如果您需要更多的澄清,这只是标记我,我认为自己在JFrames中是相当不错的 –