choosecolor

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1193665761703&lmt=1193665780&format=336x280_as&output=html&correlator=1193665761687&url=http%3A%2F%2Fwww.codeguru.cn%2Fpublic%2Fiframe%2Fwinapiiframe.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=1285758818.1193665762&ga_sid=1193665762&ga_hid=111695597&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=8&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency">     函数功能:该函数创建一个能使用户从中选择颜色的通用颜色对话框。

    函数原型:BOOL ChooseColor(LPCHOOSECOLOR IpCC);

    参数:

    lpCC:指向一个包括初始化对话框信息的CHOOSECOLOR结构。当ChooseColor函数返回时,此结构含有有关用户颜色选择的信息。

    返回值:如果用户点击对话框中的OK按钮,返回值为非零值。CHOOSECOLOR结构中的rgbResult成员含有用户选择的颜色的RGB颜色值。如果用户取消或关闭Color对话框或错误出现,返回值为零。若想获得更多错误信息,请调用CommDlgExtondedError函数,此函数的返回值为下列中的一个:

    CDERR_FINDRESFAILURE;CDERR_MEMLOCKFAILURE;CDERR_INITIALIZATION;

    CDERR_NOHINSTANCE;CDERR_LOCKRESFAILURE;CDERR_NOHOOK

    CDERR_LOADRESFAILURE;CDERR_NOTEMPLATE;CDERR_LOADSTRFAlLURE;

    CDERR_STRUCTSIZE;CDERR_MEMALLOCFAILURE

    备注Color对话框不支持彩色调色板,对话框提供的颜色的选择仅限于系统颜色和这些颜色的混合值,可以为对话框提供一个CCHOOKProc程序,此挂钩程序能处理发送给对话框的信息。通过建立CHOOSECOLOR结构中Flags成员的CC_ENABLEHOOK标志和指定IpfnHook成员中挂钩程序的地址,可使挂钩程序生效。

    速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:2.0及以上版本;头文件:commdlg.h;库文件:commdlg32.lib;Unicode:在Windows NT环境中实现为Unicode和ANSI两个版本。

错误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); }); } }
最新发布
11-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值