Jcomponent paintcomponent未出现在面板上

时间:2017-11-01 03:26:39

标签: java swing

我正在制作一个简单的GUI,其中小方框应根据其x,y坐标显示在vals_list_low = [] vals_list_high = [] i = -1 for x, y, z in lst: if z == "low": vals_list_min.append(lst[:i]) if z == "high": vals_list_max.append(lst[:i]) i += 1 上。所以我在我的结构中有三个班级:
1:Jpanel,其中包含主MyFrame
2:JFrame
3:MyPanel extends JPanel

在我的Icon extends JComponent我希望有一个MyFrame我可以通过它打开一个X,Y坐标文件,在菜单栏下面我希望有MenuBar每个X,Y坐标的所有MyPanel。我遇到的第一个问题是,当我添加图标时,Icons不会出现在Icon上。

我的代码可以在下面看到:
  

MyPanel

代码用于保存图标的面板:

public class MyFrame {
    private JFrame frame;
    private MyPanel panel;

    public MyFrame(){
        panel = new MyPanel();
    }

    /*
    *HERE GOES THE `FILE OPEN` LISTENER
    * it creates `Coordinate` for each line 
    * passes the coordinate to panel.makeIcons()
    */

    public void createGui(){
      frame = new JFrame("Graph Editor");
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setResizable(true);

      //create, get and set the Jframe menu bar
      //createMenuBar() returns a JMenuBar
      frame.setJMenuBar(createMenuBar());

      Container frame_pane = frame.getContentPane();

      panel.setBounds(0, 0, frame.getWidth(), frame.getHeight());
      frame_pane.add(panel);

      frame.pack();
      frame.setVisible(true);
    }

    public static void main(String args[]){  
        MyFrame window = new MyFrame();
        javax.swing.SwingUtilities.invokeLater(new Runnable() {     
            public void run() {
                window.createGui();
            }
        });
    }
} 

需要在上面的面板上显示的Icon代码:

public class MyPanel extends JPanel{
    private Set<Icon> points;

    public MyPanel(){
      setLayout(null);
      setPreferredSize(new Dimension(600, 600));
      setBackground(Color.YELLOW);
    }

    //gets called by `FILE OPEN` listener for each coordinate

    public void makeIcons(Coordinate obj){
        Icon temp = new Icon(obj);
        points.add(temp);

        //add the temp JComponent to this JPanel
        this.add(temp);
    }
}

第一个问题:图标没有显示在面板中,上面的代码。

第二个问题:当我将public Icon extends JComponent{ private Coordinate location; public Icon(Coordinate obj){ location = obj; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.fillRect(location.getX(), location.getY(), 20, 20); g.setColor(Color.BLACK); g.drawRect(location.getX(), location.getY(), 20, 20); } } 中的makeIcon方法更改为以下时。当MyPanel.class出现在任何图标上时,它会显示Icons MenuBar删除它们:

MenuBar

感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:2)

不要自己调用paintComponent(或任何paint)方法。这不是绘画的工作原理。

组件不会被绘制的核心原因是:

  1. 它的大小是0x0
  2. 它是隐形的
  3. 它没有添加到(间接)添加到本地对等方的容器中。
  4. 根据我的简短观察,我会说你遇到第1点,部分原因是因为使用了空布局

    另外,请记住,在绘制组件时,组件的Graphics上下文已经被翻译,因此0x0是组件的左上角。这意味着,根据您的代码,您最有可能以任何方式超出组件的可见边界。

    所以,你基本上有两个选择。实现您自己的布局管理器,它使用Coordinate信息将Icon放入容器中(然后需要覆盖getPreferredSize以提供大小提示)或者绘制你自己

    这可能看起来像这样......

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.util.HashSet;
    import java.util.Set;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.WindowConstants;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame("Graph Editor");
                    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                    frame.setResizable(true);
                    frame.add(new MyPanel());
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    
        public class Coordinate {
    
            private int x;
            private int y;
    
            public Coordinate(int x, int y) {
                this.x = x;
                this.y = y;
            }
    
            public int getX() {
                return x;
            }
    
            public int getY() {
                return y;
            }
    
        }
    
        public class Icon {
    
            private Coordinate coordinate;
    
            public Icon(Coordinate coordinate) {
                this.coordinate = coordinate;
            }
    
            public Coordinate getCoordinate() {
                return coordinate;
            }
    
            public void paint(Graphics2D g2d) {
                g2d.setColor(Color.RED);
                g2d.fillRect(0, 0, 20, 20);
                g2d.setColor(Color.BLACK);
                g2d.drawRect(0, 0, 20, 20);
            }
        }
    
        public class MyPanel extends JPanel {
    
            private Set<Icon> points;
    
            public MyPanel() {
                points = new HashSet<>();
                setLayout(null);
                setPreferredSize(new Dimension(600, 600));
                setBackground(Color.YELLOW);
            }
    
            //gets called by `FILE OPEN` listener for each coordinate
            public void makeIcons(Coordinate obj) {
                points.add(new Icon(obj));
                repaint();
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
                for (Icon icon : points) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    Coordinate coordinate = icon.getCoordinate();
                    // I'd have a size component associated with the Icon
                    // which I'd then use to offset the context by half its
                    // value, so the icon is paint around the center of the point
                    g2d.translate(coordinate.getX() - 10, coordinate.getY() - 10);
                    icon.paint(g2d);
                    g2d.dispose();
                }
            }
    
        }
    }
    

    我没有播种任何值,我很懒,但那是基本的想法

相关问题