java圖形介面設定求商 求差
/*
2 編寫一個Java Application圖形介面的程式,實現兩數相減、相除的功能
(1) 在兩個文字框裡分別輸入兩個數字,單擊“求差”、“求商”按鈕,則在第三個文字框裡顯示運算結果;
(2) 當輸入的不是數字或者除數為0時,要求用訊息對話方塊向用戶報錯;
(3) 單擊視窗右上角的“X”按鈕,即關閉視窗退出
*/
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher; //Mather是一個正則表示式介面卡
import java.util.regex.Pattern; // 通過Pattern的Matcher方法來返回一個Matcher物件。
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class NumTest {
private JFrame frame; //窗體
private JTextField num1; //第一個數
private JTextField num2; //第二個數
private JTextField sum;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() { //異常處理
try {
NumTest window = new NumTest();
window.frame.setVisible(true); //輸出異常
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public NumTest() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame(); //窗體的建立
frame.setTitle("數學計算"); //窗體名稱
frame.setBounds(100, 100, 298, 177);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
num1 = new JTextField();
num1.setBounds(10, 37, 120, 21);
frame.getContentPane().add(num1);
//num1.setColumns(10);
num2 = new JTextField();
num2.setBounds(152, 37, 120, 21);
frame.getContentPane().add(num2);
//num2.setColumns(10);
JLabel lblNewLabel = new JLabel("請輸入兩個數字");
lblNewLabel.setBounds(10, 12, 100, 15);
frame.getContentPane().add(lblNewLabel);
JButton btn1 = new JButton("求差");
btn1.setBounds(10, 68, 120, 23);
btn1.addActionListener(listener);
frame.getContentPane().add(btn1);
JButton btn2 = new JButton("求商");
btn2.setBounds(152, 68, 120, 23);
btn2.addActionListener(listener);
frame.getContentPane().add(btn2);
JLabel label = new JLabel("結果:");
label.setBounds(42, 104, 42, 15);
frame.getContentPane().add(label);
sum = new JTextField();
sum.setBounds(115, 101, 140, 21);
frame.getContentPane().add(sum);
sum.setColumns(10);
}
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String numstr1 = num1.getText().trim(); //獲取第一個數值
String numstr2 = num2.getText().trim(); //獲取第二個數值
if (!isNumeric(numstr1)) { //判斷第一個數
JOptionPane.showMessageDialog(frame.getContentPane(), "第一個數字輸入不規範!", "系統資訊", JOptionPane.ERROR_MESSAGE); //訊息對話方塊
num1.setText("");
return;
}
if (!isNumeric(numstr2)) { //判斷第二個數
JOptionPane.showMessageDialog(frame.getContentPane(), "第二個數字輸入不規範!", "系統資訊", JOptionPane.ERROR_MESSAGE); //訊息對話方塊
num2.setText("");
return;
}
float f1 = Float.parseFloat(numstr1);
float f2 = Float.parseFloat(numstr2);
switch (e.getActionCommand()) {
case "求商":
sum.setText(f1 / f2 + "");
break;
case "求差":
sum.setText(f1 - f2 + "");
break;
default:
break;
}
}
};
public boolean isNumeric(String str) { //判斷輸入內容
Pattern pattern = Pattern.compile("[0-9]{1,}"); // pattern是一個編譯好的正則表示式
Matcher isNum = pattern.matcher(str); //Mather是一個正則表示式介面卡 通過Pattern的Matcher方法來返回一個Matcher物件。
if (!isNum.matches()) { //matcher是將整個輸入序列拿去匹配
return false;
}
return true;
}
}
第二種做法
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Text2 extends JFrame
{
TextField t1=new TextField(20);
TextField t2=new TextField();
TextField t3=new TextField();
JButton btn1=new JButton("求差");
JButton btn2=new JButton("求商");
Label l1=new Label("請輸入第一個數:");
Label l2=new Label("請輸入第二個數:");
Label l3=new Label();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
Text2()
{
setTitle("計算器");
btn1.setSize(100,50);
btn1.setBackground(Color.red);
btn2.setBackground(Color.green);
l1.setVisible(true);
btn1.addActionListener(new A());
btn2.addActionListener(new B());
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
t1.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
public void focusLost(FocusEvent e) {
String str=t1.getText().trim();
if(!(t1.getText().length()==0))
{
if(!str.matches("[1-9][0-9]*"))
{
JOptionPane.showMessageDialog(null, "輸入非數字,請重新輸入!");
t1.setText("");
}
}
}
});
t2.addFocusListener(new FocusListener()
{
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
public void focusLost(FocusEvent e) {
String str=t1.getText().trim();
if(!(t2.getText().length()==0))
{
if(!str.matches("[1-9][0-9]*"))
{
JOptionPane.showMessageDialog(null, "輸入非數字,請重新輸入!");
t1.setText("");
}
}
}
});
/*t1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
if (!(c >= '0' && c <= '9')) {
JOptionPane.showMessageDialog(null, "輸入非數字,請重新輸入!", "請輸入數字", JOptionPane.ERROR_MESSAGE);
}
}
});
t2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
if (!(c >= '0' && c <= '9')) {
JOptionPane.showMessageDialog(null, "輸入非數字,請重新輸入!", "請輸入數字", JOptionPane.ERROR_MESSAGE);
}
}
});*/
setLayout(new GridLayout(2,1));
p1.setLayout(new GridLayout(3,2));
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(l3);
p1.add(t3);
p2.add(btn1);
p2.add(btn2);
add(p1);
add(p2);
setSize(400,400);
setVisible(true);
}
public static void main(String []args)
{
new Text2();
}
class A implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(!(t2.getText().length()==0)&&!(t1.getText().length()==0))
{
String b=t1.getText();
String c=t2.getText();
int a=Integer.parseInt(b)-Integer.parseInt(c);
t3.setText("a-b = "+a);
l3.setText("兩數之差為:");
t1.setText("\0");
t2.setText("\0");
}
else
JOptionPane.showMessageDialog(null, "請輸入資料!");
}
}
class B implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(!(t2.getText().length()==0)&&!(t1.getText().length()==0))
{
String b=t1.getText();
String c=t2.getText();
int a=Integer.parseInt(b)/Integer.parseInt(c);
t3.setText("a/b = "+a);
l3.setText("兩數之商為:");
t1.setText("\0");
t2.setText("\0");
}
else
JOptionPane.showMessageDialog(null, "請輸入資料!");
}
}
}