JAVA gui初探

 

今天,終於完成了java的gui一單元的學習,雖然說只是簡單的瞭解,可是也揭開了gui神祕的面紗啊!
首先gui編程的基本思路是:java

  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);
		
	}
}
相關文章
相關標籤/搜索