• Java基础之创建窗口——使用流布局管理器(TryFlowLayout)


    控制台程序。

    FlowLayout把组件放在容器的连续行中,使每一行都放置尽可能多的组件。如果某行已满,就放在下一行。工作方式类似于文本处理器把单词放在行中。主要用途是放置按钮,但也可以用来放置其他组件。这是JPanel对象的默认布局管理器。

    用来放置组建的行的默认定位方式是在容器中居中,默认方式是从左到右。利用FlowLayout类中定义的int常量,可以指定5个行定位选项:LEFT、RIGHT、CENTER、LEADING和TRAILING。CENTER选项是默认的。行中的组件默认有5个单位的空隙,行之间也有5个单位的空隙。

     1 import javax.swing.JFrame;
     2 import javax.swing.SwingUtilities;
     3 import java.awt.Toolkit;
     4 import java.awt.Dimension;
     5 import javax.swing.JButton;
     6 import java.awt.Container;
     7 import java.awt.FlowLayout;
     8 
     9 public class TryFlowLayout {
    10   // Create the application window
    11   public static void createWindow(){
    12     JFrame aWindow = new JFrame("This is the Window Title");
    13     Toolkit theKit = aWindow.getToolkit();                             // Get the window toolkit
    14     Dimension wndSize = theKit.getScreenSize();                        // Get screen size
    15 
    16     // Set the position to screen center & size to half screen size
    17     aWindow.setSize(wndSize.width/2, wndSize.height/2);                // Set window size
    18     aWindow.setLocationRelativeTo(null);                               // Center window
    19     aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    20 
    21     FlowLayout flow = new FlowLayout();                                // Create a layout manager
    22 //    FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
    23 //    FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 20, 30);
    24     Container content = aWindow.getContentPane();                      // Get the content pane
    25     content.setLayout(flow);                                           // Set the container layout mgr
    26 
    27     // Now add six button components
    28     for(int i =  1; i <= 6 ; ++i)
    29       content.add(new JButton("Press " + i));                          // Add a Button to content pane
    30 
    31 //    aWindow.pack();
    32     aWindow.setVisible(true);                                          // Display the window
    33   }
    34 
    35   public static void main(String[] args) {
    36     SwingUtilities.invokeLater(new Runnable() {
    37             public void run() {
    38                 createWindow();
    39             }
    40         });
    41   }
    42 }
  • 相关阅读:
    在c#中用指针操作图片像素点
    获取exe目录
    MySQL数据库之安装
    简介、变量、常数、if、基础数据类型、注释、input()
    day 1 预习
    Typora基础
    内存流
    System.in流
    Java&nbsp;IO&nbsp;操作
    字节流、字符流
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3464353.html
Copyright © 2020-2023  润新知