java - JPanel 位于另一个 JPanel 内 : Swing

标签 java swing layout-manager grid-layout boxlayout

我正在编写一个程序,应该显示广播电台的信息,我希望用户通过单击他们喜欢的 channel 的图像来获取该信息。

channel 是使用网格布局设置的,但我想要每个网格内部都是 channel 的图像和图像正下方的 channel 名称。所以我认为 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);

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

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

最佳答案

回复:

[...] what I want inside of each grid is both the image of the channel and the name of the channel right under the image.

参见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);

关于java - JPanel 位于另一个 JPanel 内 : Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47487039/

相关文章:

java - GroupLayout 未按预期运行

java - 在 Swing GUI 中提供空白

java - 如何关闭antlr4中的不匹配警告

java - RDBMS、JCR、CMIS 之间丢失

java - Window 类型的 setOpacity(float) 方法不可见

java - JPanels 一个在另一个之上

java - java实现TCP server和TCP client传输文件的方法

Java Applet网格布局问题

Java swing组件之间的通信

JFrame 中的 Java 多线程