zoukankan      html  css  js  c++  java
  • 简易计算器布局 BorderLayout JPanel GridLayout 混合

    import java.awt.*;
    import javax.swing.*;
    
    public class swingDemo {
        private static void addComponentsToPane(Container pane) {
            pane.setLayout(new BorderLayout()); // content pane默认是BorderLayout,因此这里可以省略
            // 构造函数BorderLayout(int horizontalGap, int verticalGap)
            // pane.setLayout(new BorderLayout(10, 10));//水平间隔 竖直间隔
            //JButton button = new JButton("Button 1 (PAGE_START)");
            //pane.add(button, BorderLayout.PAGE_START);
    
            JTextField text = new JTextField();
            pane.add(text,BorderLayout.NORTH);
            JPanel panel = new JPanel();
            pane.add(panel, BorderLayout.CENTER);
            panel.setLayout(new GridLayout(4,4,2,2));//参数依次为: 行数 列数 水平距离 竖直距离
    
            panel.add(new JButton("7"));
            panel.add(new JButton("8"));
            panel.add(new JButton("9"));
            panel.add(new JButton("-"));
    
            panel.add(new JButton("4"));
            panel.add(new JButton("5"));
            panel.add(new JButton("6"));
            panel.add(new JButton("+"));
    
            panel.add(new JButton("1"));
            panel.add(new JButton("2"));
            panel.add(new JButton("3"));
            panel.add(new JButton("/"));
    
            panel.add(new JButton("0"));
            panel.add(new JButton("."));
            panel.add(new JButton(" "));
            panel.add(new JButton("="));
    /*方法二:*//* String str[]= {"7","8","9","-","4","5","6","+","1","2","3","/","0","."," ","="};  
            for(int i =0;i<str.length;i++){  
                JButton str1=new JButton(str[i]);  
                p1.add(str1);  
                str1.setFont(new Font("宋体",Font.BOLD,20));  
            }  
    
    */
    
    
        }
    
        private static void createAndShowGUI() {
    
            //Create and set up the window.
            JFrame frame = new JFrame("BorderLayout");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //Set up the content pane.
            addComponentsToPane(frame.getContentPane());
            frame.pack();
            //Display the window.
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    

    参考资料 : java swing编程介绍      博主老师

    鲜花会生锈,盐巴会腐烂
  • 相关阅读:
    windows cmd command line 命令
    windows cmd color setup
    二进制中1的个数
    斐波拉契数列
    旋转数组的最小数字
    用两个栈实现队列
    重建二叉树
    Bootstrap学习笔记(三) 网格系统
    Bootstrap学习笔记(二) 表单
    Bootstrap学习笔记(一) 排版
  • 原文地址:https://www.cnblogs.com/hunterxing/p/9709307.html
Copyright © 2011-2022 走看看