【发布时间】:2023-04-02 06:10:01
【问题描述】:
如果调用了cont(),为什么屏幕不会更新为新面板(由theView.continueToGame() 调用)?如果我注释掉ask() 调用cont() 的位置,它似乎有效。有人可以解释为什么会这样吗?似乎有循环的东西把它搞砸了。
驱动程序.java
public class Driver {
public static void main(String[] args)
{
Controller con = new Controller();
con.ask();
}
}
Controller.java
public class Controller {
private View theView = new View();
private Model theModel = new Model();
public void ask()
{
theView.displayMenu();
cont();
System.out.println("ready");
theView.continueToGame();
}
private void cont()
{
Scanner stdin = new Scanner(System.in);
int input = 0;
while(!(input == 1))
{
System.out.println("Enter 1 to continue");
input = 0;
try
{
input = stdin.nextInt();
}
catch (InputMismatchException e)
{
System.out.println("error");
stdin.next();
}
}
stdin.close();
}
}
View.java
public class View extends JFrame {
/**
* Serial id
*/
private static final long serialVersionUID = 1L;
private String String1 = "1";
private String String2 = "2";
View()
{
setVisible(true);
setTitle("Tic-Tac-Toe");
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void displayMenu()
{
this.add(new startMenu());
}
public void continueToGame()
{
this.getContentPane().removeAll();
this.add(new gameScreen());
}
class startMenu extends JPanel{
/**
* Serial id
*/
private static final long serialVersionUID = 1L;
private startMenu()
{
setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(Color.blue);
g.setColor(Color.black);
g.drawString(String1, this.getWidth()/2, this.getHeight()/2);
}
}
class gameScreen extends JPanel
{
/**
* Serial id
*/
private static final long serialVersionUID = 1L;
private gameScreen()
{
setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(Color.green);
g.setColor(Color.black);
g.drawString(String2, this.getWidth()/2, this.getHeight()/2);
}
}
}
编辑:
将cont() 更改为
private void cont()
{
Integer input = -1;
while(!(input == 0))
{
input = JOptionPane.showConfirmDialog(theView, "Continue?", null, JOptionPane.YES_OPTION);
System.out.println(input);
}
}
也不行
【问题讨论】:
标签: java eclipse swing user-interface jframe