簡體   English   中英

第一次點擊按鈕后,按鈕停止工作

[英]After the button is clicked for the first time, the button stops working

我正在開發一個程序,如果單擊菜單項“顯示網格線”,菜單欄下方的繪圖區域將在網格上繪制網格線。

編輯:以下是程序的組件。 我把對這個問題不重要的任何部分都變成了評論。

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

    }

}

單擊網格下菜單欄中的“顯示網格線”按鈕時,將打開網格線並關閉網格線一次。 在那之后它停止工作......我該怎么做才能使網格線不止一次地再次出現和關閉?

我做了一個非常快速的測試,但是因為您沒有為我們提供可運行的示例,所以不可能重現您的問題。

我“懷疑”的是您的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();
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM