GUI multiple frames switch

后端 未结 3 554
攒了一身酷
攒了一身酷 2020-12-10 18:47

I am writing a program for a black jack game. It is an assignment we are not to use gui\'s but I am doing it for extra credit I have created two frames ant they are working.

3条回答
  •  一个人的身影
    2020-12-10 19:31

    A couple of suggestions:

    You're adding components to a JPanel that uses FlowLayout but are using BorderLayout constants when doing this which you shouldn't do as it doesn't make sense:

      add(play, BorderLayout.CENTER);
    

    Rather, if using FlowLayout, just add the components without those constants.

    Also, rather than swap JFrames, you might want to consider using a CardLayout and swapping veiws in a single JFrame. For instance:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FooBarBazDriver {
       private static final String INTRO = "intro";
       private static final String GAME = "game";
       private CardLayout cardlayout = new CardLayout();
       private JPanel mainPanel = new JPanel(cardlayout);
       private IntroPanel introPanel = new IntroPanel();
       private GamePanel gamePanel = new GamePanel();
    
       public FooBarBazDriver() {
          mainPanel.add(introPanel.getMainComponent(), INTRO);
          mainPanel.add(gamePanel.getMainComponent(), GAME);
    
          introPanel.addBazBtnActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                cardlayout.show(mainPanel, GAME);
             }
          });
    
          gamePanel.addBackBtnActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                cardlayout.show(mainPanel, INTRO);
             }
          });
       }
    
       private JComponent getMainComponent() {
          return mainPanel;
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("Foo Bar Baz");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new FooBarBazDriver().getMainComponent());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class IntroPanel {
       private JPanel mainPanel = new JPanel();
       private JButton baz = new JButton("Baz");
       private JButton exit = new JButton("Exit");
    
       public IntroPanel() {
          mainPanel.setLayout(new FlowLayout());
          baz = new JButton("Start");
          exit = new JButton("exit");
    
          mainPanel.add(new JLabel("Hello World"));
          mainPanel.add(baz);
          mainPanel.add(exit);
    
          exit.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                Window win = SwingUtilities.getWindowAncestor(mainPanel);
                win.dispose();
             }
          });
       }
    
       public void addBazBtnActionListener(ActionListener listener) {
          baz.addActionListener(listener);
       }
    
       public JComponent getMainComponent() {
          return mainPanel;
       }
    
    }
    
    class GamePanel {
       private static final Dimension MAIN_SIZE = new Dimension(400, 200);
       private JPanel mainPanel = new JPanel();
    
       private JButton foo;
       private JButton bar;
       private JButton back;
    
       public GamePanel() {
          foo = new JButton("Foo");
          bar = new JButton("Bar");
          back = new JButton("return to main menu");
    
          mainPanel.add(foo);
          mainPanel.add(bar);
          mainPanel.add(back);
          mainPanel.setPreferredSize(MAIN_SIZE);
       }
    
       public JComponent getMainComponent() {
          return mainPanel;
       }
    
       public void addBackBtnActionListener(ActionListener listener) {
          back.addActionListener(listener);
       }
    
    }
    

提交回复
热议问题