有没有办法为Text1和Text2文本定义自己的字体和颜色方案 在setBorder方法中。 java新手,在SUN教程中找不到它。
我的代码
//Create Positions Table
JPanel SpreadPanel = new JPanel();
SpreadPanel.setBorder(BorderFactory.createTitledBorder(" Text 1 Text 2"));
此致 西蒙
答案 0 :(得分:5)
setBorder(BorderFactory.createTitledBorder(null, "text", TitledBorder.CENTER, TitledBorder.BOTTOM, new Font("times new roman",Font.PLAIN,12), Color.yellow));
第一个参数null或另一个边框(对于复合边框) 您正在显示的第二个参数文本 参数2的第3和第4个参数证明和文本的位置
第四个参数 和第五个参数是设置字体和颜色的两个答案 1 :(得分:3)
如果您想要相同 {{1}中的每个字符串(例如Text1
和Text2
)的不同的字体和颜色您可能需要扩展AbstractBorder
并覆盖TitledBorder
。现有实现只有一种字体和一种颜色用于单个标题。
答案 2 :(得分:1)
文字字体:
((javax.swing.border.TitledBorder) panel_1.getBorder()).setTitleFont(new Font("Tahoma", Font.PLAIN, 20));
文字颜色:
((javax.swing.border.TitledBorder)panel_1.getBorder()).setTitleColor(Color.WHITE);
答案 3 :(得分:0)
如果您不熟悉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);
}
}
答案 4 :(得分:0)
我知道这是一个老问题。 以为我想复活它,因为也许有人知道如何解决这个问题。我只有'部分解决方案'。
我很快就实现了你想做的边框。我重用了Java提供的内容,即在swing组件中解释HTML。
所有作品都很甜美,边框适用于普通文字或HTML文字,但您尝试使用不同字体大小的情况除外。
我不知道如何解决这个问题。但我对解决方案非常感兴趣。
我知道在计算textLengthInPixels变量时,程序将是用自己的字体大小来计算每个字符串的宽度。
问题是我不知道如何获取它,可能来自View,但不知道怎么做?
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);
}
}
答案 5 :(得分:0)
试试这个:
.setBorder(UIManager.getBorder("TextField.border"));