修改
我想创建一个允许我随机创建panels
的程序,我有一个MVC
实现。
这里的情景:
当我点击按钮 Next 时,我想更新面板的数量(它是 panelContainer
中的面板列表)但GUI赢了&# 39;吨。当我使用调试器时,它看起来是正确的。我清空了panelsContainerand和valuesList,并用新的填充它,但是在GUI中,旧的面板保留,新的只是添加。所以我发现自己有20到N个面板。
我认为问题出在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());
}
}
答案 0 :(得分:0)
有许多不同的可能性(并使用不同的框架),让我们覆盖其中两个:
node.setVisible(false);
)。如果用户点击Next
,您可以隐藏旧节点并显示新节点。 root.setCenter(new FirstPage());
如果用户点击下一步按钮:
root.setCenter(new SecondPage()):
root
是BorderPane
(以及您的根视图),FirstPage
和SecondPage
是扩展Node
或Pane
的类包含您的UI元素。