简体   繁体   English

另一个 JFrame 中的 JFrame

[英]JFrame inside another JFrame

I have a game of chess.我有一盘象棋。 I have written 3 classes.我已经写了 3 个类。 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.他们都使用 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.第三个类还将通过 JFrame 显示整个应用程序。

How to do that?怎么做?

You can't put one JFrame inside another.你不能把一个 JFrame 放在另一个里面。 You have a couple of design choices here.您在这里有几个设计选择。 You can change your JFrames to JPanels.您可以将 JFrames 更改为 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.您可以为此使用 JPanels。 It's easier that way... use a JFrame for the main window and for menu items use a JPanel inside it.那样更容​​易...主窗口使用 JFrame,菜单项使用其中的 JPanel。 Search for tutorials on JPanel usage.搜索有关 JPanel 使用的教程。

In your case i suggest you to use JInternalFrame which can be added inside Jframe try out this code i hope it will work:在您的情况下,我建议您使用可以添加到Jframe 中的JInternalFrame试试这个代码,我希望它会起作用:

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.最好的办法是保持外部框架不变,并将内部内容更改为 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.当我编写 Chess 时,我有一个扩展 JFrame 的外框架和一个扩展 JPanel 的内面板,我在上面放置了棋盘。 The board itself was comprised of 64 JButtons.电路板本身由 64 个 JButton 组成。

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.您有一个 MainFrame (OuterFrame),您可以创建它。 But, you create a JFrame inside this MainFrame.但是,您在此 MainFrame 中创建了一个 JFrame。 That's not a beautiful thing to do, but it's certainly a way of opening a "JFrame inside the other".这不是一件美妙的事情,但它肯定是一种打开“JFrame 内部”的方式。 This will give you two "windows" on the screen.这将在屏幕上为您提供两个“窗口”。 You could create countless JFrames inside the MainFrame.您可以在 MainFrame 中创建无数的 JFrame。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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