JPanels和GridLayouts

时间:2013-12-07 04:01:54

标签: java swing jpanel

我一直在尝试用Java重新创建它:http://imgur.com/pjt7SMZ

image

这是我到目前为止的代码:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Display extends JFrame implements ActionListener {
    private static final int FRAME_WIDTH = 400;
    private static final int FRAME_HEIGHT = 350;

    private static final int FRAME_X_ORIGIN = 100;
    private static final int FRAME_Y_ORIGIN = 75;

    private JButton readFileButton;
    private JButton exitButton;
    private JButton statsButton;
    private JButton clearButton;
    private JButton helpButton;
    private JLabel headerLabel;

    public Display() {

        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setResizable(false);
        setTitle("CSCE155A Course Offering Viewer");
        setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel header = new JPanel(new GridLayout(1, 1, 5, 5));
        headerLabel = new JLabel("CSCE155A Course Offering Viewer");
        header.add(headerLabel);

    }

    public static void main(String[] args) {
        Display frame = new Display();
        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {

    }

}

我的问题在于JPanel。按照我们的指示,我们假设在BorderLayout内部使用GridLayout,但每当我运行代码时都没有任何反应。 JPanel甚至是最好的方法吗?现在我只是想让标题工作。

1 个答案:

答案 0 :(得分:2)

根据您的设计,您不应在JLabel上添加JPanel。在headerLabel之上添加JFrame并对齐文本CENTER

 headerLabel = new JLabel("CSCE155A Course Offering Viewer",JLabel.CENTER);       
 add(headerLabel,BorderLayout.NORTH);// Add it with JFrame.