Java编程,JFrame问题

时间:2015-01-16 21:02:26

标签: java swing interface jframe

我正在尝试使用各种不同的JPanel创建一个接口,但是出于某种原因,我遇到了这个错误。错误位于代码的底部。这是设置我的框架可见。

public class GUIExampleApp extends JFrame  implements ActionListener {
    JLabel Title, Description;
    JButton Start, Help, Quit; 
    TextField Limiting; 
    JPanel panelContainer = new JPanel (true);
    JPanel StartApplication = new JPanel (true);
    JPanel StartingApplication = new JPanel (true);

    CardLayout card = new CardLayout();

    public GUIExampleApp() { // constructing the window
        super("GUIExampleApp");
        panelContainer.setLayout(card);
        panelContainer.add(StartApplication, "1");
        panelContainer.add(StartingApplication, "2");
        card.show(panelContainer, "now");

        // Set the frame's name
        // get the container frame

        // Create labels, text boxes and buttons
        Title = new JLabel("INTERFACE");
        Description = new JLabel("Knowledge grows everyday");

        MainMenuApplicationDesc= new JLabel("Pick Which Unit you want to study");
        Title.setBackground(Color.red);
        Title.setForeground(Color.blue);
        StartingApplication.setBackground(Color.red);
        Description.setBackground(Color.red);
        Description.setForeground(Color.blue);
        Start = new JButton("Start");
        Help = new JButton("Help");
        Quit = new JButton("Quit");

        // make the buttons listen to clicks on this application
        Start.addActionListener(this);
        Help.addActionListener(this);
        Quit.addActionListener(this);

        setSize(600, 600); // Set the frame's size
        Start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                card.show(panelContainer, "2");
            }
        });

        Back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                card.show(panelContainer, "1");
            }
        });

        Quit.addActionListener(new ActionListener()  {
            public void actionPerformed(ActionEvent arg0){
                System.exit (1);
            }
        });
    }

    // ERRORS ARE HERE, "Syntax Error" 
    frame.setVisible(true);
    frame.pack(); 

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable()  {
            public void run() {
                new GUIExampleApp();
            } // Create a GUIExampleApp frame
        });
    } // main method
}

2 个答案:

答案 0 :(得分:3)

您没有声明框架对象,您的类扩展它,因此请使用超类的方法。

setVisible(true);
pack();

答案 1 :(得分:0)

有几件事。一,声明变量框架在哪里?查看您的范围,这些行不在任何方法之内。那是语法错误。