奇怪的BoxLayout绘制

huangapple go评论107阅读模式
英文:

Weird BoxLayout painting

问题

我尝试按行显示一个对象,其中每一行都是一个对象。然后我使用了一个框布局,但这样做时只有顶部图层(也就是最后一个被调用的对象)被绘制,如何修复这个问题?

这是一个最小的示例:

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class Main {
  4. public static void main(String[] args) {
  5. MainFrame frame = new MainFrame();
  6. }
  7. }
  8. private class MainFrame extends JFrame {
  9. public MainFrame(){
  10. setSize(600,400);
  11. setLocationRelativeTo(null);
  12. setVisible(true);
  13. setContentPane(new Container());
  14. }
  15. }
  16. private class Container extends JPanel {
  17. public Container(){
  18. setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
  19. for(int i =0;i<10;i++){
  20. add(new Line());
  21. }
  22. }
  23. }
  24. private class Line extends JPanel {
  25. @Override
  26. protected void paintComponent(Graphics g) {
  27. super.paintComponent(g);
  28. g.setColor(Color.green);
  29. g.fillRect(getX(),getY(),getWidth(),getHeight());
  30. }
  31. }

这是我得到的效果:

奇怪的BoxLayout绘制

这是我期望的效果:

奇怪的BoxLayout绘制

一些测试(在JBackPannel类的paint方法中的蓝色矩形)向我展示了某处正在绘制一个白色画布,但我不知道是在哪里以及为什么...

这是一个最小示例的链接

英文:

I was trying to dysplay an object line by line where each line is an object. Then I use a box Layout but when I do so only the top layer (which is the last object called) is drawed how can I fiw this ?

here is a minimal exemple :

  1. import javax.swing.*;
  2. import java.awt.*;
  3. public class Main {
  4. public static void main(String[] args) {
  5. MainFrame frame = new MainFrame();
  6. }
  7. }
  8. private class MainFrame extends JFrame {
  9. public MainFrame(){
  10. setSize(600,400);
  11. setLocationRelativeTo(null);
  12. setVisible(true);
  13. setContentPane(new Container());
  14. }
  15. }
  16. private class Container extends JPanel {
  17. public Container(){
  18. setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
  19. for(int i =0;i&lt;10;i++){
  20. add(new Line());
  21. }
  22. }
  23. }
  24. private class Line extends JPanel {
  25. @Override
  26. protected void paintComponent(Graphics g) {
  27. super.paintComponent(g);
  28. g.setColor(Color.green);
  29. g.fillRect(getX(),getY(),getWidth(),getHeight());
  30. }
  31. }

here is what I've got :

奇怪的BoxLayout绘制

and what I've expected :

奇怪的BoxLayout绘制

Some test (the blue rectangle in the paint method of the JBackPannel class) showed me that somewhere something is drawing a white canvas, but I don't know where and why ...

here is a link to a minimal exemple

答案1

得分: 2

自定义绘制始终相对于组件而不是父面板中组件的位置进行。所以:

  1. g.fillRect(getX(),getY(),getWidth(),getHeight());

应该是:

  1. g.fillRect(0, 0, getWidth(), getHeight());
英文:

Custom painting is always done relative to the component, not the components location in the parent panel.

So:

  1. g.fillRect(getX(),getY(),getWidth(),getHeight());

should be:

  1. g.fillRect(0, 0, getWidth(), getHeight());

huangapple
  • 本文由 发表于 2020年8月5日 05:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63254992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定