【问题标题】:paintComponent doesn't draw on JPanelpaintComponent 不在 JPanel 上绘制
【发布时间】:2021-04-14 13:38:13
【问题描述】:

我想在 JPanel(添加到 JScrollPane)上添加几行带有标签(JLabel)的行。早些时候,当我尝试直接在框架上添加线条和标签时,它起作用了(意味着线条和标签可见),但在添加 JScrollPane 和 Jpanel 后,线条不可见,但标签可见。

知道这里有什么问题吗?

public class gui {

static JFrame frame;
JPanel panel;

ArrayList<Line2D.Double> lines;

public gui() {
    panel = new JPanel();
    lines = new ArrayList<Line2D.Double>();
    
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            
            frame = new JFrame();
            
            addLine(10,10, 200,300);
            panel.repaint();
            
            panel.add(new test());
            
            panel.setPreferredSize(new Dimension(1200, 800));
        
            panel.setLayout(new BorderLayout());
            panel.setBackground(Color.WHITE);
            // Add scrollbar
            JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
            scrollBar.setPreferredSize(panel.getPreferredSize());
            
            frame.getContentPane().add(scrollBar);
            
            frame.getContentPane().validate();

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            frame.pack();
            frame.setVisible(true);
        }
    });
}    


public void addLine(int x1, int x2, int x3, int x4) {
    lines.add(new Line2D.Double(x1, x2, x3, x4));
}

public class test extends JPanel {
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(1200, 800);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(Color.BLACK);
        for (Line2D.Double line : lines) {
            drawArrow(g2d,
                (int)line.getX1(),
                (int)line.getY1(),
                (int)line.getX2(),
                (int)line.getY2()
                );
        }
    }
    
    void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
        Graphics2D g = (Graphics2D) g1.create();
        System.out.println("here");
        double dx = x2 - x1, dy = y2 - y1;
        double angle = Math.atan2(dy, dx);
        int len = (int) Math.sqrt(dx*dx + dy*dy);
        AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
        at.concatenate(AffineTransform.getRotateInstance(angle));
        g.transform(at);

        // Draw horizontal arrow starting in (0, 0)
        g.drawLine(0, 0, len, 0);
        g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
                      new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4);
    }
}

【问题讨论】:

    标签: java swing jpanel paintcomponent graphics2d


    【解决方案1】:

    更换了就可以了

    panel = new JPanel();
    

    panel = new test();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-02
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      相关资源
      最近更新 更多