2017-08-22 79 views
-1

我在显示JFrame上的组件时遇到问题。我正在关闭当前窗口并打开新窗口并希望显示jLabel,但没有任何事情发生。代码如下:JFrame不显示组件

   Frame[] nF = DBChooser.getFrames(); 

       nF[0].setVisible(false); 
       JFrame windoow = new JFrame("Processing");      
       JPanel pan = new JPanel(); 
       windoow.setPreferredSize(new Dimension(400, 150)); 
       pan.setPreferredSize(new Dimension(400, 150));     

       JLabel textLabel = new JLabel ("Processing..."); 
       textLabel.setLayout(null); 
       pan.setLayout(null); 
       windoow.setLayout(null);     
       pan.add(textLabel); 
       pan.revalidate(); 
       pan.repaint();     
       windoow.getContentPane().add(pan); 
       windoow.setLocationRelativeTo(null);      
       windoow.pack();     
       windoow.setVisible(true); 

我感谢所有帮助

+0

1)Java的图形用户界面有不同的OS”,屏幕大小,屏幕分辨率等方面的工作在不同的地区使用不同的PLAFs。因此,它们不利于像素的完美布局。请使用布局管理器或[它们的组合](http://stackoverflow.com/a/5630271/418556)以及[white space]的布局填充和边框(http://stackoverflow.com/a/17874718/ 418556)。 2)为了更快地获得更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 3)以最小尺寸提供ASCII图形或简单的GUI图形布局* –

+0

..如果可调整大小,则具有更大的宽度和高度。 –

回答

1

这是因为你设置一个空的布局窗口,面板不指定任何宽度,lenght或位置,或者使用一些LayoutManager或设置这些属性(如边界)。一个空的LayoutManager意味着你需要自己设置所有的东西,因为没有任何东西(没有LayoutManager)会自动放置你的元素。这个例子使用一个BorderLayout的,它创建了一个很好的效果:

enter image description here

代码:

import java.awt.BorderLayout; 
import java.awt.Dimension; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class Test { 
    public static void main(String[] args) { 
     JFrame windoow = new JFrame("Processing"); 
     JPanel pan = new JPanel(); 
     windoow.setPreferredSize(new Dimension(400, 150)); 
     pan.setPreferredSize(new Dimension(400, 150)); 

     JLabel textLabel = new JLabel("Processing..."); 
     textLabel.setLayout(null); 
     pan.setLayout(new BorderLayout()); 
     windoow.setLayout(new BorderLayout()); 
     pan.add(textLabel); 
     pan.revalidate(); 
     pan.repaint(); 
     windoow.getContentPane().add(pan); 
     windoow.setLocationRelativeTo(null); 
     windoow.pack(); 
     windoow.setVisible(true); 
    } 
} 
+0

仍然是一样的,标签不显示 – johansonik

+0

@johansonik当我运行相同的代码时,如何不显示它,并且我已经发布了带有消息的窗口图像? –

+0

我不知道为什么,但我可以看到没有任何文字的空白窗口... – johansonik

1

为什么需要这么多setLayout(null);?我删除它们,它的工作

public class DBChooser extends Frame { 

    public static void main(String args[]) { 
     Frame[] nF = DBChooser.getFrames(); 
//  nF[0].setVisible(false); 
     JFrame windoow = new JFrame("Processing"); 
     JPanel pan = new JPanel(); 
     windoow.setPreferredSize(new Dimension(400, 150)); 
     pan.setPreferredSize(new Dimension(400, 150)); 
     JLabel textLabel = new JLabel("Processing..."); 
//  textLabel.setLayout(null); 
//  pan.setLayout(null); 
//  windoow.setLayout(null);     
     pan.add(textLabel); 
     pan.revalidate(); 
     pan.repaint(); 
     windoow.getContentPane().add(pan); 
     windoow.setLocationRelativeTo(null); 
     windoow.pack(); 
     windoow.setVisible(true); 
    } 
} 
+0

它没有帮助,标签不显示 – johansonik

+0

它在我的电脑上正常工作。也许你需要在这里发布更多的代码 –

+0

什么是'nF [0] .setVisible(false);'?你确定你的代码可以在没有评论的情况下进行编译? –