gpt4 book ai didi

java - 组件以不寻常的方式呈现

转载 作者:行者123 更新时间:2023-11-30 02:49:47 25 4
gpt4 key购买 nike

我有一个自定义 JPanel 类,我希望它在其子组件前面绘制几个矩形和文本。我已经重写了这个方法:

public void paint(Graphics g){
g = getComponentGraphics(g);
super.paint(g);
//paint my custom content
}

super.paint 调用 paintChildren 来绘制子组件。但是子组件出现在我的自定义内容前面,而且它们有时也会互相争斗。

我完全不知道是什么原因造成的。

注意:我在代码中使用 setComponentZOrder,并且我的 JPanel 发生在 JScrollPane 中。

编辑:组件 ZFight 即使我从未调用 setComponentZOrder 方法。

编辑2:

    import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleSelection;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JPanel{
private static final long serialVersionUID = 1L;
public static void main(String[] atgs){
JFrame frame = new JFrame("ZFightingExample");
frame.setSize(new Dimension(500,500));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ExamplePanel panel = new ExamplePanel();
frame.add(panel);
Example a = new Example(new Rectangle(5,5,50,50)),
b = new Example(new Rectangle(40,40,50,50));
panel.add(a);
panel.add(b);
frame.setVisible(true);
}
public Example(Rectangle bounds){
super();
setBounds(bounds);
}
public void paint(Graphics g){
super.setBackground(Color.GREEN);
g.fillRect(0, 0, getWidth()-1, getHeight()-1);
super.paint(g);
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
}
}
class ExamplePanel extends JPanel{
private static final long serialVersionUID = 1L;
public ExamplePanel(){
super(null);
accessibleContext = new Accessiblecontext();
};
protected class Accessiblecontext extends AccessibleJPanel implements AccessibleSelection{
private static final long serialVersionUID = 1L;
public int getAccessibleSelectionCount() {return 2;}
public Accessible getAccessibleSelection(int i) {return (Accessible)getComponent(i);}
public boolean isAccessibleChildSelected(int i) {return true;}
public void addAccessibleSelection(int i) {}
public void removeAccessibleSelection(int i) {}
public void clearAccessibleSelection() {}
public void selectAllAccessibleSelection() {}
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.BLUE);//Should be in front of the Green boxes...
g.drawRect(10, 10, 75, 75);
}
}

最佳答案

I want it to paint several rectangles and texts in front of it's child components.

通常你会重写paintCompnent()来进行自定义绘画。

问题是“在前面”对您来说意味着什么?如果它的意思是我认为的意思,那么您需要在绘制子组件后进行绘制。所以你重写paint()的基本方法是正确的。

public void paint(Graphics g){
g = getComponentGraphics(g);
super.paint(g);
//paint my custom content
}

上述代码的问题在于您没有使用传递给绘画方法的 Graphics 对象。所以你的代码应该是:

public void paint(Graphics g)
{
//g = getComponentGraphics(g);
super.paint(g); // use the Graphics object passed to the method

//paint my custom content

g.drawRect(10, 10, 20, 20);
}

I use setComponentZOrder in my code,

你为什么玩 ZOrder?

编辑:

g.setColor(Color.BLUE);//Should be in front of the Green boxes...

正如我在回答中首先指出的,通常(99% 的时间)自定义绘画是通过覆盖面板的 PaintComponent() 方法来完成的。

问题出在示例类上:

  1. 覆盖paintComponent()而不是paint()
  2. 重写绘画方法时的第一个语句应该是 super.???.
  3. 不要在绘画方法中设置类的属性。

所以代码可能看起来像这样:

public Example(Rectangle bounds){
super();
setBounds(bounds);
setBackground(Color.GREEN);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(getBackground());
g.fillRect(0, 0, getWidth()-1, getHeight()-1);
g.setColor(Color.BLACK);
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
}

请注意,您不需要使用示例类进行任何自定义绘制。相反,您的代码可能类似于:

ExamplePanel panel = new ExamplePanel();
panel.setLayout( null ); // now you set the size/location of any component you add to the panel

JPanel example1 = new JPanel();
example1.setBackground( Color.GREEN );
example1.setBorder( new LineBorder(Color.BLACK) );
example1.setBounds( new Rectangle(....) );

panel.add(example1);

此外,ExamplePanel 类的另一个解决方案可能是使用 JLayer。 JLayer 类允许您向组件添加各种精美的装饰。查看 How to Decorate Components With The JLayer Class 上的 Swing 教程.

关于java - 组件以不寻常的方式呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39054667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com