如何在jPanel上设置边框?

2022-09-02 05:31:45

我的项目有两个类,GoBoard扩展了JPanel。

GoTest.java:

import javax.swing.*;
import java.awt.Graphics;
import java.io.*;
import java.awt.*;

import javax.swing.border.Border;
import javax.swing.border.LineBorder;

class GoTest{
    private static void initGui(){
        JFrame frame = new JFrame("GoBoard");
        GoBoard jboard = new GoBoard();
        jboard.setLayout(new BorderLayout(10,10));
        jboard.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
        frame.add(jboard);



        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                initGui();
            }
        });
    }
}

GoBoard.java:

import javax.swing.*;
import java.awt.Graphics;
import javax.swing.border.Border;
class GoBoard extends JPanel{
    private int linien;

    public GoBoard(){
        this(9);    
    }

    public GoBoard(int pLinien){
        this.linien = pLinien;
        this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        int d = 0;
        int h = 0;
        for(int i = 0; i < this.linien; i++){
            g.drawLine(0,h, getWidth(), h);
            g.drawLine(d,0,d,getHeight());
            h += getHeight()/this.linien;
            d +=getWidth()/this.linien;
        }
    }
}

我想根据显示面板的框架设置边框以具有填充。但是我没有边界。有什么想法吗?


答案 1
JPanel jPanel = new JPanel();

jPanel.setBorder(BorderFactory.createLineBorder(Color.black));

这里不仅jPanel,您还可以为任何Jcomponent添加边框


答案 2

问题可能出在两个构造函数重载上,一个设置边界,另一个不设置边界:

public GoBoard(){
    this.linien = 9;
    this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
}

public GoBoard(int pLinien){
    this.linien = pLinien;

}

如果使用第二个构造函数创建 GoBoard 对象并传递 int 参数,则不会创建空边框。若要解决此问题,请考虑更改此设置,以便两个构造函数都设置边框:

// default constructor
public GoBoard(){
    this(9);  // calls other constructor
}

public GoBoard(int pLinien){
    this.linien = pLinien;
    this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); 
}

编辑 1:您添加的边框更多用于控制如何将组件添加到 JPanel。如果要绘制一个 JPanel,但在绘图周围有边框,请考虑将此 JPanel 放入另一个 JPanel 中,后者是一个具有边框的固定 JPanel。例如,

class GoTest {
   private static final int JB_WIDTH = 400;
   private static final int JB_HEIGHT = JB_WIDTH;

   private static void initGui() {
      JFrame frame = new JFrame("GoBoard");
      GoBoard jboard = new GoBoard();
      jboard.setLayout(new BorderLayout(10, 10));

      JPanel holdingPanel = new JPanel(new BorderLayout());
      int eb = 20;
      holdingPanel.setBorder(BorderFactory.createEmptyBorder(0, eb, eb, eb));
      holdingPanel.add(jboard, BorderLayout.CENTER);
      frame.add(holdingPanel, BorderLayout.CENTER);
      jboard.setPreferredSize(new Dimension(JB_WIDTH, JB_HEIGHT));

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //!! frame.setSize(400, 400);
      frame.pack();
      frame.setVisible(true);
   }

// .... etc....

推荐