第十二周

Posted allen15773771785

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十二周相关的知识,希望对你有一定的参考价值。

布局管理器

 

绝对界面

package 计算机界面;

import java.awt.*;
import javax.swing.*;
 
public class DD extends JFrame{
 
    JButton jb1,jb2;
    JTextField jtf;
    JPasswordField jpwd;
    JLabel jl1,jl2;
    JPanel jp1,jp2,jp3;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DD test6=new DD();
    }
    public DD()
    {
        //创建组件
        jb1=new JButton("确认");
        jb2=new JButton("取消");
        
        jtf=new JTextField(10);
        jpwd=new JPasswordField(10);
        
        jl1=new JLabel("用户名:");
        jl2=new JLabel("密    码:");
        
        jp1=new JPanel();
        jp2=new JPanel();
        jp3=new JPanel();
        //设置布局管理器
        this.setLayout(new GridLayout(3,1,5,5));
        //添加组件
        jp1.add(jl1);
        jp1.add(jtf);
        
        jp2.add(jl2); 
        jp2.add(jpwd);
        
        jp3.add(jb1);
        jp3.add(jb2);
        
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        //设置窗体属性
        this.setTitle("登录界面");
        this.setSize(280, 160);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
 
}

运行结果

技术图片

 

计算器界面

代码源

package 计算机界面;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    
    public class jj extends JFrame implements ActionListener {
       
        private final String[] KEYS = { "7", "8", "9", "/", "sqrt", "4", "5", "6",
                "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
        
        private final String[] COMMAND = { "Backspace", "CE", "C" };
        
        private final String[] M = { " ", "MC", "MR", "MS", "M+" };
        
        private JButton keys[] = new JButton[KEYS.length];
       
        private JButton commands[] = new JButton[COMMAND.length];
        
        private JButton m[] = new JButton[M.length];
       
        private JTextField resultText = new JTextField("0");
     
       
        private boolean firstDigit = true;
        
        private double resultNum = 0.0;
        
        private String operator = "=";
        
        private boolean operateValidFlag = true;
     
       
        public jj() {
            super();
            
            init();
            
            this.setBackground(Color.LIGHT_GRAY);
            this.setTitle("计算器");
            
            this.setLocation(500, 300);
            
            this.setResizable(false);
           
            this.pack();
        }
     
       
        private void init() {
            
            resultText.setHorizontalAlignment(JTextField.RIGHT);
           
            resultText.setEditable(false);
            
            resultText.setBackground(Color.WHITE);
     
            
            JPanel calckeysPanel = new JPanel();
            
            calckeysPanel.setLayout(new GridLayout(4, 5, 3, 3));
            for (int i = 0; i < KEYS.length; i++) {
                keys[i] = new JButton(KEYS[i]);
                calckeysPanel.add(keys[i]);
                keys[i].setForeground(Color.blue);
            }
          
            keys[3].setForeground(Color.red);
            keys[8].setForeground(Color.red);
            keys[13].setForeground(Color.red);
            keys[18].setForeground(Color.red);
            keys[19].setForeground(Color.red);
     
            
            JPanel commandsPanel = new JPanel();
            
            commandsPanel.setLayout(new GridLayout(1, 3, 3, 3));
            for (int i = 0; i < COMMAND.length; i++) {
                commands[i] = new JButton(COMMAND[i]);
                commandsPanel.add(commands[i]);
                commands[i].setForeground(Color.red);
            }
     
            
            JPanel calmsPanel = new JPanel();
           
            calmsPanel.setLayout(new GridLayout(5, 1, 3, 3));
            for (int i = 0; i < M.length; i++) {
                m[i] = new JButton(M[i]);
                calmsPanel.add(m[i]);
                m[i].setForeground(Color.red);
            }
     
            
     
            JPanel panel1 = new JPanel();
            
            panel1.setLayout(new BorderLayout(3, 3));
            panel1.add("North", commandsPanel);
            panel1.add("Center", calckeysPanel);
     
           
            JPanel top = new JPanel();
            top.setLayout(new BorderLayout());
            top.add("Center", resultText);
     
            
            getContentPane().setLayout(new BorderLayout(3, 5));
            getContentPane().add("North", top);
            getContentPane().add("Center", panel1);
            getContentPane().add("West", calmsPanel);
            
            for (int i = 0; i < KEYS.length; i++) {
                keys[i].addActionListener(this);
            }
            for (int i = 0; i < COMMAND.length; i++) {
                commands[i].addActionListener(this);
            }
            for (int i = 0; i < M.length; i++) {
                m[i].addActionListener(this);
            }
        }
     
       
        public void actionPerformed(ActionEvent e) {
            
            String label = e.getActionCommand();
            if (label.equals(COMMAND[0])) {
               
                handleBackspace();
            } else if (label.equals(COMMAND[1])) {
                
                resultText.setText("0");
            } else if (label.equals(COMMAND[2])) {
                
                handleC();
            } else if ("0123456789.".indexOf(label) >= 0) {
                
                handleNumber(label);
               
            } else {
                handleOperator(label);
            }
        }
     
        
        private void handleBackspace() {
            String text = resultText.getText();
            int i = text.length();
            if (i > 0) {
                
                text = text.substring(0, i - 1);
                if (text.length() == 0) {
                    
                    resultText.setText("0");
                    firstDigit = true;
                    operator = "=";
                } else {
                   
                    resultText.setText(text);
                }
            }
        }
     
        /**
         * 处理数字键被按下的事件
         *
         * @param key
         */
        private void handleNumber(String key) {
            if (firstDigit) {
                // 输入的第一个数字
                resultText.setText(key);
            } else if ((key.equals(".")) && (resultText.getText().indexOf(".") < 0)) {
                // 输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
                resultText.setText(resultText.getText() + ".");
            } else if (!key.equals(".")) {
                // 如果输入的不是小数点,则将数字附在结果文本框的后面
                resultText.setText(resultText.getText() + key);
            }
            // 以后输入的肯定不是第一个数字了
            firstDigit = false;
        }
     
        /**
         * 处理C键被按下的事件
         */
        private void handleC() {
            // 初始化计算器的各种值
            resultText.setText("0");
            firstDigit = true;
            operator = "=";
        }
     
        /**
         * 处理运算符键被按下的事件
         *
         * @param key
         */
        private void handleOperator(String key) {
            if (operator.equals("/")) {
                // 除法运算
                // 如果当前结果文本框中的值等于0
                if (getNumberFromText() == 0.0) {
                    // 操作不合法
                    operateValidFlag = false;
                    resultText.setText("除数不能为零");
                } else {
                    resultNum /= getNumberFromText();
                }
            } else if (operator.equals("1/x")) {
                // 倒数运算
                if (resultNum == 0.0) {
                    // 操作不合法
                    operateValidFlag = false;
                    resultText.setText("零没有倒数");
                } else {
                    resultNum = 1 / resultNum;
                }
            } else if (operator.equals("+")) {
                // 加法运算
                resultNum += getNumberFromText();
            } else if (operator.equals("-")) {
                // 减法运算
                resultNum -= getNumberFromText();
            } else if (operator.equals("*")) {
                // 乘法运算
                resultNum *= getNumberFromText();
            } else if (operator.equals("sqrt")) {
                // 平方根运算
                resultNum = Math.sqrt(resultNum);
            } else if (operator.equals("%")) {
                // 百分号运算,除以100
                resultNum = resultNum / 100;
            } else if (operator.equals("+/-")) {
                // 正数负数运算
                resultNum = resultNum * (-1);
            } else if (operator.equals("=")) {
                // 赋值运算
                resultNum = getNumberFromText();
            }
            if (operateValidFlag) {
                // 双精度浮点数的运算
                long t1;
                double t2;
                t1 = (long) resultNum;
                t2 = resultNum - t1;
                if (t2 == 0) {
                    resultText.setText(String.valueOf(t1));
                } else {
                    resultText.setText(String.valueOf(resultNum));
                }
            }
            // 运算符等于用户按的按钮
            operator = key;
            firstDigit = true;
            operateValidFlag = true;
        }
     
        /**
         * 从结果文本框中获取数字
         *
         * @return
         */
        private double getNumberFromText() {
            double result = 0;
            try {
                result = Double.valueOf(resultText.getText()).doubleValue();
            } catch (NumberFormatException e) {
            }
            return result;
        }
     
        public static void main(String args[]) {
           jj calculator1 = new jj();
            calculator1.setVisible(true);
            calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }

 

运行结果

技术图片

学习总结

1.让我知道如何布局管理。

2.有了一个大概的布局思维。

3.对java的了解又了一个新的思维思想

 

以上是关于第十二周的主要内容,如果未能解决你的问题,请参考以下文章

第十二周 设计原则

第十二周进度条

第十二周学习进度表

第十二周学习进度

学习进度第十二周

第十二周(补)