9
votes

Java - Réglage des polices / Couleur dans SetForder

Y a-t-il une façon de définir ma propre police et mes schémas de couleur pour Text1 et Text2 Text dans la méthode définissante. Nouveau à Java et ne peut pas le trouver dans les tutoriels Sun Sun.

Mon code P>

//Create Positions Table
 JPanel SpreadPanel = new JPanel();
 SpreadPanel.setBorder(BorderFactory.createTitledBorder(" Text 1    Text 2"));


1 commentaires

Veuillez vous référer à API < / a>


6 Réponses :



0
votes

Les Javadocs pour faire cela sont quelque peu accablants si vous êtes nouveau à Java et à balancer. Les Javadocs for BorderFactory sont ici: http: // downloader.oracle.com/javase/1.5.0/docs/aplif/javax/swing/borderfactory.html

Voici un exemple de rendant le texte rouge dans une police Sans Serif: P>

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.io.IOException;

public class ScratchSpace {

    public static void main(String[] args) throws IOException {
        Font myFont = new Font("SansSerif", Font.PLAIN, 10);
        Color myColor = Color.RED;
        TitledBorder titledBorder = BorderFactory.createTitledBorder(null, " Text 1    Text 2", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, myFont, myColor);
        JFrame frame = new JFrame();
        final JLabel label = new JLabel("Hello gruel world");
        label.setBorder(titledBorder);
        frame.getContentPane().add(label);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}


0 commentaires

0
votes

Je sais que c'est une vieille question. Je pensais que je voudrais ressusciter cela comme peut-être que quelqu'un sait comment résoudre ce problème. Je n'ai que "une solution partielle".

J'ai très rapidement mis en œuvre la frontière qui fait ce que vous voulez. J'ai réutilisé ce que Java donne, c'est-à-dire une interprétation de HTML dans les composants swing. P>

Tous les œuvres douces, la frontière est parfaite pour un texte simple ou HTML, avec une exception pour une situation où vous essayez d'avoir des différentes Tailles de police pour les textes. P>

Je n'ai pas l'idée de résoudre ce problème. Mais je suis très intéressé par une solution. P>

Je sais que la procédure serait de résumer la largeur de chaque chaîne dans sa propre taille de police lors du calcul de la variable TextLengthInPixels. P>

Le problème est B> que je ne sais pas comment l'obtenir, peut-être de la vue, mais aucune idée de la façon dont? P>


import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.View;

public class MultiColorTitleBorder extends AbstractBorder
{
    private static final long serialVersionUID = 1L;
    private JLabel label;
    private int thicknessTop = 10;
    private Border border;
    private int thicknessLeft = 0;
    private int thicknessRight = 0;
    private int thicknessBottom = 0;

    public MultiColorTitleBorder(String title)
    {
        this.label = new JLabel(title);
        thicknessTop = label.getPreferredSize().height;
    }

    public MultiColorTitleBorder(String title, Border border)
    {
        this(title);
        this.border = border;
        thicknessLeft = border.getBorderInsets(null).left;
        thicknessRight = border.getBorderInsets(null).right;
        thicknessBottom = border.getBorderInsets(null).bottom;
    }

    @Override
    public synchronized void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
    {
        Graphics2D g2 = (Graphics2D) g;
        View view = (View) label.getClientProperty("html");
        String text = label.getText();
        FontMetrics fm = g2.getFontMetrics(label.getFont());
        int bY = y + fm.getAscent() - ((fm.getAscent() + fm.getDescent())) / 2;

        if(border != null)
        {
            Insets in = border.getBorderInsets(c);
            g2.setClip(x, y, thicknessLeft * 2, height);
            border.paintBorder(c, g, x, bY, width, height - bY);
            try
            {
                if(view != null)
                    text = view.getDocument().getText(0, view.getDocument().getLength());
            }catch(BadLocationException ex)
            {
                Logger.getLogger(MultiColorTitleBorder.class.getName()).log(Level.SEVERE, null, ex);
            }
            int textLengthInPixels = fm.stringWidth(text);
            System.out.println("textLengthInPixels=" + textLengthInPixels);
            g2.setClip(x +thicknessLeft * 2+ textLengthInPixels, y, width - thicknessLeft * 2 -textLengthInPixels, height);
            border.paintBorder(c, g, x, bY, width, height - bY);
            int bottomIn = in.bottom;
            g2.setClip(x, height - bottomIn, width, bottomIn);
            border.paintBorder(c, g, x, bY, width, height - bY);
            g2.setClip(x, y, width, height);
        }
        if(view != null)
            view.paint(g2, new Rectangle(x + thicknessLeft * 2, y, width - thicknessLeft * 2, height));
        else
        {
            Font prevFont = g2.getFont();
            g2.setFont(label.getFont());
            g2.drawString(text, x + thicknessLeft * 2, fm.getAscent());
            g2.setFont(prevFont);
        }
    }

    @Override
    public Insets getBorderInsets(Component c)
    {
        return new Insets(thicknessTop, thicknessLeft, thicknessBottom, thicknessRight);
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets)
    {
        insets.top = thicknessTop;
        insets.left = thicknessLeft;
        insets.right = thicknessRight;
        insets.bottom = thicknessBottom;
        return insets;
    }

    @Override
    public boolean isBorderOpaque()
    {
        return false;
    }

    public static void main(String[] args)
    {
        JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(200, 200));
        String title = "<html><color=red> Text 1</font><font color=blue>      Text 2</font&gt";
        //title = "<html><font color=red font size=5> Text 1</font><font color=blue>      Text 2</font>";
        //title = "Text 1   Text 2";

        p.setBorder(new MultiColorTitleBorder(title, new LineBorder(Color.CYAN, 6)));
        p.setBackground(Color.YELLOW);
        p.add(new JTextField(5));
        JPanel contentPane = new JPanel();
        contentPane.add(p);
        JFrame f = new JFrame();
        f.setContentPane(contentPane);
        f.setSize(800, 600);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}


0 commentaires

0
votes

Essayez ceci:

.setBorder(UIManager.getBorder("TextField.border"));


0 commentaires

8
votes
setBorder(BorderFactory.createTitledBorder(null, "text", TitledBorder.CENTER, TitledBorder.BOTTOM, new Font("times new roman",Font.PLAIN,12), Color.yellow));
the first parameter null or another border (for compound borders)
2nd param  text that you're displaying
3rd and 4th param   justification and location of the text from param 24th param 
and 5th param are the two to set font and color

0 commentaires

1
votes

polices de texte: xxx

couleur texte: xxx


0 commentaires