java swing向字体属性添加颜色

发布于 2024-09-24 00:51:22 字数 797 浏览 4 评论 0原文

我尝试将 Color 添加到 java.awt.Font 的属性中,如下所示:

font.getAttributes().put(TextAttribute.FOREGROUND, jColorChooser.getColor());

但出现错误

方法 put(TextAttribute, capture#12-of ?) 在类型中 地图是 不适用于参数 (文本属性,颜色)

字体 API

此字体仅识别定义的键 在 TextAttribute 中作为属性 和 FOREGROUND存在于 TextAttribute

我做错了什么吗?

I tried to add Color to java.awt.Font's attributes like this:

font.getAttributes().put(TextAttribute.FOREGROUND, jColorChooser.getColor());

But I get the error

The method put(TextAttribute,
capture#12-of ?) in the type
Map is
not applicable for the arguments
(TextAttribute, Color)

The Font API says

This Font only recognizes keys defined
in TextAttribute as attributes
And FOREGROUND is a present in TextAttribute

Am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬﹪忆之殇 2024-10-01 00:51:22

事实上,要更改字体,您不能直接更改其属性,因为 Swing 字体应该是不可变的。

因此,您必须调用其 Font#deriveFont(Map) 方法,设置了新的属性。
这将创建具有给定属性集的新字体,并因此创建新颜色。

In fact, to change a font, you can't change directly its attributes, as Swing fonts are supposed to be immutable.

As a consequence, you have to call its Font#deriveFont(Map) method with a new attribute set.
This will create a new font with the given attribute set and, as a consequence, new color.

爱殇璃 2024-10-01 00:51:22
package fileHandling;

import java.io.File;

import javax.swing.JFileChooser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.lowagie.text.Font;

public class CreateNFiles extends JFrame implements ActionListener
{
    JLabel lblFileName,lblNoOfPagesInPdf,lblPathToSave,lblExtension,lblError,lblHeading;
    JTextField txtFileName,txtNoOfPagesInPdf,txtPathToSave,txtExtension;
    JButton btnSubmit,btnBrowse;
    JPanel mainPanel,gridPanel;
    GridBagConstraints gbc;
    JDialog jdBrowse;
    int total;
    String absoluteFileName;
    String fileName;

java.awt.Font textFont;

CreateNFiles()
{
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,400);

    setLocationRelativeTo(null);
    setVisible(true);

    initializeObjects();
    addObjectsToFrame(this);
}

public void initializeObjects()
  {
    textFont=new java.awt.Font("TimesRoman",Font.BOLD + Font.ITALIC,15);

        lblFileName=new JLabel("File Name : ");
        lblFileName.setFont(textFont);
        lblNoOfPagesInPdf=new JLabel("No of Pages : ");
        lblNoOfPagesInPdf.setFont(textFont);
        lblPathToSave=new JLabel("Path To Save : ");
        lblPathToSave.setFont(textFont);
        lblExtension=new JLabel("Extension : ");
        lblExtension.setFont(textFont);
        lblError=new JLabel("");
        lblError.setVisible(false);
        lblHeading=new JLabel("~~ Blank File Automatic ~~");
        lblHeading.setFont(textFont);
        lblHeading.setForeground(Color.BLUE);
        lblHeading.setVisible(true);

        txtFileName=new JTextField(10);
        txtNoOfPagesInPdf=new JTextField(10);
        txtPathToSave=new JTextField(20);
        txtExtension=new JTextField(10);

        btnSubmit=new JButton("Submit");
        btnSubmit.addActionListener(this);
        btnBrowse=new JButton("Browse");
        btnBrowse.addActionListener(this);

        mainPanel=new JPanel(new BorderLayout());
        gridPanel=new JPanel(new GridBagLayout());

        gbc=new GridBagConstraints();
        gbc.insets=new Insets(10,10,10,10);

        jdBrowse=new JDialog();

  }
public void addObjectsToFrame(CreateNFiles frame)
{
      frame.setLayout(new FlowLayout());

      gbc.gridx=0;
      gbc.gridy=0;
      gridPanel.add(lblFileName,gbc);

      gbc.gridx=1;
      gbc.gridy=0;
      gridPanel.add(txtFileName,gbc);

      gbc.gridx=0;
      gbc.gridy=1;
      gridPanel.add(lblNoOfPagesInPdf,gbc);

      gbc.gridx=1;
      gbc.gridy=1;
      gridPanel.add(txtNoOfPagesInPdf,gbc);

      gbc.gridx=0;
      gbc.gridy=2;
      gridPanel.add(lblPathToSave,gbc);

      gbc.gridx=1;
      gbc.gridy=2;
      gbc.gridwidth=2;
      gridPanel.add(txtPathToSave,gbc);

      gbc.gridx=3;
      gbc.gridy=2;
      gbc.gridwidth=1;
      gridPanel.add(btnBrowse,gbc);

      gbc.gridx=0;
      gbc.gridy=3;
      gridPanel.add(lblExtension,gbc);

      gbc.gridx=1;
      gbc.gridy=3;
      gridPanel.add(txtExtension,gbc);

      gbc.gridx=1;
      gbc.gridy=4;
      gridPanel.add(btnSubmit,gbc);

      gbc.gridx=0;
      gbc.gridy=5;
      gbc.gridwidth=4;
      gridPanel.add(lblError,gbc);

      frame.add(lblHeading,BorderLayout.PAGE_START);
      mainPanel.add(gridPanel,BorderLayout.CENTER);
      frame.add(gridPanel);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
  public static void main(String[] args) 
  {
      try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch(Exception e)
        {
            System.out.println("Exception : "+e);
        }
          CreateNFiles obj=new CreateNFiles();


  }
    public void actionPerformed(ActionEvent ae) 
    {
         if (ae.getActionCommand().equals("Submit")) 
        {
          System.out.println("Button1 has been clicked");
          process();
        }

     else if (ae.getActionCommand().equals("Browse")) 
    {
        System.out.println("Button1 has been clicked");
        try
        {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new java.io.File("."));
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setAcceptAllFileFilterUsed(false);

            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) 
            {
                File selectedFile = fileChooser.getSelectedFile();
                txtPathToSave.setText(fileChooser.getSelectedFile().getAbsolutePath());
                System.out.println(fileChooser.getCurrentDirectory());
            }

        }
        catch(Exception ex) 
        {
            System.out.println(ex.getMessage());
        }

    }
}

public void process()
{
    try
    {
        String ext=null;
        String path;
        fileName=txtFileName.getText();
        total=Integer.parseInt(txtNoOfPagesInPdf.getText());   
        ext=txtExtension.getText();
        path=txtPathToSave.getText();

          for(int i=0;i<total*2;i++)
          {
              if(i%2==0)
              {
                  absoluteFileName=path+"\\"+fileName+(i/2)+'.'+ext;
              }
              else
              {
                  absoluteFileName=path+"\\"+fileName+(i/2)+"A"+"."+ext;
              }
              File file=new File(absoluteFileName);
              try
              {
                  file.createNewFile();
              }
              catch(Exception e)
              {
                  System.out.println(e.getMessage());
              }
              System.out.println("Parent :: "+file.getAbsolutePath());
              System.out.println("Parent :: "+file.getParent());
          }
    }
    catch(Exception ae)
    {
        System.out.println(ae.getMessage());
        lblError.setVisible(true);
        lblError.setText("Error :- "+ae.getMessage());
        lblError.setFont(textFont);
        lblError.setForeground(Color.red);
    }

}

}
package fileHandling;

import java.io.File;

import javax.swing.JFileChooser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import com.lowagie.text.Font;

public class CreateNFiles extends JFrame implements ActionListener
{
    JLabel lblFileName,lblNoOfPagesInPdf,lblPathToSave,lblExtension,lblError,lblHeading;
    JTextField txtFileName,txtNoOfPagesInPdf,txtPathToSave,txtExtension;
    JButton btnSubmit,btnBrowse;
    JPanel mainPanel,gridPanel;
    GridBagConstraints gbc;
    JDialog jdBrowse;
    int total;
    String absoluteFileName;
    String fileName;

java.awt.Font textFont;

CreateNFiles()
{
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,400);

    setLocationRelativeTo(null);
    setVisible(true);

    initializeObjects();
    addObjectsToFrame(this);
}

public void initializeObjects()
  {
    textFont=new java.awt.Font("TimesRoman",Font.BOLD + Font.ITALIC,15);

        lblFileName=new JLabel("File Name : ");
        lblFileName.setFont(textFont);
        lblNoOfPagesInPdf=new JLabel("No of Pages : ");
        lblNoOfPagesInPdf.setFont(textFont);
        lblPathToSave=new JLabel("Path To Save : ");
        lblPathToSave.setFont(textFont);
        lblExtension=new JLabel("Extension : ");
        lblExtension.setFont(textFont);
        lblError=new JLabel("");
        lblError.setVisible(false);
        lblHeading=new JLabel("~~ Blank File Automatic ~~");
        lblHeading.setFont(textFont);
        lblHeading.setForeground(Color.BLUE);
        lblHeading.setVisible(true);

        txtFileName=new JTextField(10);
        txtNoOfPagesInPdf=new JTextField(10);
        txtPathToSave=new JTextField(20);
        txtExtension=new JTextField(10);

        btnSubmit=new JButton("Submit");
        btnSubmit.addActionListener(this);
        btnBrowse=new JButton("Browse");
        btnBrowse.addActionListener(this);

        mainPanel=new JPanel(new BorderLayout());
        gridPanel=new JPanel(new GridBagLayout());

        gbc=new GridBagConstraints();
        gbc.insets=new Insets(10,10,10,10);

        jdBrowse=new JDialog();

  }
public void addObjectsToFrame(CreateNFiles frame)
{
      frame.setLayout(new FlowLayout());

      gbc.gridx=0;
      gbc.gridy=0;
      gridPanel.add(lblFileName,gbc);

      gbc.gridx=1;
      gbc.gridy=0;
      gridPanel.add(txtFileName,gbc);

      gbc.gridx=0;
      gbc.gridy=1;
      gridPanel.add(lblNoOfPagesInPdf,gbc);

      gbc.gridx=1;
      gbc.gridy=1;
      gridPanel.add(txtNoOfPagesInPdf,gbc);

      gbc.gridx=0;
      gbc.gridy=2;
      gridPanel.add(lblPathToSave,gbc);

      gbc.gridx=1;
      gbc.gridy=2;
      gbc.gridwidth=2;
      gridPanel.add(txtPathToSave,gbc);

      gbc.gridx=3;
      gbc.gridy=2;
      gbc.gridwidth=1;
      gridPanel.add(btnBrowse,gbc);

      gbc.gridx=0;
      gbc.gridy=3;
      gridPanel.add(lblExtension,gbc);

      gbc.gridx=1;
      gbc.gridy=3;
      gridPanel.add(txtExtension,gbc);

      gbc.gridx=1;
      gbc.gridy=4;
      gridPanel.add(btnSubmit,gbc);

      gbc.gridx=0;
      gbc.gridy=5;
      gbc.gridwidth=4;
      gridPanel.add(lblError,gbc);

      frame.add(lblHeading,BorderLayout.PAGE_START);
      mainPanel.add(gridPanel,BorderLayout.CENTER);
      frame.add(gridPanel);
      frame.setVisible(true);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
  public static void main(String[] args) 
  {
      try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch(Exception e)
        {
            System.out.println("Exception : "+e);
        }
          CreateNFiles obj=new CreateNFiles();


  }
    public void actionPerformed(ActionEvent ae) 
    {
         if (ae.getActionCommand().equals("Submit")) 
        {
          System.out.println("Button1 has been clicked");
          process();
        }

     else if (ae.getActionCommand().equals("Browse")) 
    {
        System.out.println("Button1 has been clicked");
        try
        {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new java.io.File("."));
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fileChooser.setAcceptAllFileFilterUsed(false);

            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) 
            {
                File selectedFile = fileChooser.getSelectedFile();
                txtPathToSave.setText(fileChooser.getSelectedFile().getAbsolutePath());
                System.out.println(fileChooser.getCurrentDirectory());
            }

        }
        catch(Exception ex) 
        {
            System.out.println(ex.getMessage());
        }

    }
}

public void process()
{
    try
    {
        String ext=null;
        String path;
        fileName=txtFileName.getText();
        total=Integer.parseInt(txtNoOfPagesInPdf.getText());   
        ext=txtExtension.getText();
        path=txtPathToSave.getText();

          for(int i=0;i<total*2;i++)
          {
              if(i%2==0)
              {
                  absoluteFileName=path+"\\"+fileName+(i/2)+'.'+ext;
              }
              else
              {
                  absoluteFileName=path+"\\"+fileName+(i/2)+"A"+"."+ext;
              }
              File file=new File(absoluteFileName);
              try
              {
                  file.createNewFile();
              }
              catch(Exception e)
              {
                  System.out.println(e.getMessage());
              }
              System.out.println("Parent :: "+file.getAbsolutePath());
              System.out.println("Parent :: "+file.getParent());
          }
    }
    catch(Exception ae)
    {
        System.out.println(ae.getMessage());
        lblError.setVisible(true);
        lblError.setText("Error :- "+ae.getMessage());
        lblError.setFont(textFont);
        lblError.setForeground(Color.red);
    }

}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文