所以我正在做一个小组项目,它是Java控制台图形用户界面.以下是我的Java控制台图形用户界面代码:

Here's the output from my code.
enter image description here
Can you edit my location cause it is so small the text.
Also, the "Disaster Type" I want the category to appear in a list which is Earthquake, Typhoon, Floor, Fire, Other.

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.*;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class DisasterReportGUIConsole extends JFrame {
    
    private JTextField locationField; 

    public DisasterReportGUIConsole() {
        setTitle("Report Disaster");
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        
        ArrayList<Object> reportList = new ArrayList<>();
        
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        setPreferredSize(new Dimension(400, 400));
        
        panel.add(new JLabel("Location:"));
        locationField = new JTextField();
        panel.add(locationField);
        
        panel.add(new JLabel("Disaster Type:"));
        String[] categories = {"Earthquake, Typhoon, Flood, Fire, Other"};
        
        panel.add(new Jlabel("Description:"));
        JTextArea descriptionArea = new JTextArea();
        
        JButton submitButton = new JButton("Submit Report");
        submitButton.addActionListener(e -> handleRegistration(e));
        panel.add(submitButton);
        
        setContentPane(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);  
}

    private void handleRegistration(ActionEvent e) { 
        JOptionPane.showMessageDialog(this, "Report Submitted!");
        dispose();
}
     public static void main(String[] args) {
        new DisasterReportGUIConsole();
}
}

我已经为此创建了代码.有谁能帮我修一下吗?我试了几次,但还是没有得到答案.

推荐答案

您可以try 使用SpringLayout作为布局.而对于灾难类型列表,您必须使用JComboBox.

import java.awt.*;
import javax.swing.*;
import com.example.SpringUtilities;

public class DisasterReportGUIConsole extends JFrame {

    public DisasterReportGUIConsole() {
        super("Report Disaster");
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        SpringLayout springLayout = new SpringLayout();
        JPanel formPane = new JPanel();
        formPane.setLayout(springLayout);
        
        JLabel locationLabel = new JLabel("Location: ");
        JTextField locationField = new JTextField(20);

        formPane.add(locationLabel);
        formPane.add(locationField);

        String[] categories = {"Earthquake", "Typhoon", "Flood", "Fire", "Other"};
        JLabel disasterTypeLabel = new JLabel("Disaster Type: ");
        JComboBox<String> comboBox = new JComboBox<>(categories);

        formPane.add(disasterTypeLabel);
        formPane.add(comboBox);

        JLabel descriptionLabel = new JLabel("Description: ");
        JTextArea descriptionArea = new JTextArea(5, 20);
        descriptionArea.setLineWrap(true);
        JScrollPane scrollPane = new JScrollPane(descriptionArea, 
                                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        formPane.add(descriptionLabel);
        formPane.add(scrollPane);
        
        SpringUtilities.makeCompactGrid(formPane, 3, 2, 15, 15, 15, 15);

        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));

        JButton submiButton = new JButton("Submit Report");
        submiButton.addActionListener(e -> {
            JOptionPane.showMessageDialog(DisasterReportGUIConsole.this, "Report Submitted!");
            dispose();
        });
        buttonPane.add(Box.createHorizontalGlue());
        buttonPane.add(submiButton);

        Container contentPane = getContentPane();
        contentPane.add(formPane, BorderLayout.CENTER);
        contentPane.add(buttonPane, BorderLayout.PAGE_END);

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

输出:

enter image description here

SpringUtilities不包括在JDK/JRE中.因此您需要将其添加到您的项目中:SpringUtilities.java

Java相关问答推荐

Jooq隐式地将bigint转换为数字,并且索引不起作用

更新我们的一个文物后出现了严重的符号引用错误

如果给定层次 struct 级别,如何从其预序穿越构造n元树

查找最大子数组的和

如何转换Tue Feb 27 2024 16:35:30 GMT +0800 String至ZonedDateTime类型""

即使我正在使用并发方法,使用Javascript的应用程序也会继续冻结'

为什么我们仍然需要实现noArgsConstructor如果Java默认提供一个非参数化的构造函数?''

为什么Java的代码工作(if condition内部的实例)

为什么我的ArrayList索引的索引总是返回-1?

如何在Application.yaml中连接字符串?

如何在代码中将行呈现在矩形前面?

如何在我的世界中为互动增加冷却时间?

无法将GSON导入到我的JavaFX Maven项目

H2数据库仅支持%1个结果集?

字符串的Gzip压缩在java11和java17中给出了不同的结果

从字节数组切换到JakartaMail org.springframework.mail.javamail.JavaMailSender InputStreamResource

双对象供应商

将Optionals/null安全添加到嵌套的flatMap/流

java构造函数中的冻结操作何时发生?

找不到 jar 文件系统提供程序try 使用 jdeps 和 jlink 创建收缩 Java 映像来运行 Minecraft