有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java似乎无法在JFrame中更新我的GridLayout。现在它增加了越来越多的帧,我只想更新它

所以这是我的跑步者,负责我的整个比赛。CharacterGame、CharacterChoice和CharacterHead都是包含图形和绘画的类。我希望能够通过按钮代码中的移除和重新绘制,将这些类中的不同部分调用到图形中。现在,它只是用越来越多的面板更新框架,而不是移除旧面板并将这些面板添加到旧面板中。我希望这是有意义的,因为我对图形有点陌生,可能还没有想到最好的方法。如果有人能告诉我怎么做,我将非常感激。谢谢我已经尝试了我能想到的一切。。。从removeAll方法到重新绘制。我想不出还有别的事可做

无法工作的部分位于choice1Button listener中的ShowButtonDemo()中

public class GraphicsRunner extends JFrame
{
   private static final int WIDTH = 1280;
   private static final int HEIGHT = 980;
   private JFrame mainframe;
   private JPanel controlPanel;
   private JPanel headPanel;
   private JPanel gamePanel;
   private JPanel choicePanel;
   private int gameInt = 0;
   private int choiceInt = 0;
   private int endGame = 0;

public GraphicsRunner()
{
    super("Stranded...");

    mainframe = new JFrame();

    mainframe.setBackground(Color.BLACK);
    mainframe.setSize(WIDTH,HEIGHT);
    mainframe.setLayout(new GridLayout(4, 0));
    mainframe.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });
    headPanel = new JPanel();
    gamePanel = new JPanel();
    choicePanel = new JPanel();
    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    headPanel = new CharacterHeading();
    gamePanel = new CharacterGame(gameInt);
    choicePanel = new CharacterChoice(choiceInt);

    mainframe.add(headPanel);
    mainframe.add(gamePanel);
    mainframe.add(choicePanel);
    mainframe.add(controlPanel);

    mainframe.setDefaultCloseOperation(EXIT_ON_CLOSE);

    mainframe.setVisible(true);
}

public static void main( String args[] )
{
    GraphicsRunner run = new GraphicsRunner();
    run.showButtonDemo();
}

 private void showButtonDemo(){

      JButton choice1Button = new JButton("Choice #1");        
      JButton choice2Button = new JButton("Choice #2");
      JButton choice3Button = new JButton("Choice #3");
      choice3Button.setHorizontalTextPosition(SwingConstants.LEFT);   

      choice1Button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
             gameInt = gameInt + 1;
             choiceInt = choiceInt + 1;

             gamePanel = new CharacterGame(gameInt);

             //This is the part I am currently working on and I was hoping
             //it would remove the panel instead of just removing the
             //content on the panel...
             mainframe.remove(headPanel);
             mainframe.remove(gamePanel);
             mainframe.remove(choicePanel);
             mainframe.remove(controlPanel);
             mainframe.add(headPanel, 0);
             mainframe.add(gamePanel, 1);
             mainframe.add(choicePanel, 2);
             mainframe.add(controlPanel, 3);
             mainframe.revalidate();
             mainframe.repaint();

             System.out.println(gameInt);
         }          
      });

      controlPanel.setBackground(Color.BLACK);
      controlPanel.add(choice1Button);
      controlPanel.add(choice2Button);
      controlPanel.add(choice3Button);       

      mainframe.setVisible(true);  
   }

}


共 (0) 个答案