gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:04:35 25 4
gpt4 key购买 nike

下面是我无法在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com