设置 BorderLayout 并调用 PaintComponent()
我的问题:我有一个 RoundedPanel
类,它扩展 JPanel
并重写 paintComponent()
方法,以便在面板周围绘制圆角。我创建了一个扩展 RoundedPanel
的 MyPanel
类。
当我使用 FlowLayout
布局将面板放在 MyPanel
上时,会出现圆形边框。 为了调整面板上组件的大小以使其填充最大空间,我用 Borderlayout
替换了 FlowLayout
。但随后,paintComponent()
方法从未被调用,并且圆角边框不再出现。
有谁知道如何创建一个圆形面板,当调整窗口大小时,其组件会被拉伸以填充空间?有人可以解释一下为什么使用 Borderlayout 时不调用 paintComponent()
方法吗?
提前致谢。
代码:
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Demo extends JFrame {
public static final int WINDOW_WIDTH = 540;
public static final int WINDOW_HEIGHT = 540;
private static MyPanel myPanel;
private Demo() {
super();
myPanel = new MyPanel();
getContentPane().add(myPanel);
setSize(new Dimension(500, 500));
setPreferredSize(new Dimension(500, 500));
setVisible(true);
}
public static void main(String[] args) {
new Demo();
}
}
class MyPanel extends RoundedPanel {
private JPanel rootPanel = new JPanel();
public MyPanel() {
super();
rootPanel = buildRootPanel();
rootPanel.setPreferredSize(new Dimension(400, 400));
rootPanel.setBackground(Color.RED);
add(rootPanel);
// When uncommented, the Textfield is correctly resized to fit all available space , but the rounder border doesnt appear
//setLayout(new BorderLayout());
//add(rootPanel, BorderLayout.CENTER);
}
private JPanel buildRootPanel() {
rootPanel.setLayout(new BorderLayout());
JTextField field = new JTextField();
rootPanel.add(field, BorderLayout.CENTER);
return rootPanel;
}
}
class RoundedPanel extends JPanel {
protected int strokeSize = 1;//Stroke size. it is recommended to set it to 1 for better view
private Color strokeColor = Color.BLACK;
protected Color shadowColor = Color.black;//Color of shadow
protected boolean shady = true;// Sets if it drops shadow
protected boolean highQuality = true;// Sets if it has an High Quality view
protected Dimension arcs = new Dimension(20, 20);// Double values for Horizontal and Vertical radius of corner arcs
protected int shadowGap = 5;// Distance between shadow border and opaque panel border
protected int shadowOffset = 4; // The offset of shadow.
protected int shadowAlpha = 150;// The transparency value of shadow. ( 0 - 255)
public RoundedPanel() {
super();
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
System.out.println("++++++++++++++ in paintComponent");
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int shadowGap = this.shadowGap;
Color shadowColorA = new Color(shadowColor.getRed(), shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
Graphics2D graphics = (Graphics2D) g;
//Sets antialiasing if HQ.
if (highQuality) {
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
//Draws shadow borders if any.
if (shady) {
graphics.setColor(shadowColorA);
graphics.fillRoundRect(
shadowOffset,// X position
shadowOffset,// Y position
width - strokeSize - shadowOffset, // width
height - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
} else {
shadowGap = 1;
}
//Draws the rounded opaque panel with borders.
graphics.setColor(getBackground());
graphics.fillRoundRect(0, 0, width - shadowGap, height - shadowGap, arcs.width, arcs.height);
//graphics.setColor(getForeground());
graphics.setColor(strokeColor);
graphics.setStroke(new BasicStroke(strokeSize));
graphics.drawRoundRect(0, 0, width - shadowGap, height - shadowGap, arcs.width, arcs.height);
//Sets strokes to default, is better.
graphics.setStroke(new BasicStroke());
}
}
My problem : I have a RoundedPanel
class that extends JPanel
and overrides paintComponent()
method in order to draw rounded corners around the panel. I created a MyPanel
class that extends RoundedPanel
.
When I put a panel on MyPanel
using FlowLayout
layout, the rounded border appears.
In order for the components on panel to be resized so that they fill the maximum space, I replaced the FlowLayout
by a Borderlayout
. But then , the paintComponent()
method is never called and the rounded border doesn't appear anymore.
Does anyone knows how to create a rounded panel whose components are stretched to fill the space when the window is resized? Could someone explain me why the paintComponent()
method is not called when using Borderlayout ?
Thanks in advance.
The code :
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Demo extends JFrame {
public static final int WINDOW_WIDTH = 540;
public static final int WINDOW_HEIGHT = 540;
private static MyPanel myPanel;
private Demo() {
super();
myPanel = new MyPanel();
getContentPane().add(myPanel);
setSize(new Dimension(500, 500));
setPreferredSize(new Dimension(500, 500));
setVisible(true);
}
public static void main(String[] args) {
new Demo();
}
}
class MyPanel extends RoundedPanel {
private JPanel rootPanel = new JPanel();
public MyPanel() {
super();
rootPanel = buildRootPanel();
rootPanel.setPreferredSize(new Dimension(400, 400));
rootPanel.setBackground(Color.RED);
add(rootPanel);
// When uncommented, the Textfield is correctly resized to fit all available space , but the rounder border doesnt appear
//setLayout(new BorderLayout());
//add(rootPanel, BorderLayout.CENTER);
}
private JPanel buildRootPanel() {
rootPanel.setLayout(new BorderLayout());
JTextField field = new JTextField();
rootPanel.add(field, BorderLayout.CENTER);
return rootPanel;
}
}
class RoundedPanel extends JPanel {
protected int strokeSize = 1;//Stroke size. it is recommended to set it to 1 for better view
private Color strokeColor = Color.BLACK;
protected Color shadowColor = Color.black;//Color of shadow
protected boolean shady = true;// Sets if it drops shadow
protected boolean highQuality = true;// Sets if it has an High Quality view
protected Dimension arcs = new Dimension(20, 20);// Double values for Horizontal and Vertical radius of corner arcs
protected int shadowGap = 5;// Distance between shadow border and opaque panel border
protected int shadowOffset = 4; // The offset of shadow.
protected int shadowAlpha = 150;// The transparency value of shadow. ( 0 - 255)
public RoundedPanel() {
super();
setOpaque(false);
}
@Override
protected void paintComponent(Graphics g) {
System.out.println("++++++++++++++ in paintComponent");
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int shadowGap = this.shadowGap;
Color shadowColorA = new Color(shadowColor.getRed(), shadowColor.getGreen(), shadowColor.getBlue(), shadowAlpha);
Graphics2D graphics = (Graphics2D) g;
//Sets antialiasing if HQ.
if (highQuality) {
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
//Draws shadow borders if any.
if (shady) {
graphics.setColor(shadowColorA);
graphics.fillRoundRect(
shadowOffset,// X position
shadowOffset,// Y position
width - strokeSize - shadowOffset, // width
height - strokeSize - shadowOffset, // height
arcs.width, arcs.height);// arc Dimension
} else {
shadowGap = 1;
}
//Draws the rounded opaque panel with borders.
graphics.setColor(getBackground());
graphics.fillRoundRect(0, 0, width - shadowGap, height - shadowGap, arcs.width, arcs.height);
//graphics.setColor(getForeground());
graphics.setColor(strokeColor);
graphics.setStroke(new BasicStroke(strokeSize));
graphics.drawRoundRect(0, 0, width - shadowGap, height - shadowGap, arcs.width, arcs.height);
//Sets strokes to default, is better.
graphics.setStroke(new BasicStroke());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来该字段占据了面板的所有空间。如果子级覆盖了所有父级并且父级不可见,则无需调用paintComponent(),因此不会调用它。
尝试为内容添加空边框。
Looks like the field occupies all the space of the panel. If child covers all the parent and parent isn't visible no need to call the paintComponent() so it's not called.
Try to add empty border to the content.