JAVA gui初探

今天,终于完成了java的gui一单元的学习,虽说只是简单的了解,但是也揭开了gui神秘的面纱啊!
首先gui编程的基本思路是:

  1. 创建窗体(如JFRAME),往窗体里添加其他container或者component,完成一个基本的布局;
  2. 给component添加事件和事件监听器(关键一步.);note:这里解释下事件和事件监听器,事件也就是点击鼠标,移动鼠标,按回车等等操作就称为事件,事件监听器其实是一个个接口,这个接口里有几个方法,你只要定义一个类实现这个接口,然后override其中的方法就可以实现你希望实现的功能里,这个功能也就是你override的方法。
  3. 可以使用eclipse等集成开发工具来进行可视化的操作,这些工具都是用匿名类来实现的,因此在实现功能时是不可以定义普通的变量的,只能定义final的变量,也就是说一旦赋值了也就不可以再更改了,今天大把时间就是浪费在这上面了,因为想要更改变量,一直报错final变量不可更改。

尚还存在的疑问(之后有时间再试试):如果我不用匿名类来实现功能,而是老老实实地自己定义一个class,然后这个class去implements接口实现用户监听,这样是不是就可以定义一般的变量了呢?
附上今天查阅的几篇博客的连接:
https://blog.csdn.net/yang835248367/article/details/76038703
https://blog.csdn.net/salahg/article/details/7529091
最后贴上代码(实现的是简单的bmi计算器):

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.InputMethodListener;
import java.awt.event.TextListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.awt.Color;

public class BMI_CAL extends JFrame {

	private JPanel contentPane;
	private JTextField txtcm;
	private JTextField txtkg;
	private JTextField textField;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
			        
					BMI_CAL frame = new BMI_CAL();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

    public String calculate_bmi() 
    {
        String user_sex;//define the viable sex;
        double user_height;
        double user_weight;
        double bmi;
        //System.out.println(user_sex);
        user_sex = textField.getText();
        user_height = Double.parseDouble(txtcm.getText());
        user_weight = Double.parseDouble(txtkg.getText());
        bmi = user_weight/user_height/user_height;
        
        BigDecimal bg = new BigDecimal(bmi);
        String result = bg.setScale(2, BigDecimal.ROUND_HALF_UP).toString();
        //System.out.println(result);
        return result;
    }

	/**
	 * Create the frame.
	 */
	public BMI_CAL() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		

		textField = new JTextField();
		textField.setBounds(164, 46, 50, 24);
		contentPane.add(textField);
		textField.setColumns(10);
		
		txtcm = new JTextField();
		txtcm.setBounds(164, 93, 50, 24);
		contentPane.add(txtcm);
		txtcm.setColumns(10);
		
		txtkg = new JTextField();
		txtkg.setBounds(164, 140, 50, 24);
		contentPane.add(txtkg);
		txtkg.setColumns(10);
		

		
		JLabel lblNewLabel = new JLabel("input your sex:");
		lblNewLabel.setBounds(35, 49, 150, 18);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("input your height(m):");
		lblNewLabel_1.setBounds(14, 96, 150, 18);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("input your weight(kg):");
		lblNewLabel_2.setBounds(14, 143, 150, 18);
		contentPane.add(lblNewLabel_2);
		
		textField_1 = new JTextField();
		textField_1.setBackground(Color.WHITE);
		textField_1.setEditable(false);
		textField_1.setBounds(35, 196, 352, 24);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton = new JButton("\u8BA1\u7B97BMI");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
                String bmi = calculate_bmi();
                double jud = Double.parseDouble(bmi);
                String sex = textField.getText();
                //System.out.println(sex);
                if (jud < 18.5){
                    textField_1.setText("you are "+sex+", your bmi is "+bmi+", too light!");
                }
                else if(jud>=18.5 && jud<=23.9){
                    textField_1.setText("you are "+sex+", your bmi is "+bmi+", perfect!");
                }
                else
                    textField_1.setText("you are "+sex+", your bmi is "+bmi+", heavy!");
                
                //System.out.println(bmi);
			}
		});
		btnNewButton.setBounds(257, 104, 113, 27);
		contentPane.add(btnNewButton);
		
	}
}

你可能感兴趣的:(JAVA,JAVA,gui初探,GUI)