星期二—GUI的初步了解

1、GUI:GUI(Graphical User Interface)即图形用户界面,它能够使应用程序看上去更加友好;

2、AWT(Abstract Windows Toolkit)是Java语言中最原始的GUI工具包,相关API位于java.awt包中。AWT是一个非常有限的GUI工具包,比如树、表格等都不支持。

3、在Swing编程中,有一些经常要使用到的组件,其中包括:JFrame(窗体,框架) JPanel(面板,容器) JButton(按钮) JLabel(标签) JTextField(文本框) JTextArea(文本域)

用GUI练习计算器:

package com.chinasoft.gui.PM;

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

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.Font;

import javax.swing.JTextField;
import java.awt.Color;

public class Gui extends JFrame {                       //建立一个框体
	
	private JPanel contentPane;
	private JTextField textField;
	private JButton[] allButtons;
	private JButton clearButton;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		String b = "123456";	                                         //设置一个登录界面,密码为123456
		String a = JOptionPane.showInputDialog("请输出您的密码:");
		if(a.equals(b)){	
	//	JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Gui frame = new Gui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		}else
		{JOptionPane.showMessageDialog(null, "密码输入错误", "alert", JOptionPane.ERROR_MESSAGE);}
	}
	/**
	 * Create the frame.
	 */
	public Gui() {
		setBackground(Color.BLUE);
		Suanfa f = new Suanfa();
		f.suanfa1();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("计算器");
		setBounds(100, 100, 350, 500);
		contentPane = new JPanel();
		contentPane.setBackground(Color.red);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		JButton btnNewButton_3 = new JButton("=");
		btnNewButton_3.setBackground(Color.CYAN);
		btnNewButton_3.setFont(new Font("宋体", Font.PLAIN, 30));            //基本上所有的运算过程都在“=”中实现
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			String str = textField.getText();
			byte []b1 = new byte[str.length()];
			b1 = str.getBytes();
		    int endIndex = str.length();
		    
			   for(int i =0;i <str.length();i++ ){	             //用for循环来遍历数组长度中的特殊符号
				   if(b1[i] == '+'){	                   //用if条件来判断符号,同下面的“-”,“/”,“*”		
					//获取第一个数字
					int index11 = str.indexOf("+");
					String str1 = str.substring(0, index11);					
					int a = Integer.parseInt(str1);
					//获取第二个数字
					String str2 = str.substring(index11+1, endIndex);
					int b = Integer.parseInt(str2);
					//加法计算
					int sum = a + b;	
					
					//输出结果
					textField.setText(Integer.toString(sum));
				   }else
				   if(b1[i] == '-'){
					int index33 = str.indexOf("-");
					String str3 = str.substring(0, index33);					
					int c = Integer.parseInt(str3);
					
					String str4 = str.substring(index33+1, endIndex);
					int d = Integer.parseInt(str4);
					
					int div = c - d;	
				
					
					textField.setText(Integer.toString(div));
			
				   }else
				   if(b1[i] == '*'){
					int index44 = str.indexOf("*");
					String str11 = str.substring(0, index44);					
					int c1 = Integer.parseInt(str11);	
					String str12 = str.substring(index44+1, endIndex);
					int d1 = Integer.parseInt(str12);				
					int cheng = c1 * d1;	
					textField.setText(Integer.toString(cheng));

				   }else
					if(b1[i] == '/'){
					int index5 = str.indexOf("/");
					String str13 = str.substring(0, index5);					
					int c2 = Integer.parseInt(str13);
					String str14 = str.substring(index5+1, endIndex);
					int d2 = Integer.parseInt(str14);				
					int chu = c2 / d2;				
					textField.setText(Integer.toString(chu));
				   }
				}	   
			}
		});		
		JButton btnNewButton = new JButton("0");   //把btnNewButton按钮在文本框中输出  同下其他按钮
		btnNewButton.setBackground(Color.CYAN);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText(textField.getText() + "0");
			}
		});		
		JButton btnNewButton_1 = new JButton("+");
		btnNewButton_1.setBackground(Color.CYAN);
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 30));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			textField.setText(textField.getText() + "+");				
			}
		});
		JButton button = new JButton(".");
		button.setBackground(Color.CYAN);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
	
			textField.setText(textField.getText() + ".");
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_1 = new JButton("-");
		button_1.setBackground(Color.CYAN);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText(textField.getText() + "-");
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 25));		 
		JButton button_2 = new JButton("3");
		button_2.setBackground(Color.CYAN);
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
		  textField.setText(textField.getText() + "3");
			}
		});
		button_2.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_3 = new JButton("2");
		button_3.setBackground(Color.CYAN);
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
		 textField.setText(textField.getText() + "2");
			}
		});
		button_3.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_4 = new JButton("1");
		button_4.setBackground(Color.CYAN);
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		 textField.setText(textField.getText() + "1");
			}
		});
		button_4.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_5 = new JButton("4");
		button_5.setBackground(Color.CYAN);
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		textField.setText(textField.getText() + "4");
			}
		});
		button_5.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_6 = new JButton("5");
		button_6.setBackground(Color.CYAN);
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		 textField.setText(textField.getText() + "5");
			}
		});
		button_6.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_7 = new JButton("6");
		button_7.setBackground(Color.CYAN);
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			textField.setText(textField.getText() + "6");
			}
		});
		button_7.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_8 = new JButton("*");
		button_8.setBackground(Color.CYAN);
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText(textField.getText() + "*");
			}
		});
		button_8.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_9 = new JButton("1/x");
		button_9.setBackground(Color.CYAN);
		button_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		button_9.setFont(new Font("宋体", Font.PLAIN, 7));
		JButton button_10 = new JButton("7");
		button_10.setBackground(Color.CYAN);
		button_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			textField.setText(textField.getText() + "7");
			}
		});
		button_10.setFont(new Font("宋体", Font.PLAIN, 25));
		 
		JButton button_11 = new JButton("8");
		button_11.setBackground(Color.CYAN);
		button_11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			textField.setText(textField.getText() + "8");
			}
		});
		button_11.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_12 = new JButton("9");
		button_12.setBackground(Color.CYAN);
		button_12.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			textField.setText(textField.getText() + "9");
			}
		});
		button_12.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_14 = new JButton("%");
		button_14.setBackground(Color.CYAN);
		button_14.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_13 = new JButton("/");
		button_13.setBackground(Color.CYAN);
		button_13.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			textField.setText(textField.getText() + "/");
			}
		});
		button_13.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_15 = new JButton("\u2190");          //清除按钮
		button_15.setBackground(Color.CYAN);
		button_15.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str = textField.getText();
				int a = str.length();
				if(a == 0){	
		JOptionPane.showMessageDialog(null, "没了,别逗了", "alert", JOptionPane.ERROR_MESSAGE);	
				}else{
			   String str1 = str.substring(0, a-1);
			 //  String str2 = str.replace(str, str1);
				textField.setText(str1);	
				}	
			}
		});       
		button_15.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JButton btnCe = new JButton("CE");
		btnCe.setBackground(Color.CYAN);
		btnCe.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JButton btnC = new JButton("C");    //把文本框中内容清空
		btnC.setBackground(Color.CYAN);
		btnC.addActionListener(new ActionListener() {
	   public void actionPerformed(ActionEvent e) {
		   textField.setText("");
			}
		});
		btnC.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JButton button_18 = new JButton("\u00B1");
		button_18.setBackground(Color.CYAN);
		button_18.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JButton button_19 = new JButton("\u221A");
		button_19.setBackground(Color.CYAN);
		button_19.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JButton btnMc = new JButton("MC");
		btnMc.setBackground(Color.CYAN);
		btnMc.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JButton btnMr = new JButton("MR");
		btnMr.setBackground(Color.CYAN);
		btnMr.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JButton btnM = new JButton("MS");
		btnM.setBackground(Color.CYAN);
		btnM.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JButton btnM_1 = new JButton("M+");
		btnM_1.setBackground(Color.CYAN);
		btnM_1.setFont(new Font("宋体", Font.PLAIN, 12));
		
		JButton btnM_2 = new JButton("M-");
		btnM_2.setBackground(Color.CYAN);
		btnM_2.setFont(new Font("宋体", Font.PLAIN, 12));
		
		textField = new JTextField();
		textField.setBackground(Color.BLUE);
		textField.setForeground(Color.MAGENTA);
		textField.setFont(new Font("宋体", Font.PLAIN, 30));
		textField.setColumns(10);
	
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(26)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, 259, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
									.addGap(13)
									.addComponent(button, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnNewButton_1))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
										.addComponent(button_5, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
										.addComponent(button_10, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
										.addComponent(button_15, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
										.addComponent(btnMc, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(btnMr, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
										.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
											.addGroup(gl_contentPane.createSequentialGroup()
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
													.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
													.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
													.addGroup(gl_contentPane.createSequentialGroup()
														.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED)
														.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
													.addGroup(gl_contentPane.createSequentialGroup()
														.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED)
														.addComponent(button_8, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))))
											.addGroup(gl_contentPane.createSequentialGroup()
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
													.addComponent(button_11, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
													.addComponent(btnCe, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
												.addPreferredGap(ComponentPlacement.UNRELATED)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
													.addGroup(gl_contentPane.createSequentialGroup()
														.addComponent(btnC, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED)
														.addComponent(button_18, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
													.addGroup(gl_contentPane.createSequentialGroup()
														.addComponent(button_12, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addComponent(button_13, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))
													.addGroup(gl_contentPane.createSequentialGroup()
														.addComponent(btnM, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
														.addPreferredGap(ComponentPlacement.RELATED)
														.addComponent(btnM_1, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))))))))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(btnNewButton_3, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE)
								.addComponent(button_9, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
								.addComponent(button_14, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
								.addComponent(button_19, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
								.addComponent(btnM_2, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(23, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(23)
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
					.addGap(28)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(btnMc, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnMr, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnM, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnM_1, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnM_2, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(button_15, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnCe, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnC, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_18, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_19, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(button_10, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_11, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_14, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(button_13, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
							.addComponent(button_12, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(button_5, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_6, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_7, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_8, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(button_9, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
								.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
									.addComponent(button_3, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
									.addComponent(button_4, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)))
							.addGap(8)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
									.addComponent(button, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
									.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))))
						.addComponent(btnNewButton_3, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}

	private void clearButton() {
		// TODO Auto-generated method stub
		
	}
}

 

 

posted @ 2017-07-12 18:45  王。雄  阅读(253)  评论(0编辑  收藏  举报