JPanels和GridLayouts [英] JPanels and GridLayouts

查看:123
本文介绍了JPanels和GridLayouts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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.按照我们的指示,我们假设将BorderLayoutGridLayout一起使用,但是只要运行代码,都不会发生任何事情. JPanel甚至是执行此操作的最佳方法吗?现在,我只是想让标题正常工作.

解决方案

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

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

I have been attempting to recreate this in Java: http://imgur.com/pjt7SMZ

This is the code I have so far:

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) {

    }

}

My problem is with JPanel. As we were instructed, we are suppose to use the BorderLayout with GridLayout inside, but nothing happens whenever I run the code. Is JPanel even the best way to do this? Right now I'm just trying to get the header to work.

解决方案

According to your design, you should not add JLabel on JPanel. Add headerLabel on top of JFrame and align the text CENTER.

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

这篇关于JPanels和GridLayouts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆