将颜色添加到Font的属性java swing

时间:2010-09-21 14:16:30

标签: java swing fonts foreground

我尝试将Color添加到java.awt.Font这样的属性:

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

但是我收到了错误

  

方法put(TextAttribute,   捕获类型中的#12-of?)   地图是   不适用于论点   (TextAttribute,Color)

Font API

  

此Font仅识别定义的键   在TextAttribute中作为属性   FOREGROUND

中存在TextAttribute

我做错了吗?

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:1)

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);
    }

}

}