java - JFrame中随机显示用drawLine绘制的线条

标签 java swing jframe graphics2d

下面是我无法在 Java 程序中修复的错误的最小示例。

该错误在于,这些线在 JFrame 中随机显示:有时我在运行时看到板,有时则看不到。

非常感谢任何提示。

import java.awt.Graphics;
import javax.swing.JFrame;

public class EssaiDrawLine extends JFrame{

public static void main(String[] args) {

    EssaiDrawLine mafenetre = new EssaiDrawLine();
    mafenetre.setVisible(true);

}

// constructeur
public EssaiDrawLine() {
    setTitle("mon titre");
    setSize(600, 500);
}

public void paint(Graphics g) {
    super.paint(g);
    for (int i = 0; i <= 8; i++) {
        g.drawLine(50 * i + 10, 40, 50*i + 10, 440);
        g.drawLine(10, 50 * i + 40, 410, 50 * i + 40);
    }
    g.drawString("Cliquer pour obtenir la prochaine solution", 20, 470);
}
}

最佳答案

另一种选择:不绘制线条,而是创建一个组件网格,例如另一个 JPanel(使用 GridLayout 的 JPanel)中保存的 JPanel 的二维数组,然后为 JPanel 提供一个 MouseListener,以便它们可以响应鼠标事件。例如,一个简单的版本可能如下所示(请阅读代码中的注释):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;    
import javax.swing.*;

@SuppressWarnings("serial") // get rid of warning
public class EssaiDrawLine3 extends JPanel {
    private static final int SIDE = 8; 
    private static final int SQR_LENGTH = 50;
    private static final int GAP = 5;
    private static final Color CLICK_COLOR = Color.RED;
    private static final Color SQR_COLOR = Color.LIGHT_GRAY;
    private static final String CLICK_TEXT = "Cliquer pour obtenir la prochaine solution";

    // our grid of JPanel
    private JPanel[][] grid = new JPanel[SIDE][SIDE];

    public EssaiDrawLine3() {
        // JPanel to hold the grid
        // give it an 8x8 GridLayout with 1 pixel gap for lines to show 
        JPanel gridHolder = new JPanel(new GridLayout(SIDE, SIDE, 1, 1));

        // background color that shows through as lines in gaps in grid
        gridHolder.setBackground(Color.BLACK);
        gridHolder.setBorder(BorderFactory.createLineBorder(Color.black));

        // mouse listener to add to each JPanel cell
        MyMouse myMouse = new MyMouse();

        // nested for loop to create our grid of JPanels
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                // create a single cell
                JPanel cellPanel = new JPanel();

                // size it 50x50 pixels
                cellPanel.setPreferredSize(new Dimension(SQR_LENGTH, SQR_LENGTH));

                // add the mouse listener to it
                cellPanel.addMouseListener(myMouse);

                // give it a default background color
                cellPanel.setBackground(SQR_COLOR);

                // place it into the 2-D array of JPanel
                grid[row][col] = cellPanel;

                // place it into the grid layout-using JPanel
                gridHolder.add(cellPanel);
            }
        }

        // display text at the bottom
        JLabel label = new JLabel(CLICK_TEXT, SwingConstants.CENTER);

        // allow gaps around the main JPanel
        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));

        // give the main JPanel a BorderLayout
        setLayout(new BorderLayout(GAP, GAP));
        add(gridHolder);  // add the grid to the CENTER position

        // add the JLabel to the bottom position
        add(label, BorderLayout.PAGE_END);
    }

    private class MyMouse extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            // get the JPanel cell that was clicked
            JComponent comp = (JComponent) e.getSource();

            // get its color and change it
            Color color = comp.getBackground();
            if (color.equals(CLICK_COLOR)) {
                comp.setBackground(SQR_COLOR);
            } else {
                comp.setBackground(CLICK_COLOR);
            }
        }
    }

    private static void createAndShowGui() {
        EssaiDrawLine3 mainPanel = new EssaiDrawLine3();

        JFrame frame = new JFrame("Essai Draw Line3");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

关于java - JFrame中随机显示用drawLine绘制的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51002276/

相关文章:

Java Swing - 如何在 Canvas 中绘制新线并将其添加到现有线中?

java - 按钮尺寸定为迷你

java - 在 WindowBuilder Eclipse 中更改 JFrame

java - 全屏边框问题

java - Netbeans 相当于 Eclipse 的 LogCat,用于查看 Java 日志

java - 获取二维数组中的特定用户输入

java - JOptionPane.showConfirmDialog 阻止后台窗口

java - JFrame 关闭操作

java - DOM4J 文档 : read an ISO-8859-1 xml

java - IntelliJ IDEA : "cannot resolve symbol" for String, 系统和其他 Java 类