gpt4 book ai didi

java - 第一次点击按钮后,按钮停止工作

转载 作者:行者123 更新时间:2023-11-30 04:36:03 24 4
gpt4 key购买 nike

我正在开发一个程序,如果单击菜单项“显示网格线”,菜单栏下方的绘图区域将在网格上绘制网格线。

编辑:以下是该程序的组件。我把对这个问题不重要的任何部分都变成了评论。

import java.awt.Color;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

//main initializer
public class lineFollower {
frameDesign frame;
//private static Image icon = new ImageIcon("images/icon copy.jpg").getImage();

public static void main(String[] args) {
//make new frame
JFrame frame = new frameDesign();
frame.setSize(1000,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocale(null);
frame.setTitle("Line Follower Program 1.0");
//frame.setIconImage(icon);

}

}

这是框架设计(也省略了部分):

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.*;

public class frameDesign extends JFrame {
//data fields
//JPanel DataPanel;
//ListPanel ListPanel;
DrawingPanel DrawingPanel;
grid gridbox;
menuPanel menu;
//ArrayList<Rectangle> Rectangles;
//ArrayList<Circle> Circles;
//ArrayList<Triangle> Triangles;
public int newvalue = 0;
public int[] griddim;
public int gridwidth, gridheight;
BorderLayout layout;

//combine data into frame
public frameDesign(){
//Rectangles = new ArrayList<Rectangle>();
//Circles = new ArrayList<Circle>();
//Triangles = new ArrayList<Triangle>();
//Layout: Border
BorderLayout layout = new BorderLayout();
setLayout(layout);

//Assemble the Menu
menu = new menuPanel();

//Assemble the List Panel which also includes the animation buttons
//ListPanel = new ListPanel();
//Create the gridbox
gridbox = new grid();
gridbox.setPreferredSize(new Dimension(200,200));
gridbox.setSize(500,200);
griddim = gridbox.getgriddim();
gridwidth = griddim[0];
gridheight = griddim[1];


//Data Panel consists of ListPanel, AnimatePanel, and DrawingPanel
//DataPanel = new JPanel(new FlowLayout());
//DataPanel.add(DrawingPanel= new DrawingPanel());
//DataPanel.add(ListPanel = new ListPanel());
//DataPanel.setSize(250,700);
//DataPanel.setBackground(Color.getHSBColor(150f, 100f, 100f));
//DataPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));

add(gridbox, BorderLayout.CENTER);
add(menu, BorderLayout.NORTH);
//add(DataPanel, BorderLayout.EAST);

//getPoints pointfinder = new getPoints();
//pointgrabber pointfinder2 = new pointgrabber();
//gridbox.addMouseListener(pointfinder2);
//gridbox.addMouseMotionListener(pointfinder);

//ActionListeners for Menus
menu.ShowLine.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//show the grid
if (gridbox.isgridline()==true){
gridbox.gridline(false);
}

else if (gridbox.isgridline()==false){
gridbox.gridline(true);
}
}
});
}

下一堆代码用于网格创建:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*
* This is the design for the grid
*/
public class grid extends JPanel {
//data fields
boolean isgridline;
private static int startX = 50;
private static int startY = 50;
private int intervalline;
private int[] griddim;

//gridPanel
grid(){
setBorder(BorderFactory.createLineBorder(Color.BLACK));
griddim = new int[2];
griddim[0] = 500;
griddim[1] = 500;
isgridline = false;
intervalline = 20;
}

//Paints grid
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);

//GridBox
g.setColor(Color.BLACK);
g.drawRect(50, 50, griddim[0], griddim[1]);
g.setColor(Color.WHITE);
g.fillRect(50, 50, griddim[0], griddim[1]);

//draw grid lines
if (isgridline){
g.setColor(Color.getHSBColor(150, 150, 200));
//vertical lines
for (int v = 0; v <griddim[0]; v = v+intervalline){
g.drawLine(startX, 50,
startX, 50+griddim[1]);
startX = startX+intervalline;
}
//horizontal lines
for (int v=0; v<griddim[1]; v=v+intervalline){
g.drawLine(50, startY, 50+griddim[0], startY);
startY = startY+intervalline;
}
}

}

//set grid dimensions
public void setgridbox(int[] dim){
griddim=dim;
}

//get grid dimensions
public int[] getgriddim(){
return griddim;
}
//set the grid line true or false
public void gridline(boolean islines){
isgridline=islines;
repaint();
}
//get gridline true or false
public boolean isgridline(){
return isgridline;
}

//set grid line intervals
//public void setinterval(int interval){
//intervalline=interval;
//repaint();
//}
}

最后是菜单:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.event.MenuEvent;

/*
* Class just for the menu panel
*/

public class menuPanel extends JMenuBar {

JMenuItem New, Save, Open, Exit;
JMenu fileMenu, gridMenu;
JMenuItem GridDim, GridLine, ShowLine;

menuPanel(){
//Assemble file menu
fileMenu = new JMenu("File");
New = new JMenuItem("New");
Save = new JMenuItem("Save");
Open = new JMenuItem("Open");
Exit = new JMenuItem("Exit");
fileMenu.add(New);
fileMenu.add(Save);
fileMenu.add(Open);
fileMenu.addSeparator();
fileMenu.add(Exit);
add(fileMenu);

//Assemble grid menu
gridMenu = new JMenu("Grid");
GridDim = new JMenuItem("Dimension");
GridLine = new JMenuItem("Change Grid Line Interval");
ShowLine = new JMenuItem("Show Grid Lines");
gridMenu.add(GridDim);
gridMenu.add(GridLine);
gridMenu.add(ShowLine);
add(gridMenu);

}

}

当点击grid下菜单栏的“显示网格线”按钮时,会打开和关闭网格线各一次。之后它就停止工作了...我该怎么做才能使网格线再次出现不止一次?

最佳答案

我做了一个非常快速的测试,但由于您没有向我们提供可运行的示例,因此无法重现您的问题。

我“怀疑”的是你的for-loop由于某种原因崩溃(griddim为空或存在越界异常),这导致EDT变得不稳定。

除非您使用可重复、可运行的示例来启发使用,否则我们永远无法确定

public class TestGridLine {

public static void main(String[] args) {
new TestGridLine();
}

public TestGridLine() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new GridPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class GridPane extends JPanel {

private boolean gridline;

public GridPane() {
setLayout(new GridBagLayout());
JButton btn = new JButton("Grid");
btn.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
setGridLine(!isGridLine());
}
});
add(btn);
}

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

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int width = getWidth() - 1;
int height = getHeight() - 1;

//GridBox
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
g.setColor(Color.WHITE);
g.fillRect(1, 1, width - 1, height - 1);

//draw grid lines
if (gridline) {
g.setColor(Color.getHSBColor(150, 150, 200));
//vertical lines
for (int v = 0; v < width - 2; v += 10) {
g.drawLine(v, 1, v, height - 2);
}
//horizontal lines
for (int v = 0; v < height - 2; v += 10) {
g.drawLine(1, v, width - 2, v);
}
}


}

//returns boolean gridline
public boolean isGridLine() {
return gridline;
}

//set the grid line true or false
public void setGridLine(boolean islines) {
if (islines != gridline) {
gridline = islines;
repaint();
}
}
}
}

关于java - 第一次点击按钮后,按钮停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13505726/

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