Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

编写一个Java应用程序,实现如下图所示的图形界面。要求选择左侧的斜体,粗体,大字号,中字号,小字号字体的单选框选项后,右侧文字就呈现出对应的字体效果,且要求单击关闭按钮可将窗口…

  1. User

    编写一个Java应用程序,实现如下图所示的图形界面。要求选择左侧的斜体,粗体,大字号,中字号,小字号字体的单选框选项后,右侧文字就呈现出对应的字体效果,且要求单击关闭按钮可将窗口关闭

  2. Assistant

    。

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FontApplication extends JFrame {
        private JTextArea textArea;
        private JRadioButton italicButton;
        private JRadioButton boldButton;
        private JRadioButton largeButton;
        private JRadioButton mediumButton;
        private JRadioButton smallButton;
    
        public FontApplication(String title) {
            super(title);
    
            // 初始化窗口大小和位置
            setSize(500, 500);
            setLocationRelativeTo(null);
    
            // 创建面板组件
            JPanel controlPanel = new JPanel();
            JPanel textPanel = new JPanel(new BorderLayout());
    
            // 创建字体控制单选按钮
            italicButton = new JRadioButton("Italic");
            boldButton = new JRadioButton("Bold");
            largeButton = new JRadioButton("Large");
            mediumButton = new JRadioButton("Medium");
            smallButton = new JRadioButton("Small");
    
            // 将单选按钮加入一个按钮组
            ButtonGroup group = new ButtonGroup();
            group.add(italicButton);
            group.add(boldButton);
            group.add(largeButton);
            group.add(mediumButton);
            group.add(smallButton);
    
            // 设置默认选项
            mediumButton.setSelected(true);
    
            // 监听单选按钮的事件
            italicButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    updateFont();
                }
            });
    
            boldButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    updateFont();
                }
            });
    
            largeButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    updateFont();
                }
            });
    
            mediumButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    updateFont();
                }
            });
    
            smallButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    updateFont();
                }
            });
    
            // 创建文本区域
            textArea = new JTextArea("Hello World", 10, 30);
            textArea.setEditable(false);
            textArea.setLineWrap(true);
    
            // 将文本区域加入到面板中
            textPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
    
            // 将单选按钮加入到控制面板中
            controlPanel.add(italicButton);
            controlPanel.add(boldButton);
            controlPanel.add(largeButton);
            controlPanel.add(mediumButton);
            controlPanel.add(smallButton);
    
            // 将控制面板和文本面板加入到窗口中
            add(textPanel, BorderLayout.CENTER);
            add(controlPanel, BorderLayout.NORTH);
    
            // 添加关闭按钮
            JButton closeButton = new JButton("Close");
            closeButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
            add(closeButton, BorderLayout.SOUTH);
        }
    
        private void updateFont() {
            int style = Font.PLAIN;
            int size = 12;
    
            if (italicButton.isSelected()) {
                style |= Font.ITALIC;
            }
    
            if (boldButton.isSelected()) {
                style |= Font.BOLD;
            }
    
            if (largeButton.isSelected()) {
                size = 24;
            } else if (mediumButton.isSelected()) {
                size = 16;
            } else if (smallButton.isSelected()) {
                size = 12;
            }
    
            Font font = new Font("Serif", style, size);
            textArea.setFont(font);
        }
    
        public static void main(String[] args) {
            FontApplication app = new FontApplication("Font Application");
            app.setVisible(true);
        }
    }
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.