如何用 Java 建立一個垂直工具欄?


我們首先建立一個工具欄 −

JToolBar toolbar = new JToolBar();

現在,設定方向以建立垂直工具欄 −

toolbar.setOrientation(SwingConstants.VERTICAL);

以下是用 Java 建立垂直工具欄的示例 −

package my;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      JToolBar toolbar = new JToolBar();
      toolbar.setOrientation(SwingConstants.VERTICAL);
      toolbar.add(new JTextArea());
      toolbar.add(new JButton("Submit"));
      frame.add(toolbar,BorderLayout.WEST);
      frame.setTitle("Vertical ToolBar");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      //frame.setLocationByPlatform(true);
      frame.setSize(550, 400);
      frame.setVisible(true);
   }
}

輸出

更新於: 30-Jul-2019

386 次瀏覽

開啟你的 事業

完成課程,獲得認證

開始
廣告
© . All rights reserved.