如何在 Java 中使用 Box 分離行或列中的元件


要在行或列中分隔元件,請使用 createGlue() 方法。這將建立一個分隔元件的不可見“粘合劑”元件。

以下是一個使用 Box 在行或列中分隔元件的示例 −

示例

package my;
import java.awt.BorderLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Matches");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JButton button1 = new JButton("CSK");
      JButton button2 = new JButton("DC");
      JButton button3 = new JButton("MI");
      JButton button4 = new JButton("SRH");
      JButton button5 = new JButton("RR");
      JButton button6 = new JButton("KKR");
      Box box = new Box(BoxLayout.X_AXIS);
      box.add(button1);
      box.add(button2);
      box.add(Box.createGlue());
      box.add(button3);
      box.add(button4);
      box.add(Box.createGlue());
      box.add(button5);
      box.add(button6);
      JScrollPane jScrollPane = new JScrollPane();
      jScrollPane.setViewportView(box);
      frame.add(jScrollPane, BorderLayout.CENTER);
      frame.setSize(550, 250);
      frame.setVisible(true);
   }
}

輸出

更新日期:30-Jul-2019

257 次瀏覽

開啟你的職業生涯

完成課程進行認證

開始
廣告
© . All rights reserved.