如何在单击按钮时动态更改JPanel?

时间:2015-10-16 17:32:56

标签: java swing

修改

我想创建一个允许我随机创建panels的程序,我有一个MVC实现。

这里的情景:

当我点击按钮 Next 时,我想更新面板的数量(它是 panelContainer中的面板列表)但GUI赢了&# 39;吨。当我使用调试器时,它看起来是正确的。我清空了panelsContainerand和valuesList,并用新的填充它,但是在GUI中,旧的面板保留,新的只是添加。所以我发现自己有20到N个面板。

See the image

我认为问题出在Test()(GameView)

旧的是这样的:

public void test(int number, List<Integer> panels){
    myListFigures.clear();
    nbpanels = number;
    myListTmp = panels;     

    panelsContrainer.removeAll();
    panelsContrainer.revalidate();
    setWork(nbpanels,myListTmp);
    panelsContrainer.revalidate();
}

我的代码:

GameModel :创建面板数量及其值的类。

public class GameModel {

private boolean isDragging;       
private int numberToAcheve;
private int numberOfCut;
private int numberOfPanels;
private int sum;
private List<Integer> myListRandomValue = new ArrayList<>(); 
private List<Integer> myListPanelsValue = new ArrayList<>(); 

GameModel(){  

   this.isDragging = false;
   this.numberToAcheve = 0;
   this.numberOfCut= 0;
   this.numberOfPanels = 0;
   this.sum = 0;
   this.allWork();
}

public void allWork(){     
    int maximum = 6;
    int minimum = 3;
    Random rn = new Random();
    StringBuilder sb = new StringBuilder(); 
    int randomNumber = 0; 
    int range =  0;

    // decoupage()
    range =  maximum - minimum + 1;
    numberOfCut = rn.nextInt(range) + minimum; 

    // NumberAttribution()
    for(int instance = 0; instance < numberOfCut;instance++){
        if (Math.random() > 0.5) {
            randomNumber = 1 + (int) (Math.random() * 9);
        } else {
            randomNumber = 10 + (int) (Math.random() * 90);
        }
        myListRandomValue.add(randomNumber);
    }   

    // panelConcat()
    for(int i=0; i < myListRandomValue.size(); i++){
        sb.append(myListRandomValue.get(i));
    }

    for(int j=0; j< sb.length(); j++){
        myListPanelsValue.add(Integer.parseInt(sb.substring(j,j+1)));
    }
       numberOfPanels = myListPanelsValue.size();
}    

public int numberToAcheve(){
    for(int i = 0; i < myListPanelsValue.size(); i++)
    {
        sum += myListPanelsValue.get(i);
    }
 return sum;      
}   

public int getNumberToAcheve() {
    return numberToAcheve;
}

public int getSum() {
    return sum;
}   

public int getNumberOfPanels() {
    return numberOfPanels;
}

public List<Integer> getMyListPanelsValue() {
    return myListPanelsValue;
}
}  

GameView :类包含所有组件,并添加setWork()到GUI的值的面板

public class GameView {

public List<Integer> myListTmp = new ArrayList<>(); 
public int nbpanels = 0;

public  List<Figure> myListFigures = new ArrayList<>(); 
private JPanel panelsContrainer = new JPanel(new GridLayout(1,0));
private JPanel controlsContrainer = new JPanel();

private final JFrame frame;
private JButton next   = new JButton("Next");        


public GameView(){                  

    frame = new JFrame("");
    frame.getContentPane().setLayout(new BorderLayout()); 

    controlsContrainer.add(next);        


    frame.getContentPane().add(panelsContrainer, BorderLayout.NORTH);
    frame.getContentPane().add(controlsContrainer, BorderLayout.CENTER);  
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           
    frame.setSize(800,300);
    frame.setVisible(true);   
}

public JButton getNext() {
    return next;
}
public void setWork(int numberOfPanels, List<Integer> panelsValues){
    for(int instance = 0; instance < numberOfPanels; instance++){
        myListFigures.add(new Figure(panelsValues.get(instance)));
        panelsContrainer.add(myListFigures.get(instance));    
    }
}

public void test(int number, List<Integer> panels){
    myListFigures.clear();
    nbpanels = number;
    myListTmp = panels;     

} 

}

GameController:

public class GameController {

private GameModel model;
private GameView view;


public GameController(GameModel model, GameView view) {
    this.model = model;
    this.view = view;        
      this.view.setWork(this.model.getNumberOfPanels(),this.model.getMyListPanelsValue());

}

public void contol(){        
    view.getNext().addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
            action();
       }
    });

}

private void action(){
model.allWork();                
    view.test(model.getNumberOfPanels(),model.getMyListPanelsValue());
}     

}

1 个答案:

答案 0 :(得分:0)

有许多不同的可能性(并使用不同的框架),让我们覆盖其中两个:

  1. 你把所有东西扔在舞台上。您不希望用户看到您可以设置为不可见的内容(通常为node.setVisible(false);)。如果用户点击Next,您可以隐藏旧节点并显示新节点。
  2. 您使用准备好的面板,然后将它们换掉。示例(JavaFX):
  3. root.setCenter(new FirstPage());

    如果用户点击下一步按钮:

    root.setCenter(new SecondPage()):

    rootBorderPane(以及您的根视图),FirstPageSecondPage是扩展NodePane的类包含您的UI元素。