gpt4 book ai didi

java - JPanels 和 GridLayouts

转载 作者:行者123 更新时间:2023-11-30 09:14:32 24 4
gpt4 key购买 nike

我一直在尝试用 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。正如我们被指示的那样,我们假设使用 BorderLayoutGridLayout 在里面,但是每当我运行代码时没有任何反应。 JPanel 是最好的方法吗?现在我只是想让 header 正常工作。

最佳答案

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

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

关于java - JPanels 和 GridLayouts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20437429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com