所以我有以下代码(对不起,如果它很乱),当我按下“显示垂直网格”按钮时,它不想打开垂直网格。有人可以解决这个问题吗?我完全没主意了。
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);
}
}
}