JFrame inside another JFrame

痴心易碎 提交于 2019-11-29 11:09:17

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.

ksodari

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!