Swing JPanel 设置画布背景及自己绘图

构造器中添加

setBackground(Color.WHITE);


重新画图中添加

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);


// 转换画笔为2D

Graphics2D g2d = (Graphics2D) g;


// 设置画笔粗细

g2d.setStroke(new BasicStroke(5.0f));


// 设置画笔颜色

g2d.setColor(Color.GRAY);

// 设置画笔字体
g2d.setFont(new Font("宋体", Font.BOLD, 14));

}

你可能感兴趣的:(Swing,JAVA开发专栏)