簡體   English   中英

BorderFactory和Metal L&F問題

[英]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()方法。 這就是為什么調整框架大小也會導致問題的原因。 您正在重新創建所有組件。

解決方案:

  1. 不要重寫paintComponent()方法!
  2. 在類的構造函數中創建所有組件
  3. 不要使用空布局。 而是使用面板上的GridLayout並將正方形添加到網格中。
  4. 在構造函數中使用setBackground(Color.LIGHT_GRAY)設置背景

也:

  1. 擺脫方法上所有已synchronized關鍵字。 不需要它們。
  2. 不要使用“ ==”來比較對象。 使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM