如何创建修改现有边框的自定义边框类

时间:2013-11-12 02:28:18

标签: java swing border

我正在开发一个桌面RPG的图形工具,它将显示一个网格并允许用户更改网格的线条(即边框) - 网格的每个方格都是一个jpanel - 只需点击一下老鼠。

我目前的代码是这些代码:

if(quadrant ==2)
{
      setBorder(new CompoundBorder(getBorder(), new MatteBorder(0,0,3,0, Color.BLUE)));
}

如果在方形底部附近的点范围内单击鼠标,则底部边框将变为蓝色,其他边框将保持不变。此代码不起作用,因为它将新边框添加到旧边框。在随后点击底线时,蓝色边框将变得更高,因为新的蓝色边框位于前一个蓝色边框的顶部...直到方形填充为蓝色。最终目标是通过多次点击循环显示颜色 - 即,一次点击蓝色,两次点击绿色,三次点击将其恢复为默认黑色。

我的想法是创建一个新的自定义边框类,它接受getBorder(),但修改它以便一边没有边框。但我不知道如何实现这一目标。

已修改为添加完整代码:

TestGrid02类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

public class TestGrid02 {

public static void main(String[] args) {
    new TestGrid02();
}

public TestGrid02() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new TestPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

        }
    });
}
}

TestPane类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

 public class TestPane extends JPanel {
    Border border = null;
    public TestPane() {
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        for (int row = 0; row < 10; row++) {
            for (int col = 0; col < 10; col++) {
                gbc.gridx = col;
                gbc.gridy = row;

                CellPane cellPane = new CellPane();


                if (row < 9) {
                    if (col < 9) {
                        border = new MatteBorder(1, 1, 0, 0, Color.BLACK);
                    } else {
                        border = new MatteBorder(1, 1, 0, 1, Color.BLACK);
                    }
                } else {
                    if (col < 9) {
                        border = new MatteBorder(1, 1, 1, 0, Color.BLACK);
                    } else {
                        border = new MatteBorder(1, 1, 1, 1, Color.BLACK);
                    }
                }

                cellPane.setBorder(border);
                cellPane.setBackground(Color.WHITE);
                add(cellPane, gbc);
            }
        }
    }
}

CellPane类:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class CellPane extends JPanel {


    public CellPane() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Point p = e.getPoint();
                int quadrant = 0;
                if((p.x >= 0 && p.x <11) && p.y >= 0 && p.y < 41)
                {
                    quadrant = 1;
                }
                else if((p.x >= 0 && p.x <41) && p.y >= 40)
                {
                    quadrant = 2;
                }
                else if((p.x >= 10 && p.x <51) && p.y < 11)
                {
                 quadrant = 3;   
                }
                else if((p.x >40 && p.y >10))
                {
                    quadrant = 4;
                }
                else
                {
                    quadrant = 5;
                }


                if(e.getButton()== MouseEvent.BUTTON1)
                {
                if(quadrant==1)
                {
                    Border border = getBorder();
                    if (border instanceof CompoundBorder) { 
                    border = ((CompoundBorder)border).getOutsideBorder();
                    }
                    setBorder(new CompoundBorder(border, new MatteBorder(0,3,0,0, Color.BLUE)));
                }
                if(quadrant ==2)
                {
                    Border border = getBorder();
                    if (border instanceof CompoundBorder) { 
                    border = ((CompoundBorder)border).getOutsideBorder();
                    }
                    setBorder(new CompoundBorder(border, new MatteBorder(0,0,3,0, Color.BLUE)));
                }
                if(quadrant ==3)
                {
                    Border border = getBorder();
                    if (border instanceof CompoundBorder) { 
                    border = ((CompoundBorder)border).getOutsideBorder();
                    }
                    setBorder(new CompoundBorder(border, new MatteBorder(3,0,0,0, Color.BLUE)));
                }
                if(quadrant == 4)
                {
                    Border border = getBorder();
                    if (border instanceof CompoundBorder) { 
                    border = ((CompoundBorder)border).getOutsideBorder();
                    }
                    setBorder(new CompoundBorder(border, new MatteBorder(0,0,0,3, Color.BLUE)));
                }


                //original code. These borders "grow" on subsequent clicks
                /*
                if(quadrant==1)
                {

                    setBorder(new CompoundBorder(getBorder(), new MatteBorder(0,3,0,0, Color.BLUE)));
                }
                if(quadrant ==2)
                {

                    setBorder(new CompoundBorder(getBorder(), new MatteBorder(0,0,3,0, Color.BLUE)));
                }
                if(quadrant ==3)
                {

                    setBorder(new CompoundBorder(getBorder(), new MatteBorder(3,0,0,0, Color.BLUE)));
                }
                if(quadrant == 4)
                {

                    setBorder(new CompoundBorder(getBorder(), new MatteBorder(0,0,0,3, Color.BLUE)));
                }
                */

                if(quadrant == 5)
                {
                    //setBorder(new MatteBorder(3,3,3,3, Color.BLUE));      //Click in the middle to highlight all sides of a square
                }



                }                                       
                }


        });
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(50, 50);
    }
}

1 个答案:

答案 0 :(得分:1)

基本上,您需要检查Border并确定它是什么类型并从那里做出选择......

Border border = getBorder();
if (border instanceof CompoundBorder) { 
    border = ((CompoundBorder)border).getOutsideBorder();
}
setBorder(new CompoundBorder(border, new MatteBorder(0,0,3,0, Color.BLUE)));

基本上,这会检查border是否已经是CompoundBorder并抓住外边框(如果是)。然后,它会使用现有的外部Border和新的MatteBorder ...

创建新边框

虽然这不是没有问题......

根据对问题的更改进行了更新

基本上,你需要的是一些方法来“记住”点击之前的状态,并保持那些不会改变的状态。

基本上,我所做的是Insets。当用户点击时,我会检查quadrant并相应地修改insets,然后更新Border

public class CellPane extends JPanel {

    private Insets insets = new Insets(0, 0, 0, 0);

    public CellPane() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Point p = e.getPoint();
                int quadrant = 0;
                if ((p.x >= 0 && p.x < 11) && p.y >= 0 && p.y < 41) {
                    quadrant = 1;
                } else if ((p.x >= 0 && p.x < 41) && p.y >= 40) {
                    quadrant = 2;
                } else if ((p.x >= 10 && p.x < 51) && p.y < 11) {
                    quadrant = 3;
                } else if ((p.x > 40 && p.y > 10)) {
                    quadrant = 4;
                } else {
                    quadrant = 5;
                }

                if (e.getButton() == MouseEvent.BUTTON1) {
                    if (quadrant == 1) {
                        System.out.println("Left");
                        insets.left = insets.left == 0 ? 3 : 0;
                    }
                    if (quadrant == 2) {
                        System.out.println("Bottom");
                        insets.bottom = insets.bottom == 0 ? 3 : 0;
                    }
                    if (quadrant == 3) {
                        System.out.println("Top");
                        insets.top = insets.top == 0 ? 3 : 0;
                    }
                    if (quadrant == 4) {
                        System.out.println("Right");
                        insets.right = insets.right == 0 ? 3 : 0;
                    }

                    Border border = getBorder();
                    if (border instanceof CompoundBorder) {
                        border = ((CompoundBorder) border).getOutsideBorder();
                    }
                    setBorder(new CompoundBorder(border, new MatteBorder(insets, Color.BLUE)));

                    if (quadrant == 5) {
                        //setBorder(new MatteBorder(3,3,3,3, Color.BLUE));      //Click in the middle to highlight all sides of a square
                    }

                }
            }

        });
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(50, 50);
    }
}