gpt4 book ai didi

java - 如何在jPanel上设置边框?

转载 作者:太空狗 更新时间:2023-10-29 22:36:51 25 4
gpt4 key购买 nike

我的项目由两个类组成,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;
}
}
}

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

最佳答案

JPanel jPanel = new JPanel();

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

这里不仅是jPanel,你可以给任何Jcomponent添加边框

关于java - 如何在jPanel上设置边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5716390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com