- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个扩展JFrame
的类Window
和一个扩展JPanel
的类Content
。 Content
对象被添加到 Window
对象中。
类窗口
:
public class Window extends JFrame
{
private Content content;
public Window()
{
setTitle("My Window");
setSize(800, 600);
setResizable(false);
setLocationRelativeTo(getParent());
setDefaultCloseOperation(EXIT_ON_CLOSE);
content = new Content(this);
add(content);
setVisible(true);
}
public static void main(String[] args)
{
new Window();
}
}
类内容
:
public class Content extends JPanel
{
public Content(Window w)
{
window = w;
System.out.println(window.getContentPane().getWidth());
}
}
现在我需要知道内容 Pane 的宽度。但是 window.getContentPane().getWidth()
返回 0。
你能告诉我为什么吗?
最佳答案
在尝试调用 getWidth() 之前,先使用 SetPreferredSize() 然后使用 Pack() 是关键。这段代码只是您的代码,稍加修改,就可以正常工作。
public class Window extends JFrame
{
private Content content;
public Window()
{
setTitle("My Window");
setPreferredSize(new Dimension(800, 600));
setResizable(false);
setLocationRelativeTo(getParent());
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
content = new Content(this);
add(content);
setVisible(true);
}
public static void main(String[] args)
{
new Window();
}
}
public class Content extends JPanel
{
Window window = null;
public Content(Window w)
{
window = w;
System.out.println(window.getContentPane().getWidth());
}
}
关于java - 为什么 getContentPane().getWidth() 返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29481059/
我做了一些研究来了解 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
我是一名优秀的程序员,十分优秀!