Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-按顺序设置字体/颜色_Java_Swing_Fonts_Colors_Jpanel - Fatal编程技术网

Java-按顺序设置字体/颜色

Java-按顺序设置字体/颜色,java,swing,fonts,colors,jpanel,Java,Swing,Fonts,Colors,Jpanel,有没有一种方法可以为Text1和Text2文本定义自己的字体和颜色方案 在setboorder方法中。java新手,在SUN教程中找不到 我的代码 //Create Positions Table JPanel SpreadPanel = new JPanel(); SpreadPanel.setBorder(BorderFactory.createTitledBorder(" Text 1 Text 2")); 问候 Simon如果您想要为同一标题边框中的每个字符串(例如Text1

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

我的代码

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

如果您想要为同一
标题边框中的每个字符串(例如
Text1
Text2
)使用不同的字体和颜色,您可能需要扩展和覆盖
paintBorder()
。现有的实现对于一个标题只有一种字体和一种颜色。

如果您不熟悉Java和Swing,那么用于此操作的Javadoc会让您有些不知所措。BorderFactory的JavaDocs如下:

下面是一个在无衬线字体中使文本变为红色的示例:

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

}

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

我已经很快实施了边界,这符合你的要求。我重用了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);
    }
}


导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Dimension;
导入java.awt.Font;
导入java.awt.FontMetrics;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Insets;
导入java.awt.Rectangle;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.border.AbstractBorder;
导入javax.swing.border.border;
导入javax.swing.border.LineBorder;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.View;
公共类多色标题顺序扩展了AbstractBorder
{
私有静态最终长serialVersionUID=1L;
私人标签;
私有内部厚度TOP=10;
私人边界;
私有int-thicknessleet=0;
私有int thicknessRight=0;
私有int-thicknessBottom=0;
公共多色标题顺序(字符串标题)
{
this.label=新的JLabel(标题);
thicknessTop=label.getPreferredSize().height;
}
公共多色标题顺序(字符串标题、边框)
{
这(标题),;
this.border=边界;
thicknessLeft=border.getBorderInsets(null).left;
thicknessRight=border.getBorderInsets(null).right;
thicknessBottom=border.getBorderInsets(null).bottom;
}
@凌驾
公共边界(组件c、图形g、整数x、整数y、整数宽度、整数高度)
{
图形2d g2=(图形2d)g;
视图=(视图)label.getClientProperty(“html”);
String text=label.getText();
FontMetrics fm=g2.getFontMetrics(label.getFont());
int bY=y+fm.getAscent()-((fm.getAscent()+fm.getDescent())/2;
如果(边框!=null)
{
Insets in=border.getBorderInsets(c);
g2.设置夹(x,y,厚度左*2,高度);
边框。油漆边框(c、g、x、bY、宽度、高度-bY);
尝试
{
如果(视图!=null)
text=view.getDocument().getText(0,view.getDocument().getLength());
}捕获(BadLocationException ex)
{
Logger.getLogger(multicolortileborder.class.getName()).log(Level.SEVERE,null,ex);
}
int textlenghthinpixels=fm.stringWidth(文本);
System.out.println(“textlenghthinpixels=“+textlenghthinpixels”);
g2.设置剪辑(x+厚度左*2+文本长度像素,y,宽度-厚度左*2-文本长度像素,高度);
边框。油漆边框(c、g、x、bY、宽度、高度-bY);
int bottomIn=in.bottom;
g2.设置夹(x,高度-底进,宽度,底进);
边框。油漆边框(c、g、x、bY、宽度、高度-bY);
g2.设置夹(x、y、宽度、高度);
}
如果(视图!=null)
视图.绘制(g2,新矩形(x+厚度左*2,y,宽度-厚度左*2,高度));
其他的
{
Font-prevFont=g2.getFont();
g2.setFont(label.getFont());
g2.抽绳(文本,x+厚度左*2,fm.getAscent());
g2.setFont(prevFont);
}
}
@凌驾
公共插图getBorderInsets(组件c)
{
返回新插图(厚度顶部、厚度左侧、厚度底部、厚度右侧);
}
@凌驾
公共插图getBorderInsets(组件c,插图插图插图)
{
insets.top=厚度top;
插图左=厚度左;
插图右=厚度右;
插图.底部=厚度底部;
返回插图;
}
@凌驾
公共布尔值isborder不透明()
{
返回false;
}
公共静态void main(字符串[]args)
{
JPanel p=新的JPanel();
p、 setPreferredSize(新尺寸(200200));
String title=“Text 1 Text 2试试这个:

.setBorder(UIManager.getBorder("TextField.border"));
第一个参数null或另一个边框(用于复合边框) 正在显示的第二个参数文本 第3和第4参数对正以及参数2中文本的位置

第四参数 第五个参数是设置字体和颜色的两个参数

文本字体:

((javax.swing.border.TitledBorder) panel_1.getBorder()).setTitleFont(new Font("Tahoma", Font.PLAIN, 20));
文本颜色:

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