无法将项目添加到自定义 JPanel
我的问题是我创建了一个 JPanel 来绘制渐变作为背景。但是当我向其中添加组件(如 JButton)时,它什么也没做...
请帮忙!
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CocoaTrashBar extends JPanel {
String titleText = "";
Color topLineColor = new Color(128, 128, 128);
Color bottomLineColor = new Color(69, 69, 69);
Color gradientTop = new Color(116, 116, 116);
Color gradientBottom = new Color(81, 81, 81);
public CocoaTrashBar() {
setDefaults();
}
public CocoaTrashBar(String title) {
setDefaults();
this.titleText = title;
}
public void setTitle(String title) {
this.titleText = title;
this.repaint();
}
private void setDefaults() {
super.setOpaque(true);
this.setPreferredSize(new Dimension(0, 24));
installWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
topLineColor = new Color(128, 128, 128);
bottomLineColor = new Color(69, 69, 69);
gradientTop = new Color(116, 116, 116);
gradientBottom = new Color(81, 81, 81);
repaintComponent();
}
public void windowLostFocus(WindowEvent e) {
topLineColor = new Color(171, 171, 171);
bottomLineColor = new Color(103, 103, 103);
gradientTop = new Color(156, 156, 156);
gradientBottom = new Color(121, 121, 121);
repaintComponent();
}
}, this);
}
private static void installWindowFocusListener(
WindowFocusListener focusListener, JComponent component) {
component.addPropertyChangeListener("Frame.active",
createFrameFocusPropertyChangeListener(focusListener, component));
}
private static PropertyChangeListener createFrameFocusPropertyChangeListener(
final WindowFocusListener focusListener, final JComponent component) {
return new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
Window window = SwingUtilities.getWindowAncestor(component);
boolean hasFocus = (Boolean) component.getClientProperty("Frame.active");
if (hasFocus) {
focusListener.windowGainedFocus(
new WindowEvent(window, WindowEvent.WINDOW_GAINED_FOCUS));
} else {
focusListener.windowLostFocus(
new WindowEvent(window, WindowEvent.WINDOW_LOST_FOCUS));
}
}
};
}
private void repaintComponent() {
this.repaint();
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
// Draw first line
g2d.setPaint(topLineColor);
g2d.drawLine(0, 0, this.getWidth(), 0);
// Draw last line
g2d.setPaint(bottomLineColor);
g2d.drawLine(0, 23, this.getWidth(), 23);
// Draw gradient
GradientPaint gradient = new GradientPaint(
this.getX(),
this.getY()+1,
gradientTop,
this.getX(),
this.getHeight()-1,
gradientBottom);
g2d.setPaint(gradient);
g2d.fillRect(this.getX(), this.getY()+1, this.getWidth(), this.getHeight()-2);
if(titleText != null) {
g2d.setFont(new Font("", Font.BOLD, 13));
g2d.setColor(Color.WHITE);
g2d.drawString(titleText, 10, 16);
}
g.dispose();
}
}
My issues is that I have created a JPanel that draws a gradient as a background. But when I go to add components to it (like a JButton) it does nothing...
Please help!
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class CocoaTrashBar extends JPanel {
String titleText = "";
Color topLineColor = new Color(128, 128, 128);
Color bottomLineColor = new Color(69, 69, 69);
Color gradientTop = new Color(116, 116, 116);
Color gradientBottom = new Color(81, 81, 81);
public CocoaTrashBar() {
setDefaults();
}
public CocoaTrashBar(String title) {
setDefaults();
this.titleText = title;
}
public void setTitle(String title) {
this.titleText = title;
this.repaint();
}
private void setDefaults() {
super.setOpaque(true);
this.setPreferredSize(new Dimension(0, 24));
installWindowFocusListener(new WindowFocusListener() {
public void windowGainedFocus(WindowEvent e) {
topLineColor = new Color(128, 128, 128);
bottomLineColor = new Color(69, 69, 69);
gradientTop = new Color(116, 116, 116);
gradientBottom = new Color(81, 81, 81);
repaintComponent();
}
public void windowLostFocus(WindowEvent e) {
topLineColor = new Color(171, 171, 171);
bottomLineColor = new Color(103, 103, 103);
gradientTop = new Color(156, 156, 156);
gradientBottom = new Color(121, 121, 121);
repaintComponent();
}
}, this);
}
private static void installWindowFocusListener(
WindowFocusListener focusListener, JComponent component) {
component.addPropertyChangeListener("Frame.active",
createFrameFocusPropertyChangeListener(focusListener, component));
}
private static PropertyChangeListener createFrameFocusPropertyChangeListener(
final WindowFocusListener focusListener, final JComponent component) {
return new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
Window window = SwingUtilities.getWindowAncestor(component);
boolean hasFocus = (Boolean) component.getClientProperty("Frame.active");
if (hasFocus) {
focusListener.windowGainedFocus(
new WindowEvent(window, WindowEvent.WINDOW_GAINED_FOCUS));
} else {
focusListener.windowLostFocus(
new WindowEvent(window, WindowEvent.WINDOW_LOST_FOCUS));
}
}
};
}
private void repaintComponent() {
this.repaint();
}
@Override
public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
// Draw first line
g2d.setPaint(topLineColor);
g2d.drawLine(0, 0, this.getWidth(), 0);
// Draw last line
g2d.setPaint(bottomLineColor);
g2d.drawLine(0, 23, this.getWidth(), 23);
// Draw gradient
GradientPaint gradient = new GradientPaint(
this.getX(),
this.getY()+1,
gradientTop,
this.getX(),
this.getHeight()-1,
gradientBottom);
g2d.setPaint(gradient);
g2d.fillRect(this.getX(), this.getY()+1, this.getWidth(), this.getHeight()-2);
if(titleText != null) {
g2d.setFont(new Font("", Font.BOLD, 13));
g2d.setColor(Color.WHITE);
g2d.drawString(titleText, 10, 16);
}
g.dispose();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于您的
paint
方法覆盖调用paintComponent
将您的 Paint 方法替换为:
the problem is with your
paint
method override callingpaintComponent
replace your paint method with this:
在方法
paint(Graphics g)
中,您的第一行是:这将绘制您的按钮。该方法的其余部分将继续在该按钮上进行绘制:
因此,您看不到任何按钮。把第一行作为最后一行,你应该就可以了。
In method
paint(Graphics g)
, your first line is:This will paint your button. The rest of that method will proceed to paint over that button:
Hence, you see no button. Make that first line the last line and you should be good.