[英]JFrame inside another JFrame
我有一盘象棋。 我已经写了 3 个类。 第一个如果是游戏。 (棋盘、棋子等)另一个是菜单。 (新建、打开、设置时间等按钮)
他们都使用 JFrame。
我想把上面提到的两个类都放到第三个类中。 例如,游戏窗口在左边,菜单在右边。 第三个类还将通过 JFrame 显示整个应用程序。
怎么做?
你不能把一个 JFrame 放在另一个里面。 您在这里有几个设计选择。 您可以将 JFrames 更改为 JPanels。 这可能是最简单的更改。 另一方面,您可以考虑使用内部框架。
您可以为此使用 JPanels。 那样更容易...主窗口使用 JFrame,菜单项使用其中的 JPanel。 搜索有关 JPanel 使用的教程。
在您的情况下,我建议您使用可以添加到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();
}
}
最好的办法是保持外部框架不变,并将内部内容更改为 JPanels。 当我编写 Chess 时,我有一个扩展 JFrame 的外框架和一个扩展 JPanel 的内面板,我在上面放置了棋盘。 电路板本身由 64 个 JButton 组成。
根据您的描述,我认为这将是一个很好的起点:
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();
}
}
另外,警告一句:
完全实现国际象棋的逻辑是非常困难的,无论你为图形做什么。
希望这可以帮助!
我想这就是你想要做的。
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);
}
}
您有一个 MainFrame (OuterFrame),您可以创建它。 但是,您在此 MainFrame 中创建了一个 JFrame。 这不是一件美妙的事情,但它肯定是一种打开“JFrame 内部”的方式。 这将在屏幕上为您提供两个“窗口”。 您可以在 MainFrame 中创建无数的 JFrame。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.