JFrame inside another JFrame

后端 未结 5 683
[愿得一人]
[愿得一人] 2020-12-19 10:55

I have a game of chess. I have written 3 classes. 1st if for game. (chessboard, pieces, and so on) And another one is for menu. (buttons like new, open, set time)

Bo

相关标签:
5条回答
  • 2020-12-19 11:07

    You can't put one JFrame inside another. You have a couple of design choices here. You can change your JFrames to JPanels. This is probably the easiest change. On the other hand, you can look at using Internal Frames instead.

    0 讨论(0)
  • 2020-12-19 11:08

    The best thing to do would be to leave the outer frame as it is and change the inner contents to JPanels. When I wrote Chess, I had an outer frame which extended JFrame, and inner panel that extended JPanel on which I placed the board. The board itself was comprised of 64 JButtons.

    Given your description, I think this would be a good starting point:

    package data_structures;
    
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    @SuppressWarnings("serial")
    public class Chess extends JFrame implements ActionListener {
    
        private JButton[][] tiles; 
    
        public Chess() {
    
            setTitle("Chess");
            setSize(500, 500);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            setVisible(true);
    
            setLayout(new BorderLayout());
    
            JPanel board = new JPanel();
    
            board.setLayout(new GridLayout(8, 8));
    
            tiles = new JButton[8][8];
    
            for(int y = 0; y < tiles.length; y++) {
    
                for(int x = 0; x < tiles[y].length; x++) {
    
                    tiles[x][y] = new JButton();
    
                    tiles[x][y].setActionCommand(x + " " + y);
                    tiles[x][y].addActionListener(this);
    
                    board.add(tiles[x][y]);
                }
            }
    
            add(board, BorderLayout.CENTER);
    
            JPanel options = new JPanel();
            options.setLayout(new GridLayout(1, 3));
    
            JButton newGame = new JButton("New");
            newGame.addActionListener(this);
    
            options.add(newGame);
    
            JButton openGame = new JButton("Open");
            openGame.addActionListener(this);
    
            options.add(openGame);
    
            JButton setTime = new JButton("Set Time");
            setTime.addActionListener(this);
    
            options.add(setTime);
    
            add(options, BorderLayout.SOUTH);
    
            revalidate();
        }
    
        public void actionPerformed(ActionEvent event) {
    
            String command = event.getActionCommand();
    
            System.out.println(command);
    
            revalidate();
        }
    
        public static void main(String[] args) {
            new Chess();
        }
    }
    

    Also, a word of warning:

    Fully implementing the logic of Chess is very difficult, no matter what you do for the graphics.

    Hope this helps!

    0 讨论(0)
  • 2020-12-19 11:13

    In your case i suggest you to use JInternalFrame which can be added inside Jframe try out this code i hope it will work:

    package demo;
    
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    
    public class Demo {
        public static void main(String[] args) {
    
            JFrame jf=new JFrame();
            jf.setLayout(null);
            jf.setSize(1280, 720);
            jf.setVisible(true);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JInternalFrame jInternalFrame=new JInternalFrame();
            jInternalFrame.setLocation(100, 100);
            jInternalFrame.setSize(500, 300);
            jInternalFrame.setTitle("Internal frame");
            jInternalFrame.setVisible(true);
            jInternalFrame.setClosable(true);
            jInternalFrame.setResizable(true);
            jf.add(jInternalFrame);
            jf.repaint();
        }
    }
    
    0 讨论(0)
  • 2020-12-19 11:16

    I guess that's what you want to do.

    public class OuterFrame extends JFrame {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        OuterFrame outerFrame = new OuterFrame();
                        outerFrame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        public OuterFrame() {
            JFrame innerFrame = new JFrame();
            innerFrame.setVisible(true);
        }    
    }
    

    You have a MainFrame (OuterFrame), and you create it. But, you create a JFrame inside this MainFrame. That's not a beautiful thing to do, but it's certainly a way of opening a "JFrame inside the other". This will give you two "windows" on the screen. You could create countless JFrames inside the MainFrame.

    0 讨论(0)
  • 2020-12-19 11:20

    You can use JPanels for that. It's easier that way... use a JFrame for the main window and for menu items use a JPanel inside it. Search for tutorials on JPanel usage.

    0 讨论(0)
提交回复
热议问题