Java JComponent不可见,有人知道为什么吗?
以下是我的JFrame代码:Java JComponent不可见,有人知道为什么吗?,java,swing,custom-controls,paint,Java,Swing,Custom Controls,Paint,以下是我的JFrame代码: public static void main(String[] args) { JFrame jf = new JFrame(); jf.setSize(600,600); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyCustomWidget widget = MyCustomWidget.createWidget(400, 400); widget.setVi
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setSize(600,600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyCustomWidget widget = MyCustomWidget.createWidget(400, 400);
widget.setVisible(true);
// just to set x and y
widget.setLocation(40, 40);
jf.getContentPane().add(widget);
jf.setVisible(true);
}
下面是MyCustomWidget的代码:
public class MyCustomWidget extends JComponent {
public void paint(Graphics g)
{
super.paint(g);
}
public static MyCustomWidget createWidget(int width,int height)
{
MyCustomWidget tw = new MyCustomWidget();
tw.setBounds(0,0,width,height);
tw.setBackground(Color.RED);
return tw;
}
}
问题是,JComponent没有显示在窗口中,我不明白为什么。我甚至添加了一个小部件。setVisible(true)
,以确保它是可见的。什么都不管用。你能看出我做错了什么吗
在你们建议的修改之后,代码现在是:
包javaapplication2
public class Main {
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame jf = new JFrame();
jf.setSize(600,600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(null);
JComponent container = (JComponent) jf.getContentPane();
container.setDebugGraphicsOptions(DebugGraphics.FLASH_OPTION);
DebugGraphics.setFlashColor(Color.RED);
DebugGraphics.setFlashCount(2);
DebugGraphics.setFlashTime(100);
MyCustomWidget widget = MyCustomWidget.createTimeline(400, 400);
container.add(widget);
jf.setVisible(true);
}
});
}
}
以及:
公共类MyCustomWidget扩展JComponent{
公共组件(图形g)
{
设置前景(颜色为黑色);
抽绳(g);
}
//一个小东西,看看有什么东西被画出来了
专用空心抽绳(图g)
{
int distanceBetween=getHeight()/numberOfLines;
int start=0;
int colourIndex=0;
颜色[]={Color.BLUE,Color.WHITE,Color.YELLOW};
for(int i=0;i
下面是这里发生的事情:
- JFrame内容窗格的默认布局为BorderLayout。这意味着您的组件已调整为内容窗格的整个大小。要在添加组件之前修复此问题,请执行以下操作
- 背景不会在JComponent的直接后代中自动绘制。您可以使组件不透明(setOpaque(true)),从JPanel扩展,或者像这样重写paintComponent方法
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// your code goes here
}
}
下面是这里发生的事情:
- JFrame内容窗格的默认布局为BorderLayout。这意味着您的组件已调整为内容窗格的整个大小。要在添加组件之前修复此问题,请执行以下操作
- 背景不会在JComponent的直接后代中自动绘制。您可以使组件不透明(setOpaque(true)),从JPanel扩展,或者像这样重写paintComponent方法
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// your code goes here
}
}
默认情况下,JComponents不是不透明的,因此不会绘制背景。调用
setOpaque(true)
就可以了
关于您的代码的2条评论:
paintComponent()
,而不是paint()
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// your code goes here
}
}
默认情况下,JComponents不是不透明的,因此不会绘制背景。调用
setOpaque(true)
就可以了
关于您的代码的2条评论:
paintComponent()
,而不是paint()
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// your code goes here
}
}
这种方法似乎有效。。。但是,有时当我运行JFrame时,什么也不显示,而其他时候,小部件显示正确。我正在Kubuntu 9.04上开发,并使用:java版本“1.6.0_0”/OpenJDK运行时环境(IcedTea6 1.6.1)(6b16-1.6.1-3ubuntu1)/OpenJDK客户端虚拟机(构建14.0-b16,混合模式,共享)main方法中的代码应包装到SwingUtilities.invokeLater中,以符合Swing线程规则。请调用super.paintComponent或使用set不透明(true)而不是重写paintComponent。我尝试了这两种方法。10次跑步中仍有9次没有显示任何内容。此外,当没有显示任何内容时,JFrame会冻结,如中所示,除非明确终止进程,否则我无法停止它。我迷路了,这和你使用图形有关。当我删除4行与DebugGraphics相关的代码时,一切都很好。。。但是,有时当我运行JFrame时,什么也不显示,而其他时候,小部件显示正确。我正在Kubuntu 9.04上开发,并使用:java版本“1.6.0_0”/OpenJDK运行时环境(IcedTea6 1.6.1)(6b16-1.6.1-3ubuntu1)/OpenJDK客户端虚拟机(构建14.0-b16,混合模式,共享)main方法中的代码应包装到SwingUtilities.invokeLater中,以符合Swing线程规则。请调用super.paintComponent或使用set不透明(true)而不是重写paintComponent。我尝试了这两种方法。10次跑步中仍有9次没有显示任何内容。此外,当没有显示任何内容时,JFrame会冻结,如中所示,除非明确终止进程,否则我无法停止它。我迷路了,这和你使用图形有关。当我删除与DebugGraphics相关的4行时,一切都正常工作。
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// your code goes here
}
}