Java 程序不绘制垂直网格

标签 java swing jbutton paintcomponent

所以我有以下代码(对不起,如果它困惑),当我按下按钮“显示垂直网格”时,它不想打开垂直网格。有人可以解决这个问题吗?我完全没有想法。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.MouseInfo;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

public class Game extends JPanel implements ActionListener {

private GridPane gridPane;

public static square s =  new square();

public boolean isMouseClicked = false;

public static JMenuBar bar = new JMenuBar();




public int gridY = 1;
public int gridX = 1;


public Game() {
    setLayout(new BorderLayout());

   OptionPanel options = new OptionPanel();
   options.addActionListener(this);
   add(options, BorderLayout.NORTH);


    gridPane = new GridPane();
    add(gridPane);


}

public static void main(String args[]) {
    Game game = new Game();

    JFrame frame = new JFrame();
    frame.setVisible(true);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setTitle("Game");
    frame.setAlwaysOnTop(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    frame.setLayout(new BorderLayout());
    frame.add(game);


}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equalsIgnoreCase("grid")) {
        gridPane.setGridOn(!gridPane.isGridOn());
    }

    if (e.getActionCommand().equalsIgnoreCase("square")){
        gridPane.setSqaureOn(!gridPane.isSquareOn());
    }
}

public class GridPane extends JPanel {

    private boolean gridOn = false;
    private boolean squareOn = false;
    private boolean vertOn;

    public GridPane() {
        setBackground(Color.BLACK);
    }

    public boolean isGridOn() {
        return gridOn;
    }

    public boolean isSquareOn(){

        return squareOn;
    }

    public boolean isVertOn(){

        return vertOn;
    }

    public void setGridOn(boolean value) {
        if (value != gridOn) {
            this.gridOn = value;
            repaint();
        }
    }

    public void setVertOn(boolean value){

        if (value != vertOn){
            this.vertOn = value;
            repaint();
        }
    }

    public void setSqaureOn(boolean value){
        if (value != squareOn){
            this.squareOn = value;
            isMouseClicked = true;
            repaint();
        }
    }

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

        Toolkit tk = Toolkit.getDefaultToolkit();

        if (gridOn) {
            g.setColor(Color.white);
            for (int i = 0; i < tk.getScreenSize().height; i += 64){
                gridY++;
                g.drawLine(0, (64 * gridY), tk.getScreenSize().width,(64 * gridY));
            }
        }

        gridY = -1;

       gridX = -1;

       if(isSquareOn() && isMouseClicked == true){
           s.drawSquare(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y, 64, 64,g,new Color(255,255,255));
       }

       if (vertOn){
           g.setColor(Color.white);
           for (int ig = 0; ig < tk.getScreenSize().width; ig += 64){
              gridX++;
              g.drawLine((64 * gridX), 0,(64 * gridX),tk.getScreenSize().height);
          }
       }

 }


}

public class OptionPanel extends JPanel {

    public JButton grid;

    public JButton vgrid;

    public JButton square;

    public OptionPanel() {

        //Sets the stuff for the panel
        setBackground(new Color(155,0,255));
        setLayout(new GridBagLayout());
        //end

        //The Show Grid Button Stuff
        grid = new JButton("Show Horizontal Grid");
        grid.setActionCommand("grid");
        //end

        //The vertical grid
        vgrid = new JButton("Show Vertical Grid");
        vgrid.setActionCommand("vgrid");
        //end

        //The Square tool button stuff
        square = new JButton("Sqaure Tool");
        square.setActionCommand("square");
        //end

        //The gridbagConstraints things
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;


        //kind of like padding
        gbc.weighty = 1;

        //sets the positions
        gbc.gridx = 0;
        gbc.gridy = 0;

        //add it
        add(grid, gbc);

        //changes position for the second button
        gbc.gridx = -1;
        gbc.gridy = 0;

        // adds it
        add(vgrid,gbc);

        //end


    }

    public void addActionListener(ActionListener listener) {
        //adds action listeners
        grid.addActionListener(listener);
        vgrid.addActionListener(listener);
    }
}
}

最佳答案

协调这一行:

vgrid.setActionCommand("vgrid");

这些行:

public void actionPerformed(ActionEvent e) {
  if (e.getActionCommand().equalsIgnoreCase("grid")) {
     gridPane.setGridOn(!gridPane.isGridOn());
  }

  if (e.getActionCommand().equalsIgnoreCase("square")) {
     gridPane.setSqaureOn(!gridPane.isSquareOn());
  }
}

它在哪里检查给垂直网格按钮的 actionCommand,然后调用正确的方法来设置垂直网格状态?

更重要的是——学习和使用一个有用的调试工具是在您的代码中散布 println 语句,以查看您认为应该被调用的方法是否实际上正在叫。

关于Java 程序不绘制垂直网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16493715/

相关文章:

java - 反向代理背后的 Csrfguard

java - UTF-8 的 getBytes() 不适用于大写德语变音符号

java - 向导不重绘/重新验证 jframe

java - 2D JButton 数组,想要为游戏添加 Action 监听器

java - 单击 JButton 后关闭 jFrame

java - MainActivity中的java.lang.NullPointerException setOnClickListener showdialog

java - 为什么人们想要学习和/或练习使用带有 Java SE 的服务器,而不是使用 EE 方法?

java - GridBagLayout 组件显示不正确的宽度

java - 使用javax.swing输入信息并将其存储在Array List中

java - 如何用触摸屏使Java JButton在视觉上有压抑感?