用JAVA实现简单文本编译器

 设计一个简单的文字输入的界面,并添加文字颜色和字号大小的编辑按钮。根据选择的不同按钮,显示出相应的效果。除此之外,再添加一个滑块组件,根据滑块的位置变化,设置字体的大小,滑块向右字号就跟着变大,反之则变小。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class TextEditor extends JFrame {
    private JLabel textLabel;
    private JLabel commentLabel;
    private JRadioButton redButton, blueButton, greenButton;
    private JRadioButton smallButton, mediumButton, largeButton;
    private JSlider fontSizeSlider;

    public TextEditor() {
        // 设置窗口标题
        setTitle("简单文本编辑器");
        // 设置窗口大小
        setSize(400, 300);
        // 设置窗口关闭方式
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // 创建内容面板
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());

        // 创建文本标签
        textLabel = new JLabel("欢迎来到JAVA实训!");
        textLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        contentPanel.add(textLabel, BorderLayout.CENTER);

        // 创建字体颜色选择面板
        JPanel colorPanel = new JPanel();
        colorPanel.setLayout(new FlowLayout());
        JLabel colorLabel = new JLabel("字体颜色:");
        redButton = new JRadioButton("红色");
        blueButton = new JRadioButton("蓝色");
        greenButton = new JRadioButton("绿色");
        ButtonGroup colorGroup = new ButtonGroup();
        colorGroup.add(redButton);
        colorGroup.add(blueButton);
        colorGroup.add(greenButton);
        colorPanel.add(colorLabel);
        colorPanel.add(redButton);
        colorPanel.add(blueButton);
        colorPanel.add(greenButton);

        // 创建字号选择面板
        JPanel sizePanel = new JPanel();
        sizePanel.setLayout(new FlowLayout());
        JLabel sizeLabel = new JLabel("字体大小:");
        smallButton = new JRadioButton("小号");
        mediumButton = new JRadioButton("中号");
        largeButton = new JRadioButton("大号");
        ButtonGroup sizeGroup = new ButtonGroup();
        sizeGroup.add(smallButton);
        sizeGroup.add(mediumButton);
        sizeGroup.add(largeButton);
        sizePanel.add(sizeLabel);
        sizePanel.add(smallButton);
        sizePanel.add(mediumButton);
        sizePanel.add(largeButton);

        // 创建字号选择滑块面板
        JPanel fontSizePanel = new JPanel();
        fontSizePanel.setLayout(new FlowLayout());
        JLabel fontSizeLabel = new JLabel("滑动标签:");
        fontSizeSlider = new JSlider(JSlider.HORIZONTAL, 10, 40, 12);
        fontSizeSlider.setMinorTickSpacing(1);
        fontSizeSlider.setMajorTickSpacing(5);
        fontSizeSlider.setPaintTicks(true);
        fontSizeSlider.setPaintLabels(true);
        fontSizePanel.add(fontSizeLabel);
        fontSizePanel.add(fontSizeSlider);

        // 创建注释标签
        commentLabel = new JLabel("输入评论");
        commentLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        contentPanel.add(commentLabel, BorderLayout.SOUTH);

        // 添加事件监听器
        ActionListener colorListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (redButton.isSelected()) {
                    textLabel.setForeground(Color.RED);
                } else if (blueButton.isSelected()) {
                    textLabel.setForeground(Color.BLUE);
                } else if (greenButton.isSelected()) {
                    textLabel.setForeground(Color.GREEN);
                }
            }
        };
        redButton.addActionListener(colorListener);
        blueButton.addActionListener(colorListener);
        greenButton.addActionListener(colorListener);

        ActionListener sizeListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (smallButton.isSelected()) {
                    textLabel.setFont(textLabel.getFont().deriveFont(Font.PLAIN, 12));
                    commentLabel.setFont(commentLabel.getFont().deriveFont(Font.PLAIN, 12));
                    fontSizeSlider.setValue(12);
                } else if (mediumButton.isSelected()) {
                    textLabel.setFont(textLabel.getFont().deriveFont(Font.PLAIN, 18));
                    commentLabel.setFont(commentLabel.getFont().deriveFont(Font.PLAIN, 18));
                    fontSizeSlider.setValue(18);
                } else if (largeButton.isSelected()) {
                    textLabel.setFont(textLabel.getFont().deriveFont(Font.PLAIN, 24));
                    commentLabel.setFont(commentLabel.getFont().deriveFont(Font.PLAIN, 24));
                    fontSizeSlider.setValue(24);
                }
            }
        };
        smallButton.addActionListener(sizeListener);
        mediumButton.addActionListener(sizeListener);
        largeButton.addActionListener(sizeListener);

        fontSizeSlider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int fontSize = fontSizeSlider.getValue();
                textLabel.setFont(new Font("微软雅黑", Font.PLAIN, fontSize));
                commentLabel.setFont(new Font("微软雅黑", Font.PLAIN, fontSize));
            }
        });

        // 创建选择面板,将颜色选择面板和字号选择按钮面板放入其中
        JPanel selectionPanel = new JPanel();
        selectionPanel.setLayout(new BorderLayout());
        selectionPanel.add(colorPanel, BorderLayout.NORTH);
        selectionPanel.add(sizePanel, BorderLayout.SOUTH);

        // 将选择面板和字号选择滑块面板添加到内容面板
        contentPanel.add(selectionPanel, BorderLayout.NORTH);
        contentPanel.add(fontSizePanel, BorderLayout.SOUTH);

        // 将内容面板添加到主窗口
        add(contentPanel);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TextEditor editor = new TextEditor();
                editor.setVisible(true);
            }
        });
    }
}

运行结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值