错误Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at test.ArtFont.main(ArtFont.java:221)
代码 import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.plaf.metal.*;
public class ArtFont extends JFrame {
private JTextField textField;
private JTextArea textArea;
private JCheckBox boldCheckBox, italicCheckBox;
private JButton colorButton;
private JComboBox<String> fontComboBox, sizeComboBox, lookComboBox;
private JLabel infoLabel;
public ArtFont() {
// 设置窗口标题
super("字体设置工具");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(650, 450);
setLocationRelativeTo(null);
// 创建主面板
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
// 添加学生信息
infoLabel = new JLabel("学生信息: 张明 - 202305678 - 计算机科学与技术", JLabel.CENTER);
infoLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
infoLabel.setForeground(new Color(0, 100, 200));
infoLabel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createMatteBorder(0, 0, 2, 0, new Color(150, 150, 200)),
BorderFactory.createEmptyBorder(8, 0, 8, 0)
));
mainPanel.add(infoLabel, BorderLayout.NORTH);
// 创建控制面板
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
controlPanel.setBorder(BorderFactory.createTitledBorder("字体设置"));
// 文本输入框
JPanel textInputPanel = new JPanel(new BorderLayout(5, 5));
textInputPanel.add(new JLabel("输入文字:"), BorderLayout.WEST);
textField = new JTextField();
textField.addActionListener(this::handleTextInput);
textInputPanel.add(textField, BorderLayout.CENTER);
controlPanel.add(textInputPanel);
controlPanel.add(Box.createVerticalStrut(10));
// 样式设置面板
JPanel stylePanel = new JPanel(new GridLayout(2, 2, 5, 5));
stylePanel.setBorder(BorderFactory.createTitledBorder("字体样式"));
// 粗体复选框
boldCheckBox = new JCheckBox("粗体");
boldCheckBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
boldCheckBox.addItemListener(this::updateFontStyle);
stylePanel.add(boldCheckBox);
// 斜体复选框
italicCheckBox = new JCheckBox("斜体");
italicCheckBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
italicCheckBox.addItemListener(this::updateFontStyle);
stylePanel.add(italicCheckBox);
// 颜色按钮
colorButton = new JButton("选择颜色");
colorButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
colorButton.addActionListener(this::chooseColor);
stylePanel.add(colorButton);
controlPanel.add(stylePanel);
controlPanel.add(Box.createVerticalStrut(10));
// 字体设置面板
JPanel fontSettingsPanel = new JPanel(new GridLayout(2, 2, 5, 5));
fontSettingsPanel.setBorder(BorderFactory.createTitledBorder("字体设置"));
// 字体选择框
fontSettingsPanel.add(new JLabel("字体样式:"));
fontComboBox = new JComboBox<>(new String[]{"宋体", "黑体", "楷体", "微软雅黑", "Arial", "Times New Roman"});
fontComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
fontComboBox.addActionListener(this::updateFont);
fontSettingsPanel.add(fontComboBox);
// 字号选择框
fontSettingsPanel.add(new JLabel("字体大小:"));
sizeComboBox = new JComboBox<>(new String[]{"12", "14", "16", "18", "20", "24", "28", "36"});
sizeComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
sizeComboBox.setSelectedItem("16");
sizeComboBox.addActionListener(this::updateFont);
fontSettingsPanel.add(sizeComboBox);
controlPanel.add(fontSettingsPanel);
// 添加到主面板
mainPanel.add(controlPanel, BorderLayout.WEST);
// 创建文本显示区
JPanel displayPanel = new JPanel(new BorderLayout());
displayPanel.setBorder(BorderFactory.createTitledBorder("预览效果"));
textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
textArea.setText("在这里输入文本,查看字体效果...");
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setForeground(Color.BLACK);
textArea.setBackground(new Color(245, 245, 245));
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(300, 250));
displayPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(displayPanel, BorderLayout.CENTER);
// 外观选择面板
JPanel lookPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
lookPanel.add(new JLabel("窗体外观:"));
lookComboBox = new JComboBox<>(new String[]{"默认", "Windows", "Metal", "Motif", "Nimbus"});
lookComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
lookComboBox.addActionListener(this::changeLookAndFeel);
lookPanel.add(lookComboBox);
// 添加重置按钮
JButton resetButton = new JButton("重置设置");
resetButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
resetButton.addActionListener(e -> resetSettings());
lookPanel.add(resetButton);
mainPanel.add(lookPanel, BorderLayout.SOUTH);
// 添加主面板到窗口
add(mainPanel);
}
// 处理文本输入
private void handleTextInput(ActionEvent e) {
textArea.setText(textField.getText());
}
// 更新字体样式
private void updateFontStyle(ItemEvent e) {
int style = Font.PLAIN;
if (boldCheckBox.isSelected()) style |= Font.BOLD;
if (italicCheckBox.isSelected()) style |= Font.ITALIC;
textArea.setFont(new Font(
(String)fontComboBox.getSelectedItem(),
style,
Integer.parseInt((String)sizeComboBox.getSelectedItem())
));
}
// 更新字体
private void updateFont(ActionEvent e) {
updateFontStyle(null);
}
// 选择颜色
private void chooseColor(ActionEvent e) {
Color color = JColorChooser.showDialog(
this,
"选择文本颜色",
textArea.getForeground()
);
if (color != null) {
textArea.setForeground(color);
colorButton.setForeground(color);
}
}
// 更改外观
private void changeLookAndFeel(ActionEvent e) {
String look = (String)lookComboBox.getSelectedItem();
try {
switch (look) {
case "Windows":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
break;
case "Metal":
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
break;
case "Motif":
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
break;
case "Nimbus":
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
break;
default:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "无法设置外观: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
// 重置设置
private void resetSettings() {
textField.setText("");
textArea.setText("在这里输入文本,查看字体效果...");
boldCheckBox.setSelected(false);
italicCheckBox.setSelected(false);
fontComboBox.setSelectedItem("微软雅黑");
sizeComboBox.setSelectedItem("16");
textArea.setForeground(Color.BLACK);
colorButton.setForeground(Color.BLACK);
lookComboBox.setSelectedItem("默认");
textArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception ex) {
// 忽略异常
}
}
public static void main(String[] args) {
// 设置GUI外观
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(() -> {
ArtFont app = new ArtFont();
app.setVisible(true);
});
}
}
最新发布