![](/img/trans.png)
[英]Java L&F customization: How do I use synth to customize BorderFactory borders?
[英]BorderFactory and Metal L&F issues
我看到一种奇怪的行为。 L&F集为Metal。
UIManager.setLookAndFeel( “javax.swing.plaf.metal.MetalLookAndFeel中”);
如果我在JPanel上设置Border border1,则在同一JPanel上将Border更改为border2,当工具提示通过border2时,重绘会重绘border1。 这仅在具有LineBorder,TitledBorder的L&F Metal上发生。
仅在Metal L&F中发生。
即使您没有专门设置LAF,您仍然会遇到问题。
如果我在JPanel上设置Border border1,则在同一JPanel上将Border更改为border2,当工具提示通过border2时,重绘会重绘border1
它与工具提示无关。
只需单击几个正方形,然后调整框架大小,所有“边框”都将重新绘制为蓝色。
问题是您的ColorsBoard
类。
您不应该重写paintComponent()
方法来创建组件。
绘画方法仅用于绘画。
每当Swing确定需要重新绘制组件时,都会调用paintComponent()方法。 这就是为什么调整框架大小也会导致问题的原因。 您正在重新创建所有组件。
解决方案:
setBackground(Color.LIGHT_GRAY)
设置背景 也:
synchronized
关键字。 不需要它们。 equals(...)
方法。 public class BorderTest {
private JFrame jFrame;
private Container contentPane;
private ColorsBoard colorsBoard;
public BorderTest() {
super();
ToolTipManager.sharedInstance().setInitialDelay(10);
ToolTipManager.sharedInstance().setDismissDelay(1500);
jFrame = new JFrame();
contentPane = jFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
jFrame.setPreferredSize(new Dimension(700, 500));
colorsBoard = new ColorsBoard();
contentPane.add(colorsBoard, BorderLayout.CENTER);
JLabel label = new JLabel(""
+ "<html>Click two or three small squares. <br/>"
+ "LineBorder's are set. <br/>"
+ "Then pass the tooltips over the red borders. <br/>"
+ "The red LineBorders' are back to blue. <br/>"
+ "This phenomen appears only in Metal L&F. </html>");
contentPane.add(label, BorderLayout.EAST);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
new BorderTest();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ColorsBoard extends JPanel {
private int squareSize = 315;
private int horizontal = 8; //number of squares H
private int vertical = 8; //number of squares V
private SquaredPanel[] squarePanels;
int index = 0;
public int lastClickedSquare = -1;
// the chess board like JPanel.
public ColorsBoard() {
squarePanels= new SquaredPanel[horizontal * vertical];
this.setPreferredSize(new Dimension(squareSize, squareSize));
setLayout(null);
}
// fill with squares (SquaredPanel)
protected synchronized void paintComponent(Graphics g) {
super.paintComponent(g);
int tileSizeH = squareSize / horizontal;
int tileSizeV = squareSize / vertical;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
for(int i = 0; i < horizontal*vertical; i++) {
squarePanels[i] = new SquaredPanel(this);
}
index = 0;
for (int i = 0; i < horizontal; i++) {
for (int j = 0; j < vertical; j++) {
if(index == horizontal * vertical) break;
squarePanels[index].setBackground(Color.gray);
squarePanels[index].setSize(tileSizeH - 1, tileSizeV - 1);
squarePanels[index].setLocation(i * tileSizeH, j * tileSizeV);
squarePanels[index].setName("index " + index);
squarePanels[index].setIndex(index);
add(squarePanels[index]);
index++;
}
}
}
public void eraseLastBlueBorder(int lastClickedSquare2) {
if (lastClickedSquare == -1) {
lastClickedSquare = lastClickedSquare2;
return;
}
if(!squarePanels[lastClickedSquare].isRed)(squarePanels[lastClickedSquare]).cleanBorder();
lastClickedSquare = lastClickedSquare2;
}
}
class SquaredPanel extends JPanel {
private String name;
protected Boolean isRed = false;
private int index;
private Border theBorder = (new LineBorder(Color.gray, 2));
protected Border getTheBorder() {
return theBorder;
}
public SquaredPanel(ColorsBoard colorsBoard) {
super();
addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
colorsBoard.eraseLastBlueBorder(index);
setTitleBorder("RED");
}
public void mouseEntered(MouseEvent e) {
setToolTipText(name);
}
public void mouseExited(MouseEvent e) {
setToolTipText(null);
}
});
}
// the setBorder call
protected void setTitleBorder(String title) {
theBorder = (new LineBorder(title == "BLUE" ? Color.red : Color.blue, 2));
setBorder(theBorder);
}
public synchronized void cleanBorder() {
setTitleBorder("BLUE");
}
public void setName(String name) {
this.name = name;
}
public void setIndex(int k) {
index = k;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.