简体   繁体   中英

JFrame inside another JFrame

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)

Both of them use JFrame.

I would like to put both classes mentioned above into the 3rd class. For example the Game window would be on the left, and the menu on the right. The third class would also show the whole app by JFrame.

How to do that?

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.

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.

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();
    }
}

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM