Java - 在 setBorder 中设置字体/颜色

发布于 2024-10-10 09:18:38 字数 275 浏览 7 评论 0原文

有没有办法可以为 Text1 和 Text2 文本定义自己的字体和颜色方案 在 setBorder 方法中。 java新手,在SUN教程中找不到它。

我的代码

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

问候 西蒙

Is there a way I can define my own font and color schemes for Text1 AND Text2 text
within the setBorder method. New to java and cannot find it in the SUN tutorials.

My code

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

Regards
Simon

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

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

发布评论

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

评论(6

往昔成烟 2024-10-17 09:18:38
setBorder(BorderFactory.createTitledBorder(null, "text", TitledBorder.CENTER, TitledBorder.BOTTOM, new Font("times new roman",Font.PLAIN,12), Color.yellow));

第一个参数 null 或另一个边框(对于复合边框)
您显示的第二个参数文本
中文本的位置

第 3 个和第 4 个参数的对齐方式以及第 2 个参数和第 4 个参数
第 5 个参数是设置字体和颜色的两个参数

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 2

4th param
and 5th param are the two to set font and color

明天过后 2024-10-17 09:18:38

如果您希望同一字符串中的每个字符串(例如 Text1Text2)使用不同的字体和颜色TitledBorder,您可能需要扩展 AbstractBorder 并覆盖 paintBorder()。现有的实现对于单个标题只有一种字体和一种颜色。

If you want a different font and color for each of the strings (e.g. Text1 and Text2) in the same TitledBorder, you may be need to extend AbstractBorder and override paintBorder(). The existing implementation only has one font and one color for a single title.

梦初启 2024-10-17 09:18:38

文字字体:

((javax.swing.border.TitledBorder) panel_1.getBorder()).setTitleFont(new Font("Tahoma", Font.PLAIN, 20));

文字颜色:

((javax.swing.border.TitledBorder)panel_1.getBorder()).setTitleColor(Color.WHITE);

Text Font:

((javax.swing.border.TitledBorder) panel_1.getBorder()).setTitleFont(new Font("Tahoma", Font.PLAIN, 20));

Text Color:

((javax.swing.border.TitledBorder)panel_1.getBorder()).setTitleColor(Color.WHITE);
像你 2024-10-17 09:18:38

如果您是 Java 和 Swing 的新手,那么用于执行此操作的 JavaDocs 有点让人不知所措。 BorderFactory 的 JavaDocs 位于:http:// download.oracle.com/javase/1.5.0/docs/api/javax/swing/BorderFactory.html

下面是将无衬线字体的文本设置为红色的示例:

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

}

The JavaDocs for doing this are somewhat overwhelming if you are new to Java and Swing. The JavaDocs for BorderFactory are here: http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/BorderFactory.html

Here's an example of making the text red in a sans serif font:

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

}
﹉夏雨初晴づ 2024-10-17 09:18:38

我知道这是一个老问题。
我想我想复活它,因为也许有人知道如何解决这个问题。我只有“部分解决方案”。

我很快就实现了你想要的边界。我重用了 Java 提供的功能,即 swing 组件中 HTML 的解释。

一切都很顺利,对于纯文本或 HTML 文本,边框绘制得很好,但您尝试为文本使用不同字体大小的情况除外。

我不知道如何解决这个问题。但我对解决方案非常感兴趣。

我知道该过程是在计算 textLengthInPixels 变量时将每个字符串以其自己的字体大小的宽度相加。

问题是我不知道如何获取它,也许是从视图中获取它,但不知道如何获取?



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

I know it is an old question.
Thought I would like to resurrect it as maybe someone knows how to solve this problem. I have only 'a partial solution'.

I have very quickly implemented the border which does what you want. I have reused what Java gives, i.e. interpretation of HTML in swing components.

All works sweet, the border is painted fine for a plain or HTML text, with exception for a situation where you are trying to have different font sizes for the texts.

I do not have idea how to solve this issue. But I am very much interested in a solution.

I know the procedure would be to sum up width of each string in its own font size when calculating the textLengthInPixels variable.

The problem is that I do not know how to get it, maybe from the View, but no idea how?



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

眸中客 2024-10-17 09:18:38

试试这个:

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

Try this:

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