Java 无法绘制自定义组件
Java 无法绘制自定义组件,java,swing,awt,Java,Swing,Awt,我正在尝试创建一个可以识别为组件的类(称为Stock),并让我在其上使用Listener。 但是,除非您指定(这不是一个好主意!)在panel.paint()中绘制它,否则不会绘制它。 程序按如下方式运行: JFrame frame = new JFrame (".."); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); final Stock theStock=new St
我正在尝试创建一个可以识别为组件的类(称为Stock),并让我在其上使用Listener。
但是,除非您指定(这不是一个好主意!)在panel.paint()中绘制它,否则不会绘制它。
程序按如下方式运行:
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel();
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
除非主要(JPanel)被覆盖,以便有一个规范调用库存绘制,否则不会绘制库存: @Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.drawRect(x-2, y-2, 74, 124);
}
这是我的代码——我想问题可能会发生,因为我并没有重写从组件继承的paint方法之外的任何内容。
**********修改为可测试,请参见下文
**
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Stock extends Component{
private int x; //the position for image
private int y;//position for image
public Stock() {
x=285;
y=280;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.drawRect(x-2, y-2, 74, 124);
}
public void works(){
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel(){
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
theStock.paint(g);
}
};
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
public void doesNotWork(){
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel();
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Stock a = new Stock();
a.works();
//a.doesNotWork();
}
}
p、 我没有使用JComponent,因为JComponent的功能比我需要使用的更多。组件(容器和按钮从中继承)应该可以正常工作
p、 即使使用JComponent,也会得到相同的结果。代码如下
import java.awt.*;
导入javax.swing.*
@SuppressWarnings("serial")
public class Stock extends JComponent{
private int x; //the position for image
private int y;//position for image
public Stock() {
x=285;
y=280;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(x-2, y-2, 74, 124);
}
public void works(){
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel(){
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
theStock.paint(g);
}
};
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
public void doesNotWork(){
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel();
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Stock a = new Stock();
//a.works();
a.doesNotWork();
}
}
您的问题在于布局、大小和首选大小。由于您正在使用JPanel将your Stock组件添加到FlowLayout,因此该组件将按照其首选大小(0,0)调整自身大小。如果你给JPanel一个不同的布局,比如
primary.setLayout(new BorderLayout());
然后添加你的Stock对象,它将填充主JPanel并绘制自己
其他选项包括设置股票对象preferredSize,甚至覆盖其getPreferredSize()
方法,使其足够大:
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
你的主要方法是什么?你如何使用这门课?还有@Ashiquzzaman,他不能覆盖paintComponent,因为他的类(不知什么原因)没有扩展JComponent或JPanel,而是扩展AWT类组件?@hoverCraftfullOfels,很抱歉我没有注意到。@Ashiquzzaman:没问题,但我上面帖子中的其他问题仍然存在。K.Mole先生——请告诉我们您是如何使用这个类的,请告诉我们您为什么要使用过时的AWT组件类、组件,而不是不太过时的Swing组件类。@immibis:也许您是对的,main的表述方式是错误的,但上下文很重要,包括组件的位置和大小,它所添加的内容。最好的“为什么这段代码不起作用”类型的问题有一些主要的方法,这样我们就可以运行、修改和测试程序来找出错误。因此,我肯定希望在任何形状和形式中都有更多的上下文,最好是.BTW-这个自定义组件在流布局中的大小为0x0。。