Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JComponent不可见,有人知道为什么吗?_Java_Swing_Custom Controls_Paint - Fatal编程技术网

Java JComponent不可见,有人知道为什么吗?

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

以下是我的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.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方法
编辑:

为了遵守Swing线程规则,main方法中的代码应该在EDT线程上运行。这意味着它必须被包装成

SwingUtilities.invokeLater(new Runnable() {
     public void run() {
         // your code goes here
     } 
}

下面是这里发生的事情:

  • JFrame内容窗格的默认布局为BorderLayout。这意味着您的组件已调整为内容窗格的整个大小。要在添加组件之前修复此问题,请执行以下操作
  • 背景不会在JComponent的直接后代中自动绘制。您可以使组件不透明(setOpaque(true)),从JPanel扩展,或者像这样重写paintComponent方法
编辑:

为了遵守Swing线程规则,main方法中的代码应该在EDT线程上运行。这意味着它必须被包装成

SwingUtilities.invokeLater(new Runnable() {
     public void run() {
         // your code goes here
     } 
}

默认情况下,JComponents不是不透明的,因此不会绘制背景。调用
setOpaque(true)
就可以了

关于您的代码的2条评论:

  • 如果要编写自定义绘制代码,请重写
    paintComponent()
    ,而不是
    paint()
  • 对UI组件的任何更改都必须在EventDispatchThread(EDT)上进行,否则将导致从行为不一致到完全崩溃的一般不良后果。因此,在主要应用程序中,您需要将代码包装在调用器中:

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            // your code goes here
        }
    }
    

  • 默认情况下,JComponents不是不透明的,因此不会绘制背景。调用
    setOpaque(true)
    就可以了

    关于您的代码的2条评论:

  • 如果要编写自定义绘制代码,请重写
    paintComponent()
    ,而不是
    paint()
  • 对UI组件的任何更改都必须在EventDispatchThread(EDT)上进行,否则将导致从行为不一致到完全崩溃的一般不良后果。因此,在主要应用程序中,您需要将代码包装在调用器中:

    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
        }
    }