java界面-加减-下拉列表文本框按钮

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class BaseFrame extends JFrame{

	public BaseFrame (String title)
	{
		setLayout(null);
		setTitle(title);
		setSize(800,300);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			System.exit(0);
			}
		});//关闭窗口的匿名类
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	}
	public void showMe(){
		setVisible(true);
	}
	

	

}



import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

public class Calc {

	public static void main(String[] args) {
		JPanel j=new JPanel();
		BaseFrame f=new BaseFrame("hello");
		j.setSize(800,50);
		j.setLayout(new GridLayout(1,5));
		JTextField t1=new JTextField();
		JTextField t2=new JTextField();
		JTextField t3=new JTextField();
		
		JComboBox cb=new JComboBox();
		cb.addItem("+");
		cb.addItem("-");
		cb.addItem("*");
		cb.addItem("/");
		
		JButton btn =new JButton("按钮");
		j.add(t1);
		j.add(cb);
		j.add(t2);
		j.add(btn);
		j.add(t3);
		j.setVisible(true);
		f.add(j);
		f.showMe();

	}

}



效果图

只有界面无法实现实际功能-----下一步实现实际功能 敬请期待




实现加法

import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class Calc {
	public static void main(String[] args) {
		JPanel j=new JPanel();
		BaseFrame f=new BaseFrame("hello");
		j.setSize(800,50);
		j.setLayout(new GridLayout(1,5));
		JTextField t1=new JTextField();
		JTextField t2=new JTextField();
		JTextField t3=new JTextField();
		JComboBox cb=new JComboBox();
		cb.addItem("+");
		cb.addItem("-");
		cb.addItem("*");
		cb.addItem("/");
		JButton btn =new JButton("按钮");
		j.add(t1);
		j.add(cb);
		j.add(t2);
		j.add(btn);
		j.add(t3);
		j.setVisible(true);
		f.add(j);
		f.showMe();
		String s=(String)cb.getSelectedItem();
	    btn.addActionListener(new ActionListener() {	
	    	public void actionPerformed(ActionEvent e) {
				if(s.equals("+")){
				int c=0;   	
				int num1 = Integer.parseInt(t1.getText());
				int num2 = Integer.parseInt(t2.getText());
				c=num1+num2;
				String s1=Integer.toString(c);
				t3.setText(s1);
			    }
			}
		});
	}
}




posted @ 2016-11-15 12:36  孙中明  阅读(235)  评论(0编辑  收藏  举报