【问题标题】:Java Swing how to draw polygons with coordinate params when layout is not absoluteJava Swing如何在布局不是绝对时使用坐标参数绘制多边形
【发布时间】:2019-05-03 19:34:45
【问题描述】:

我正在尝试使用 swing 制作一个小 GUI。我希望它在具有 GridBagLayout 的 JPanel 上绘制一个多边形。我该怎么做呢?我可以给出要绘制的多边形坐标吗?坐标是基于绘制多边形的单元格、JPanel 还是 Frame?

最初为了测试,我只是尝试在我拥有的类中用坐标画一条线,然后将其添加到 JPanel。什么都没有画出来。

我在 UniverseDisplay 类中重写的 paintComponent 方法是:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.black);
    g.drawLine(20,  20,  30,  30);
    System.out.println("PAINTED");
}

我拥有的设置 UniverseDisplay 并导致调用 paintComponent 的函数如下:

public void setUpGenerateButton(){
    generateButton = new JButton("Generate Model");
    generateButton.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    Universe u = new Universe(reflexive, transitive, symmetric, hereditary);
                    World w = new World(u);
                    u.addWorld(w);
                    Tree t = new Tree();
                    t.setRoot(t);
                    t.setData(expressions, 0, expressions.size());
                    t.setInitialWorlds(w);
                    t.setInitialUniverses(u);
                    t.evaluateModalExpressionV2(u);
                    /*
                     * this bit below isnt actually painting anything on the 
                     * JPanel even though paintComponent() is being called properly
                     */
                    UniverseDisplay ud = new UniverseDisplay(u);
                    rightPanel.add(ud);
                    rightPanel.revalidate();
                    rightPanel.repaint();
                }
            });
}

JPanel 已设置

public void setUpPanels() {
    setUpTextInputField();
    setUpGenerateButton();
    setUpPropsPanel();
    setUpButtons();
    setUpClearButton();


    leftPanel = new JPanel();
    rightPanel = new JPanel();
    leftPanel.setLayout(new GridBagLayout());
    rightPanel.setLayout(new GridBagLayout());

    leftPanel.setBackground(Color.gray);
    rightPanel.setBackground(Color.white);

    /*
     * create a new constraints for each new component, to avoid bugs
     */
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 0;
    leftPanel.add(inputL, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 1;
    leftPanel.add(inputField, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.insets = new Insets(0, 15, 0, 0);
    leftPanel.add(generateButton, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 2;
    leftPanel.add(propsL, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 3;
    constraints.insets = new Insets(10, 0, 0, 0);
    constraints.fill = GridBagConstraints.BOTH;
    constraints.anchor = GridBagConstraints.WEST;
    leftPanel.add(propsPanel, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.ipadx = 30;
    constraints.ipady = 70;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = new Insets(10, 15, 0, 0);
    leftPanel.add(buttonPanel, constraints);

    constraints = new GridBagConstraints();
    constraints.gridx = 0;
    constraints.gridy = 4;
    constraints.insets = new Insets(10, 0, 0, 0);
    constraints.anchor = GridBagConstraints.WEST;
    leftPanel.add(clearButton, constraints);

    add(leftPanel);
    add(rightPanel);
}

为什么在 JPanel 中没有画线?如何在没有将其布局设置为 null 的 JPanel 中绘制形状?

我希望这些信息足以提供有用的答案:)

【问题讨论】:

标签: java swing layout awt


【解决方案1】:

当您进行自定义绘画时,您有责任确定组件的首选尺寸。这是通过覆盖组件的getPreferredSize() 来完成的。

所以在你的情况下你需要使用:

@Overrid
public Dimension getPreferredSize()
{
    return new Dimension(50, 50);
}

然后布局管理器可以使用首选大小信息来定位组件在您的布局中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    相关资源
    最近更新 更多