为什么我在Swing中看不到JFrame的标题?

问题描述 投票:0回答:1

所以我正在制作一个小组项目,它是 Java 控制台 GUI。这是我的 Java 控制台 GUI 代码:

这是我的代码的输出。

为什么我看不到此输出的标题?我已经输入了我的标题。有人可以修复我的布局吗?

这是编码部分。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class Homepage5FeatureKeysGUIConsole extends JFrame {

    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
        Homepage5FeatureKeysGUIConsole homePage = new Homepage5FeatureKeysGUIConsole();
        homePage.createAndShowGUI();
});
}

    public void createAndShowGUI() {
        setTitle("Select Feature");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(6, 1, 10, 10));

        JButton buttonFeature1 = createFeatureButton("Admin Registration");
        panel.add(buttonFeature1);

        JButton buttonFeature2 = createFeatureButton("Volunteer Registration");
        panel.add(buttonFeature2);

        JButton buttonFeature3 = createFeatureButton("Disaster Report");
        panel.add(buttonFeature3);

        JButton buttonFeature4 = createFeatureButton("Safe Shelter");
        panel.add(buttonFeature4);

        JButton buttonFeature5 = createFeatureButton("Fund Raising");
        panel.add(buttonFeature5);

        JButton buttonEmergencyContact = createFeatureButton("Emergency Contact");
        panel.add(buttonEmergencyContact);

        setContentPane(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
}

    private JButton createFeatureButton(String label) {
        JButton button = new JButton(label);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setBackground(new Color(124, 200, 255));
        button.addActionListener((ActionEvent e) -> handleFeatureButton(label));
        return button;
}

    private void handleFeatureButton(String feature) {
        switch (feature) {
            case "Admin Registration" -> openAdminAppGUI();
            case "Volunteer Registration" -> openVolunteerAppGUI();
            case "Disaster Report" -> openDisasterReportGUI();
            case "Safe Shelter" -> openSafeShelterGUI();
            case "Fund Raising" -> openFundRaisingGUI();
            case "Emergency Contact" -> openEmergencyContactGUI();
}
}

    private void openAdminAppGUI() {
        AdminAppGUI adminAppGUI = new AdminAppGUI();
        adminAppGUI.createAndShowGUI();
        dispose();
}

    private void openVolunteerAppGUI() {
        VolunteerAppGUI volunteerAppGUI = new VolunteerAppGUI();
        volunteerAppGUI.createAndShowGUI();
        dispose();
}

    private void openDisasterReportGUI() {
        DisasterReportGUI disasterReportGUI = new DisasterReportGUI();
        disasterReportGUI.createAndShowGUI();
        dispose();
}

    private void openSafeShelterGUI() {
        SafeShelterGUI safeShelterGUI = new SafeShelterGUI();
        safeShelterGUI.createAndShowGUI();
        dispose();
}

    private void openFundRaisingGUI() {
        FundRaisingGUIConsole fundRaisingGUI = new FundRaisingGUIConsole();
        fundRaisingGUI.createAndShowGUI();
        dispose();
}

    private void openEmergencyContactGUI() {
        EmergencyContactGUI emergencyContactGUI = new EmergencyContactGUI();
        emergencyContactGUI.createAndShowGUI();
        dispose();
}
}

我一直在尝试修复布局,但它仍然根本不起作用,而且我看不到输出中的标题“选择功能”。

java swing
1个回答
0
投票

手动添加尺寸:

panel.setPreferredSize(new Dimension(400, 400));
© www.soinside.com 2019 - 2024. All rights reserved.