我知道其他线程已经问过这个问题,但我看不出我的代码有什么问题。我的JPanel没有出现在我的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 :(得分:0)
你是否已经在这个主方法中实现了类?除非你真正定义它,否则它不会在程序的主方法中知道它
public static void main(String[] args) {
new DequeueView();
}
PS:你可以在JFrame frame = new JFrame中添加title参数(" title")