代码之家  ›  专栏  ›  技术社区  ›  Nivas

为字体属性添加颜色

  •  1
  • Nivas  · 技术社区  · 14 年前

    我试着给他添颜色java.awt.Font的属性如下:

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

    但我得到了错误

    方法put(TextAttribute, 地图是 不适用于参数

    这个 Font API

    此字体仅识别定义的键 在TextAttribute中作为属性 FOREGROUND 有礼物吗 TextAttribute

    我做错什么了吗?

    2 回复  |  直到 14 年前
        1
  •  4
  •   Riduidel    14 年前

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

    因此,你不得不称其为 Font#deriveFont(Map) 具有新属性集的方法。 这将创建一个具有给定属性集的新字体,并因此创建新颜色。

        2
  •  1
  •   Ladislav Mrnka    13 年前
    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);
        }
    
    }
    
    }