【问题标题】:Can't draw lines on existing panel无法在现有面板上画线
【发布时间】:2012-11-24 10:59:13
【问题描述】:

我正在用 Swing 编写我的第一个 Java GUI。 我正在尝试创建一个带有 10X10 点的面板并在这些点上画线。

问题是我只能创建上述之一。 这意味着我可以创建一个 10X10 点的矩阵或创建线条。

这是我的代码:

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setTitle("DrawMatrix");
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new DrawRectPanel());
        frame.setSize(240, 260);
        frame.setVisible(false);
        frame.getContentPane().add(new DrawLine());
        frame.setVisible(true);
   }
}

public class DrawRectPanel extends JPanel{
   final static int DIST = 20;
   final static int MAX_ROW = 11;
   final static int MAX_COL = 11;
   int i = 1;

@Override

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    {
        // Points matrix
        int x = 0;
        for(int row = 1; row < MAX_ROW; row++)
        {
             x = row * DIST;
            for(int col = 1; col < MAX_COL; col++)
            {
                int y = col * DIST;
                g.setColor(Color.blue);
                g.drawLine(x,y,x,y+1);
            }
        }
    }
  }
}

public class DrawLine extends JPanel {

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(20,20,40,40);
    }
 }

知道为什么吗?我应该以不同的方式使用 setVisible 吗?

【问题讨论】:

标签: java swing jpanel layout-manager paintcomponent


【解决方案1】:

您在连续调用add(..) 时覆盖添加到contentPane 的每个组件。

我希望它们都在中心,因为我想从点到点画线

您应该使用包含单个DrawingPanel 的方法,对象可以被吸引到该方法。您可能还需要一个数组来保存要绘制的形状,以便可以轻松获取和操作paintComponent(..)

  • 请记住在 Event Dispatch Thread via SwingUtilities.inovkeLater(..) 上创建和操作所有 Swing 组件

  • 不要在JFrame 上调用setSize(),而是在设置JFrame 可见之前调用pack(),而不是覆盖getPrefferedSizeJPanelsetPrefferedSize 并返回适合其中所有图形的尺寸.

  • 不需要getContentPand.add(..),只需在JFrame 上调用add(..),因为调用将被定向到contentPane

这里是一个例子(基本上你用上面的修复编码)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setTitle("DrawMatrix");
                frame.setResizable(false);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(new DrawPanel());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class DrawPanel extends JPanel {

    final static int DIST = 20;
    final static int MAX_ROW = 11;
    final static int MAX_COL = 11;
    int i = 1;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // Points matrix
        int x = 0;
        for (int row = 1; row < MAX_ROW; row++) {
            x = row * DIST;
            for (int col = 1; col < MAX_COL; col++) {
                int y = col * DIST;
                g.setColor(Color.blue);
                g.drawLine(x, y, x, y + 1);
            }
        }
        g.drawLine(20, 20, 40, 40);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多