Hereunder是我无法在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);
}
}
答案 0 :(得分:1)
永远不要直接在JFrame中绘制,而是在JPanel的paintComponent方法中绘制,然后在JFrame中显示该JPanel。在标准教程中对此进行了很好的解释::Lesson: Performing Custom Painting
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
@SuppressWarnings("serial")
public class EssaiDrawLine2 extends JPanel {
private static final int PS_WIDTH = 600;
private static final int PS_HEIGHT = 500;
public EssaiDrawLine2() {
setPreferredSize(new Dimension(PS_WIDTH, PS_HEIGHT));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(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);
}
private static void createAndShowGui() {
EssaiDrawLine2 mainPanel = new EssaiDrawLine2();
JFrame frame = new JFrame("Essai Draw Line");
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());
}
}
答案 1 :(得分:1)
另一种选择:不画线,而是创建一个组件网格,例如使用另一个GridLayout的另一个JPanel中保存的二维JPanels数组,然后为这些JPanels提供一个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());
}
}