我的问题:我有一个RoundedPanel
类扩展JPanel
并覆盖paintComponent()
方法,以便在面板周围绘制圆角。我创建了一个MyPanel
类,扩展了RoundedPanel
。
当我使用MyPanel
布局将面板放在FlowLayout
上时,会出现圆角边框。
为了调整面板上的组件大小以填充最大空间,我将FlowLayout
替换为Borderlayout
。但是,从不调用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());
}
}
答案 0 :(得分:4)
看起来该字段占据了面板的所有空间。如果子项覆盖了所有父项,则父项不可见,则无需调用paintComponent(),因此不会调用它。 尝试在内容中添加空边框。