Java的GUI实现最简单的平法计算器

import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class DemoCalulator {
public static void main(String[] args) {
JFrame frame = new JFrame("calculator");
JPanel panel = new JPanel();
frame.add(panel);
//用户名的输入框
JLabel nameLabel = new JLabel("输入数字");


JTextField nameField = new JTextField("please into num",12);

JButton button = new JButton("平方");

panel.add(nameLabel);

panel.add(nameField);

panel.add(button);

FrameUtil u = new FrameUtil();
u.initFrame(frame, 300, 300);

nameField.addMouseListener(new MouseAdapter() {


@Override
public void mouseClicked(MouseEvent e) {
JTextField field =  (JTextField) e.getSource();
if(field.getText().equals("please into num")){
field.setText("");
}
}
});


button.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e){
long num = Long.parseLong(nameField.getText());
nameField.setText((num*num)+"");
}
  });    


}
}

你可能感兴趣的:(java)