当我切换时,有人可以解释为什么没有加载。每当我单击菜单UI按钮切换网格时,控制台就会打印出正确的布尔值,但网格不会绘制。
我尝试了重绘方法,并尝试再次调用加载网格的方法。
private boolean toggleGrid = false;
public Editor() {
width = DEFAULT_WIDTH;
height = DEFAULT_HEIGHT;
setTitle("Map Editor");
setSize(width, height);
mouse = new Mouse();
addMouseListener(mouse);
addMouseMotionListener(mouse);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
loadMenuUI();
loadTileContainer();
loadEditContainer();
}
private void loadMenuUI(){
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmNewProject = new JMenuItem("New Project");
mntmNewProject.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK));
mnFile.add(mntmNewProject);
JMenuItem mntmSave = new JMenuItem("Save...");
mntmSave.setAccelerator(KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
mnFile.add(mntmSave);
JMenuItem mntmLoad = new JMenuItem("Load...");
mntmLoad.setAccelerator(KeyStroke.getKeyStroke('O', Event.CTRL_MASK));
mnFile.add(mntmLoad);
mnFile.addSeparator();
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.setAccelerator(KeyStroke.getKeyStroke('N', InputEvent.CTRL_MASK));
mnFile.add(mntmExit);
JMenu mnOptions = new JMenu("Options");
menuBar.add(mnOptions);
JMenuItem mntmChangeGridSize = new JMenuItem("Change Grid Size");
mnOptions.add(mntmChangeGridSize);
JMenuItem mntmChangeScreenSize = new JMenuItem("Change Screen Size");
mnOptions.add(mntmChangeScreenSize);
JMenuItem mntmToggleGrid = new JMenuItem("Toggle Grid");
mntmToggleGrid.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(toggleGrid) toggleGrid = false;
else {
toggleGrid = true;
repaint();
}
}
});
mnOptions.add(mntmToggleGrid);
}
private void loadTileContainer(){
tileContainer = new JPanel(new GridLayout(20,20));
tileContainer.setBackground(Color.LIGHT_GRAY);
tileContainer.setBounds(0, 0, width - 15, height - 200);
tileContainer.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
if(toggleGrid){
System.out.println("Working");
for(int i = 0; i < 400; i++){
JLabel label = new JLabel();
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
tileContainer.add(label);
}
}
contentPane.add(tileContainer);
}
答案 0 :(得分:2)
一旦调用了这个方法,它就结束了,完成了:
private void loadTileContainer(){
tileContainer = new JPanel(new GridLayout(20,20));
tileContainer.setBackground(Color.LIGHT_GRAY);
tileContainer.setBounds(0, 0, width - 15, height - 200);
tileContainer.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
if(toggleGrid){
System.out.println("Working");
for(int i = 0; i < 400; i++){
JLabel label = new JLabel();
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
tileContainer.add(label);
}
}
contentPane.add(tileContainer);
}
然后你的菜单代码:
mntmToggleGrid.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(toggleGrid) toggleGrid = false;
else {
toggleGrid = true;
repaint();
}
}
});
只需在ActionListener中将toggleGrid设置为true,就不会再次神奇地调用loadTileContainer()
方法,因此您的ActionListener无效也就不足为奇了。相反,您需要从if块中提取代码并将其放在自己的方法中,可以在需要时从动作侦听器中调用。
然后在更改容器的内容后,您需要在容器上调用revalidate()
然后repaint()
。
然而,更好的是使用CardLayout交换组件。 CardLayout tutorial会告诉你如何。