2017-11-25 100 views
1

我正在编写一个应该在电台上显示信息的程序,我希望用户通过单击他们喜欢的频道的图像来获取该信息。另一个JPanel中的JPanel:Swing

通道设置为带有网格布局的upp,但是我想在每个网格内部都是通道的图像和图像下的通道名称。所以我认为GridLayout里面的一个BoxLayout可以做到这一点,但我没有看到我想要的GUI。

jf = new JFrame(); 

     jf.setTitle("tutorial"); 
     jf.setSize(500, 500); 
     jf.setMinimumSize(new Dimension(500, 500)); 
     jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

     JPanel jPanel = new JPanel(); 
     jPanel.setLayout(new BoxLayout(jPanel,BoxLayout.Y_AXIS)); 
     JButton jButton1 = new JButton("first"); 
     JButton jButton2 = new JButton("second"); 
     jPanel.add(jButton1); 
     jPanel.add(jButton2); 
     jf.add(jPanel); 

     grid = new JPanel(); 
     grid.setLayout(new GridLayout(3,4,5,10)); 
     grid.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 


     JPanel jPanel2 = new JPanel(); 
     jPanel.setLayout(new BoxLayout(jPanel,BoxLayout.PAGE_AXIS)); 
     JButton jButton = new JButton("first"); 
     JButton jButton12 = new JButton("second"); 
     jPanel.add(jButton); 
     jPanel.add(jButton12); 
     grid.add(jPanel); 
     grid.add(jPanel2); 

     jf.add(grid); 

下面是我尝试将两个按钮的框布局放入网格布局的一些示例代码。

我应该如何将一个布局放入另一个布局?

+1

1)为了更好地帮助越早,张贴[MCVE]或[简要,自包含的,正确的示例](HTTP:/ /www.sscce.org/)。 2)以最小尺寸提供ASCII图形或简单的GUI图形布局图,并且如果可调整大小,具有更多宽度和高度,则显示应该如何使用额外空间。 3)*“我没有看到我想要的GUI。”*它看起来如何?截图会很方便。如果你没有足够的代表。将截图嵌入[edit],将其上传到图像共享网站并提供链接。 –

+1

'我想要在每个网格内部既是通道的图像又是图像下的通道名称 - 您可以将图像和文本添加到同一个按钮。阅读API有一些方法可以让你设置文本相对于图像的位置。 – camickr

回答

1

回复:

[...]我希望每个网格内是通道的两个图像和右眼图像下的通道的名称。

看到How to Use Buttons, Check Boxes, and Radio Buttons

enter image description here

ImageIcon middleButtonIcon = createImageIcon("images/middle.gif"); 

... 

b2 = new JButton("Middle button", middleButtonIcon); 
b2.setVerticalTextPosition(AbstractButton.BOTTOM); 
b2.setHorizontalTextPosition(AbstractButton.CENTER); 
相关问题