简体   繁体   中英

After the button is clicked for the first time, the button stops working

I am developing a program where if the menu item "show grid line" is clicked, the drawing area below the menu bar will draw grid lines on the grid.

EDIT: Below are the components to the program. I turned any parts that are not important for this issue into comments.

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);

    }

}

This is the frame design (omitting parts as well):

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);
    }
}   
});
}

The next pile of code is for the grid creation:

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();
//}
}

Lastly, the menu:

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);

    }

}

When the "show grid lines" button in the menu bar under grid is clicked, it turns on the grid lines and turns off the grid lines once each. It stops working after that... What can I do such that the grid lines show up on and off again more than just once?

I did a really quick test, but because you've not provided us with runnable example, it's not possible to reproduce your problem.

What I "suspect" is your for-loop is crashing for some reason (the griddim is null or there's an out bounds exception) which is causing the EDT to become unstable.

Until you enlighten use with a repeatable, runnable example, we're never going to be sure

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();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM