(1).新建一个java项目
(2).在新建的项目的包资源管理器中打开src,单击右键选择新建————》
选择其他————》WindowBuilder————》Swing Designer————》
JFrame————》点击下一步,输入名称————》点击完成。
(3)至此,完成了界面的搭建,接下来可以放置控件,进行后续步骤
变量名.getText();
变量名.setText("字符串");
String.valueOf(double型数据);//这是double型转为String类型
1.放置10个按钮,分别为数字0~9,并为每个按钮设置监听器
代码如下:
package Hello;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.JTextPane;
public class LoginFrame extends JFrame {
private JPanel contentPane;
private JTextField userNameC;
private JPasswordField passwordC;
private double num1=0;//要操作的第一个数字
private double num2=0;//要操作的第二个数字
private double count=0;//当有小数时,计数是第几位小数
private int dot=0;//监测是否输入了小数
private double result;//连个数操作的结果
private int sel=0;//选择器,选择进行了+、-、*、/、^中的那个操作,为0时表示还在输入第一位数
private boolean jisuan=false;//监测是否开始输入第二个数
private JTextField display;//定义显示窗口控件变量名为:display
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrame frame = new LoginFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public double zhishu(double x,double y){//正整数的指数次方
if(y>0){
return x*zhishu(x,y-1);
}else if(y<0){
return (x*zhishu(x,-y-1));
}else{
return 1;
}
}
public double fuzhishu(double x,double y){//负指数的指数次方
double i=zhishu(x,y);
return 1/i;
}
public double x_n(double x,double y){//x^n计算
if(y>0){
return zhishu(x,y);
}else if(y<0){
return fuzhishu(x,y);
}else{
return 1;
}
}
public double jiecheng(double x){//计算阶乘
if(x>1){
return x*jiecheng(x-1);
}
else
return 1;
}
public double deal_num1(double y){//对第一位操作数进行处理
if(dot==0)
num1=num1*10+y;
else{
count++;
num1=num1+y/zhishu(10,count);
}
return 1;
}
public double deal_num2(double y){//对第二位数进行操作
if(jisuan==true){
if(dot==0)
num2=num2*10+y;
else{
count++;
num2=num2+y/zhishu(10,count);
}
}
return 1;
}
/**
* Create the frame.
*/
public LoginFrame() {
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);
JLabel lblNewLabel = new JLabel("用户名");
lblNewLabel.setBounds(10, 10, 54, 15);
contentPane.add(lblNewLabel);
userNameC = new JTextField();
userNameC.setBounds(52, 7, 94, 21);
contentPane.add(userNameC);
userNameC.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("密 码");
lblNewLabel_1.setBounds(10, 38, 54, 15);
contentPane.add(lblNewLabel_1);
passwordC = new JPasswordField();
passwordC.setBounds(52, 35, 94, 21);
contentPane.add(passwordC);
JButton btnNewButton = new JButton("\u767B\u9646");
btnNewButton.setBounds(20, 72, 64, 23);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("单击了登陆按钮");
System.out.println("输入的用户名是:"+userNameC.getText());
System.out.println("输入的密码是:"+passwordC.getText());
}
});
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("\u9000\u51FA");
btnNewButton_1.setBounds(94, 72, 74, 23);
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("单击了退出按钮");
System.out.println(userNameC.getText()+"成功退出了登陆!");
}
});
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("1");//数字“1”按钮
btnNewButton_2.setBounds(185, 6, 64, 23);
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(1);//处理第二位数
switch (sel){
case 0:deal_num1(1);//处理第一位数
System.out.println(num1);
display.setText(String.valueOf(num1));
break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(btnNewButton_2);
JButton button = new JButton("2");//数字“2”按钮
button.setBounds(259, 6, 64, 23);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(2);
switch (sel){
case 0:
deal_num1(2);
display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button);
JButton button_1 = new JButton("3");//数字“3”按钮
button_1.setBounds(332, 6, 64, 23);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(3);
switch (sel){
case 0: deal_num1(3);
display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button_1);
JButton button_2 = new JButton("4");//数字“4”按钮
button_2.setBounds(185, 46, 64, 23);
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(4);
switch (sel){
case 0:deal_num1(4);
display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button_2);
JButton button_3 = new JButton("5");//数字“5”按钮
button_3.setBounds(259, 46, 64, 23);
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(5);
switch (sel){
case 0:
deal_num1(5);
display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button_3);
JButton button_4 = new JButton("6");//数字“6”按钮
button_4.setBounds(332, 46, 64, 23);
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(6);
switch (sel){
case 0:
deal_num1(6);
display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button_4);
JButton button_5 = new JButton("7");//数字“7”按钮
button_5.setBounds(185, 86, 64, 23);
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(7);
switch (sel){
case 0:
deal_num1(7); display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button_5);
JButton button_6 = new JButton("8");//数字“8”按钮
button_6.setBounds(259, 86, 64, 23);
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(8);
switch (sel){
case 0:
deal_num1(8);
display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button_6);
JButton button_7 = new JButton("9");//数字“9”按钮
button_7.setBounds(332, 86, 64, 23);
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(9);
switch (sel){
case 0:
deal_num1(9);
display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button_7);
JButton button_8 = new JButton("0");//“0”数字按钮
button_8.setBounds(185, 119, 64, 23);
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deal_num2(0);
switch (sel){
case 0:
deal_num1(0);
display.setText(String.valueOf(num1));break;
case 1:display.setText(String.valueOf(num1+"+"+num2));break;
case 2:display.setText(String.valueOf(num1+"-"+num2));break;
case 3:display.setText(String.valueOf(num1+"*"+num2));break;
case 4:display.setText(String.valueOf(num1+"/"+num2));break;
case 5:display.setText(String.valueOf(num1+"^"+num2));break;
}
}
});
contentPane.add(button_8);
JButton button_9 = new JButton("+");//加法按钮
button_9.setBounds(332, 119, 64, 23);
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.setText(num1+"+");
sel=1;
dot=0;
jisuan=true;
count=0;
}
});
contentPane.add(button_9);
JButton button_10 = new JButton("-");//减法按钮
button_10.setBounds(332, 152, 64, 23);
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.setText(num1+"-");
sel=2;dot=0;jisuan=true;
count=0;
}
});
contentPane.add(button_10);
JButton button_11 = new JButton("=");//等于结果按钮
button_11.setBounds(185, 152, 64, 42);
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switch (sel){
case 0:display.setText(String.valueOf(result));break;
case 1:result=num1+num2;display.setText(String.valueOf(num1+"+"+num2+"="+result));break;
case 2:result=num1-num2;display.setText(String.valueOf(num1+"-"+num2+"="+result));break;
case 3:result=num1*num2;display.setText(String.valueOf(num1+"*"+num2+"="+result));break;
case 4:result=(double)num1/num2;display.setText(String.valueOf(num1+"/"+num2+"="+result));break;
case 5:result=x_n(num1,num2);display.setText(String.valueOf(num1+"^"+num2+"="+result));break;
}
num1=num2=0;
sel=0;
dot=0;jisuan=false;count=0;
}
});
contentPane.add(button_11);
JButton button_12 = new JButton("/");//除法按钮
button_12.setBounds(332, 218, 64, 23);
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.setText(num1+"/");
sel=4;
dot=0;jisuan=true;
count=0;
}
});
contentPane.add(button_12);
JButton button_13 = new JButton("*");//乘法按钮
button_13.setBounds(332, 185, 64, 23);
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.setText(num1+"*");
sel=3;
dot=0;
jisuan=true;
count=0;
}
});
contentPane.add(button_13);
JButton btnNewButton_3 = new JButton("C");//清空显示窗口按钮
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
display.setText("");
num1=num2=0;
jisuan=false;
count=0;
dot=0;
}
});
btnNewButton_3.setBounds(185, 204, 64, 36);
contentPane.add(btnNewButton_3);
display = new JTextField();//显示输入、输出和结果的控件
display.setBounds(10, 115, 158, 51);
contentPane.add(display);
display.setColumns(10);
JButton sqrtC = new JButton("sqrt");//开平方操作
sqrtC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
result=Math.sqrt(num1);
display.setText(String.valueOf("sqrt("+num1+")"+"="+result));
num1=0;
count=0;
dot=0;
}
});
sqrtC.setBounds(259, 152, 64, 23);
contentPane.add(sqrtC);
JButton btnNewButton_5 = new JButton("x^n");//任意次方操作
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
display.setText(num1+"^");
sel=5;
dot=0;
count=0;
jisuan=true;
}
});
btnNewButton_5.setBounds(259, 182, 64, 23);
contentPane.add(btnNewButton_5);
JButton btnNewButton_4 = new JButton(".");//小数点
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dot=1;
}
});
btnNewButton_4.setBounds(259, 119, 64, 23);
contentPane.add(btnNewButton_4);
JButton btnNewButton_6 = new JButton("!");//阶乘运算
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
result=jiecheng(num1);
display.setText(String.valueOf(num1+"!"+"="+result));
num1=0;
dot=0;
count=0;
}
});
btnNewButton_6.setBounds(259, 218, 64, 23);
contentPane.add(btnNewButton_6);
}
}