2014-11-04 70 views
0

我正在使用菜單嘗試更改按鈕編號。 但是這不應該刷新。 我該如何解決這個問題? 我想'文件 - >修改 - >按鈕10x10 - > 20x20更改。 ' 測試和修改下面的源代碼。請...如何解決錯誤?擺動按鈕重新繪製

請給我更改來源。 TT

package com.test; 

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 

public class MineMain extends JFrame { 

    private GridLayout grid; 
    private JPanel jp; 
    private int rownum, colnum; 
    private JButton[][] btn = null; 

    public MineMain(){ 
     super("MINE"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     Menu_Init(); 
//  grid = new GridLayout(); 
//  jp = new JPanel(); 
     rownum = 10; 
     colnum = 10; 

     Init(200, 250); 
    } 

    private void setBtn(int row, int col){ 
     btn = new JButton[row][col]; 
    } 

    public void Init(int w, int h){ 
     if(jp != null) 
      jp.removeAll(); 
     else 
      jp = null; 

     btn = null; 
     jp = null; 
//  jp.removeAll(); 

     grid = new GridLayout(rownum, colnum, 0, 0); 
     jp = new JPanel(grid); 

     setBtn(rownum, colnum); 

     for(int i=0;i<btn.length;i++){ 
      for(int j=0;j<btn[i].length;j++){ 
       btn[i][j] = new JButton(); 
       jp.add(btn[i][j]); 
      } 
     } 

//  jp.revalidate(); 
//  jp.repaint(); 

     this.add(jp); 

     this.setSize(w, h); 
     this.setLocation(200, 200); 
     this.setVisible(true); 
     this.setResizable(false); 

    } 

    public void Menu_Init(){ 
     JMenuBar bar = new JMenuBar(); 
     setJMenuBar(bar); 

     JMenu filemenu = new JMenu("File(F)"); 
     filemenu.setMnemonic('F'); 

     JMenuItem startmenu = new JMenuItem("New Game(N)"); 
     startmenu.setMnemonic('N'); 
     startmenu.setActionCommand("NEW START"); 
     startmenu.addActionListener(new MenuActionListener()); 
     filemenu.add(startmenu); 

     JMenuItem minecntmenu = new JMenuItem("MINE MODIFY(M)"); 
     minecntmenu.setMnemonic('M'); 
     minecntmenu.setActionCommand("MODIFY"); 
     minecntmenu.addActionListener(new MenuActionListener()); 
     filemenu.add(minecntmenu); 

     JMenuItem close = new JMenuItem("CLOSE(C)"); 
     close.setMnemonic('C'); 
     filemenu.add(close); 
     bar.add(filemenu); //JMenuBar에 JMenu 부착 

    } 

    private class MenuActionListener implements ActionListener { 
     public void actionPerformed(ActionEvent e){ 
      if(e.getActionCommand().equals("START")){ 
       JOptionPane.showMessageDialog(null, "NEW GAME", "MINE", JOptionPane.YES_NO_OPTION); 
      } else if(e.getActionCommand().equals("MODIFY")){ 
       modify(2); 
      } 
     } 
    } 

    private void modify(int lvl){ 
     rownum = 20; 
     colnum = 20; 

     Init(400, 500); 
    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     new MineMain(); 
    } 

} 
+0

請訪問你的一些[前面的問題(http://stackoverflow.com/users/4115803/mickey-jee?tab=questions)見如果你可以接受一些以及.. – 2014-11-04 02:33:40

回答

3

我看你是在modify()再次調用Init()

Init(),我假設你正在使用

if(jp != null) 
    jp.removeAll(); 
else 
    jp = null; 

以清除JPanel的?

在繼續此操作之前,您想從JFrame中刪除現有的JPanel(即jp)(即this)。

所以,你可以更改您的代碼

if(jp != null) { 
    // JPanel already exists. so, remove JPanel jp from the JFrame 
    this.remove(jp); 
    jp.removeAll(); 
} else 
    jp = null; 
+0

非常非常感謝你... – 2014-11-04 02:24:20

+2

沒問題,複選標記將不勝感激,雖然:P – wns349 2014-11-04 02:29:41

+1

同意,請[接受答案](http://meta.stackexchange .com/a/5235/155831),因爲它有助於解決問題。 – 2014-11-04 02:31:37