如何使用 Java 在工具欄中新增分隔符?


要新增分隔符,使用 addSeparator() 方法。讓我們首先建立一個工具欄 −

JToolBar toolbar = new JToolBar();

現在,我們將建立一些元件,並設定一個分隔符來分隔它們,如下所示 −

toolbar.add(new JTextArea(" Add name here"));
toolbar.add(new JButton("Submit Name"));
toolbar.addSeparator();
toolbar.add(new JTextArea(" Add age here"));
toolbar.add(new JButton("Submit Age"));

以下是使用 Java 在工具欄中新增分隔符的示例 −

示例

package my;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
public class SwingDemo {
   public static void main(String[] args) {
      JPanel panel = new JPanel(new BorderLayout());
      JToolBar toolbar = new JToolBar();
      panel.add(toolbar, BorderLayout.PAGE_START);
      toolbar.add(new JTextArea(" Add name here"));
      toolbar.add(new JButton("Submit Name"));
      toolbar.addSeparator();
      toolbar.add(new JTextArea(" Add age here"));
      toolbar.add(new JButton("Submit Age"));
      toolbar.add(new JButton("Clear"));
      toolbar.add(new JButton("Refresh"));
      toolbar.addSeparator();
      JFrame frame = new JFrame(BorderLayout.PAGE_START);
      frame.setTitle("Separator");
      frame.setContentPane(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationByPlatform(true);
      frame.pack();
      frame.setSize(550, 200);
      frame.setVisible(true);
   }
}

輸出

更新於: 2019 年 7 月 30 日

147 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始吧
廣告
© . All rights reserved.