- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题的标题不言自明。我正在使用 JFrame 制作扫雷的克隆,并且刚刚完成玩家选择游戏大小的起始屏幕。单击按钮时,框架应该会为游戏屏幕做好准备。当我单击一个按钮时,该按钮仍处于“按下”状态并且 JFrame 卡住并且我必须将其关闭。我做错了什么?
代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Minesweeper extends JFrame implements ActionListener{
JLabel starttitle;
ButtonObject[] gamefield;
JFrame frame;
JPanel startscreen;
JPanel gamescreen;
int gamesize;
JButton ten;
JButton tfive;
JButton fifty;
GridLayout layout;
public Minesweeper()
{
frame = new JFrame("Minesweeper");
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);;
startscreen = new JPanel();
startScreen();
}
public void startScreen()
{
ten = new JButton("10 x 10");
tfive = new JButton("25 x 25");
fifty = new JButton("50 x 50");
starttitle = new JLabel("Welcome to minesweeper. Click a game size to begin.");
frame.add(startscreen);
startscreen.add(starttitle);
startscreen.add(ten);
startscreen.add(tfive);
startscreen.add(fifty);
ten.addActionListener(this);
tfive.addActionListener(this);
fifty.addActionListener(this);
}
public void gameScreen()
{
frame.getContentPane().removeAll();//freezes JFrame
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ten)
{
gamesize = 10;
gameScreen();
}
else if(e.getSource()==tfive)
{
gamesize = 25;
gameScreen();
}
else if(e.getSource()==fifty)
{
gamesize = 50;
gameScreen();
}
else
{
System.out.println("Fatal error");
}
}
public static void main(String[] args)
{
new Minesweeper();
}
}
最佳答案
它不会卡住,调用你的框架的repaint()
方法,所有的组件都会被清除。
只需在 actionPerformed()
中添加下一行:
frame.repaint();
关于java - JFrame 在调用 getContentPane().removeAll() 后卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19927824/
我做了一些研究来了解 JFrame 的使用及其部件,我发现了以下信息: 1- JFrame 的部分 根 Pane 菜单裸露 内容 Pane 玻璃板 2- JFrame 实现 RootPaneConta
我创建了一个扩展 JFrame 的类,默认情况下它会创建一个 JPanel。我试过这个: public class Main extends JFrame { JPanel pane; private
何时使用: Container c = getContentPane(); 以及何时使用: frame.getContentPane(); 最佳答案 getContentPane().setBackg
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有一个扩展 JPanel 并绘制三角形的类。我从其他类(class)调用它来创建三个三角形,但是当绘制第三个三角形时,前两个三角形消失了。如何添加一起显示的多个三角形。代码如下: 三角形.Java:
我这里有一个代码,但我收到了这个错误。如何修复以下代码的错误? Container container = getContentPane(); 这是运行代码。 import java.awt.Bord
我正在处理我的作业,其中一部分要求我在 JFrame 的网格布局中放置一个红色 block 。之后,用户应该能够通过箭头键更改该红色 block 的位置。 到目前为止,我已经能够将红色 block 添
我创建的这些脚本遇到了一些(非常烦人的)问题。 Sburb.java import java.awt.Color; import javax.swing.*; public class Sburb {
我想从内部 Action 类调用外部类的方法 getContentPane()。我不明白为什么我的代码不起作用。 public class MainFrame extends JFrame {
我找到了三种方法来填充我的 JFrame frame = new JFrame("...")createContentPanel 返回一个 JPanel,createToolBar 返回一个 Tool
在这段代码中: JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); fr
我访问过很多有关 Java Swing 的教程,我想知道使用 .getContentPane().add(); 添加组件之间的区别或使用.add() ,哪个是最好的? (如果还有“更好”的话)。 最佳
如果这有助于解决问题,我可以提供所有代码,但我相信,由于这实际上只影响一个类,因此可以在这里解决它,以简化问题。 我有一个 Solitaire 项目(注意包/类名称,这是反复试验,我没有删除旧项目),
java swing 中的静态上下文错误无法引用非静态方法 getContentPane() import javax.swing.*; import java.awt.*; import java.
我刚刚开始使用 pro Window Builder 创建 java GUI,默认情况下创建了 JFrame 和 getContentPane 面板,在我继续创建另一个具有卡片布局的 mainPane
在一个 JFrame 中,我用另一个 JPanel 替换了一个 Jpanel 。 package testing; import java.io.*; import java.util.*; impo
我有一个扩展JFrame的类Window和一个扩展JPanel的类Content。 Content 对象被添加到 Window 对象中。 类窗口: public class Window extend
这个问题的标题不言自明。我正在使用 JFrame 制作扫雷的克隆,并且刚刚完成玩家选择游戏大小的起始屏幕。单击按钮时,框架应该会为游戏屏幕做好准备。当我单击一个按钮时,该按钮仍处于“按下”状态并且 J
我发现我可以轻松使用(JPanel)frame.getContentPane()作为默认层JPanel,即使有边框。 (一开始这让我感到惊讶。) 这是一种常见/不常见/好/坏行为吗?为什么?执行此操作
我知道,as of Java 1.5 ,可以像这样将组件添加到 JFrame: myFrame.add(myButton); 代替: myFrame.getContentPane().add(myBu
我是一名优秀的程序员,十分优秀!