为什么 Grid mze 不更新?按钮应该编辑行和列,然后将更新 Grid mze 的单元格,但事实并非如此。如果我单击与 H 对齐的 + 按钮,mze 应更新为 26x25 网格,但没有任何反应,控制台上仅显示 Columns + 1。
import javax.swing.*;
import java.awt.*;
import javax.swing.GroupLayout.*;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class csc extends JFrame{
public csc(String s){
super(s);
setSize(800,600);
//code generated from eclipse
JPanel panel = new JPanel();
panel.setBackground(SystemColor.control);
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 774, Short.MAX_VALUE)
.addContainerGap())
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
.addContainerGap())
);
JPanel scCont = new JPanel();
scCont.setBorder(new MatteBorder(0, 0, 1, 1, (Color) new Color(0, 0, 0)));
JPanel cplCont = new JPanel();
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(27)
.addComponent(scCont, GroupLayout.PREFERRED_SIZE, 475, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(cplCont, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE)
.addContainerGap(19, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(131)
.addComponent(cplCont, GroupLayout.PREFERRED_SIZE, 249, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel.createSequentialGroup()
.addGap(35)
.addComponent(scCont, GroupLayout.PREFERRED_SIZE, 480, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(35, Short.MAX_VALUE))
);
Grid mze = new Grid(4,4,400,400);
cscPanel cpl = new cscPanel(this,mze);
scCont.setLayout(new BorderLayout(0, 0));
mze = new Grid(cpl.rows,cpl.cols,400,400);
mze.setForeground(SystemColor.desktop);
scCont.add(mze);
GroupLayout gl_cplCont = new GroupLayout(cplCont);
gl_cplCont.setHorizontalGroup(
gl_cplCont.createParallelGroup(Alignment.LEADING)
.addGroup(gl_cplCont.createSequentialGroup()
.addContainerGap()
.addComponent(cpl, GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
.addContainerGap())
);
gl_cplCont.setVerticalGroup(
gl_cplCont.createParallelGroup(Alignment.LEADING)
.addGroup(gl_cplCont.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cpl, GroupLayout.PREFERRED_SIZE, 419, GroupLayout.PREFERRED_SIZE))
);
cplCont.setLayout(gl_cplCont);
panel.setLayout(gl_panel);
getContentPane().setLayout(groupLayout);
//end of eclipse generated code
pack();
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){
csc MakeMaze = new csc("Catch Torchic!");
MakeMaze.setVisible(true);
}
}
class cscPanel extends JPanel{
static final int MIN_CELLS = 3;
static final int MAX_W_CELLS = 50;
static final int MAX_H_CELLS = 50;
static final int MAX_DELAY = 500;
static final int MIN_DELAY = 5;
int rows = 25; int cols = 25; private int genDelay = 50; private int solveDelay = 50;
JButton bGen;
JButton bmm;
JButton bSolve;
JButton bLoad;
JButton bRowsPlus;
JButton bRowsMinus;
JButton bColsPlus;
JButton bColsMinus;
private TextField tfRows;
private TextField tfCols;
private JPanel rowsPanel;
private JPanel colsPanel;
private JPanel rowsBtnPanel;
private JPanel colsBtnPanel;
private Grid mze;
private csc Fr;
private JPanel p = new JPanel();
private cscButtonHandler handler;
cscPanel(csc Fr, Grid mze) {
this.Fr = Fr;
this.mze = mze;
setLayout(new GridLayout(11, 1, 0, 3));
this.rowsBtnPanel = new JPanel();
this.colsBtnPanel = new JPanel();
this.rowsBtnPanel.setLayout(new GridLayout(1, 2));
this.colsBtnPanel.setLayout(new GridLayout(1, 2));
this.rowsBtnPanel.add(this.bRowsMinus = new JButton("-"));
this.rowsBtnPanel.add(this.bRowsPlus = new JButton("+"));
this.colsBtnPanel.add(this.bColsMinus = new JButton("-"));
this.colsBtnPanel.add(this.bColsPlus = new JButton("+"));
this.rowsPanel = new JPanel();
this.rowsPanel.setLayout(new BorderLayout(2, 0));
this.rowsPanel.add("West", new Label("W"));
this.rowsPanel.add("Center", this.tfRows = new TextField(3));
this.rowsPanel.add("East", this.rowsBtnPanel);
this.colsPanel = new JPanel();
this.colsPanel.setLayout(new BorderLayout(2, 0));
this.colsPanel.add("West", new Label("H"));
this.colsPanel.add("Center", this.tfCols = new TextField(3));
this.colsPanel.add("East", this.colsBtnPanel);
this.tfRows.setEditable(false);
this.tfCols.setEditable(false);
add(this.bLoad = new JButton("LOAD"));
add(this.rowsPanel);
add(this.colsPanel);
add(this.bGen = new JButton("RANDOM"));
add(this.bSolve = new JButton("SOLVE"));
add(this.bmm = new JButton("MAIN MENU"));
this.tfRows.setText(Integer.toString(this.rows));
this.tfCols.setText(Integer.toString(this.cols));
this.bSolve.setEnabled(false);
handler = new cscButtonHandler(Fr,this,mze);
bGen.addActionListener(handler);
bmm.addActionListener(handler);
bSolve.addActionListener(handler);
bLoad.addActionListener(handler);
bRowsPlus.addActionListener(handler);
bRowsMinus.addActionListener(handler);
bColsPlus.addActionListener(handler);
bColsMinus.addActionListener(handler);
}
void setPlusMinusEnable() {
this.bRowsPlus.setEnabled(this.rows < 50);
this.bRowsMinus.setEnabled(this.rows > 3);
this.bColsPlus.setEnabled(this.cols < 50);
this.bColsMinus.setEnabled(this.cols > 3);
}
void setGenEnable(boolean paramBoolean){
this.bGen.setEnabled(paramBoolean); }
void setSolveEnable(boolean paramBoolean) { this.bSolve.setEnabled(paramBoolean); }
}
class cscButtonHandler implements ActionListener{
csc a;
cscPanel b;
Grid c;
public cscButtonHandler(csc a,cscPanel b, Grid c){
this.a = a;
this.b = b;
this.c = c;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == b.bmm)
{System.out.println("Back to MainMenu");}
else if(e.getSource() == b.bSolve)
{System.out.println("Solve Maze");}
else if(e.getSource() == b.bLoad)
{System.out.println("Open Load Dialog");}
else if(e.getSource() == b.bRowsPlus)
{System.out.println("Rows + 1");
b.rows++;
c.repaint(b.rows,b.cols,400,400);}
else if(e.getSource() == b.bRowsMinus)
{System.out.println("Rows - 1");
b.rows--;
c.repaint(b.rows,b.cols,400,400);}
else if(e.getSource() == b.bColsPlus)
{System.out.println("Columns + 1");
b.cols++;
c.repaint(b.rows,b.cols,400,400);}
else if(e.getSource() == b.bColsMinus)
{System.out.println("Columns - 1");
b.cols--;
c.repaint(b.rows,b.cols,400,400);}
else if(e.getSource() == b.bGen)
{System.out.println("Generating Randomly");}
}
}
class Grid extends Canvas{
Cell[][] maze;
int rows;
int cols;
public Grid(int rows, int cols, int h, int w) {
this.rows = rows;
this.cols = cols;
maze = new Cell[rows][cols];
setPreferredSize(new Dimension(h,w));
}
public void paint(Graphics g) {
int k;
double width = getSize().width;
double height = getSize().height;
double htOfRow = height / (rows);
for (k = 0; k < rows; k++)
g.drawLine(0, (int) (k * htOfRow) , (int) width, (int) (k * htOfRow) );
double wdOfRow = width / (cols);
for (k = 0; k < cols; k++)
g.drawLine((int) (k*wdOfRow) , 0,(int) (k*wdOfRow) , (int) height);
}
}
class Cell {}