JAVA程序问题
程序要求:编写一个JAVA程序,在程序中建立一个窗口,窗口中有四个按钮,分别为加减乘除:窗口上还有三个文本框,单击任一按钮,将两个基本点文本框的数字进行相应大运算。在第三...
程序要求:编写一个JAVA程序,在程序中建立一个窗口,窗口中有四个按钮,分别为加减乘除:窗口上还有三个文本框,单击任一按钮,将两个基本点文本框的数字进行相应大运算。在第三个文本档中显示结果。
展开
2013-09-11
展开全部
希望这次别给我把换行弄没了....
import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;
import javax.swing.GroupLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;
public class Calc extends JFrame {
private JTextField jtfData1; private JTextField jtfData2; private JTextField jtfResult; public Calc() { init(); setSize(500, 130); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void init() { JLabel lblData1 = new JLabel("数据1"); jtfData1 = new JTextField(); JLabel lblData2 = new JLabel("数据2"); jtfData2 = new JTextField(); JLabel lblResult = new JLabel("结果"); jtfResult = new JTextField(); JPanel pnlJTF = new JPanel(); GroupLayout layout = new GroupLayout(pnlJTF); pnlJTF.setLayout(layout); GroupLayout.ParallelGroup vGroup = layout.createParallelGroup(GroupLayout.Alignment.CENTER); layout.setAutoCreateContainerGaps(true); vGroup.addComponent(lblData1).addComponent(jtfData1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE) .addComponent(lblData2).addComponent(jtfData2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE) .addComponent(lblResult).addComponent(jtfResult, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE); layout.setVerticalGroup(vGroup); GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); hGroup.addComponent(lblData1); hGroup.addComponent(jtfData1); hGroup.addComponent(lblData2); hGroup.addComponent(jtfData2); hGroup.addComponent(lblResult); hGroup.addComponent(jtfResult); layout.setHorizontalGroup(hGroup); JButton btnAdd = new JButton("加"); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calc(1); } }); JButton btnJian = new JButton("减"); btnJian.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calc(2); } }); JButton btnMul = new JButton("乘"); btnMul.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calc(3); } }); JButton btnDiv = new JButton("除"); btnDiv.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calc(4); } }); JPanel pnlBtn = new JPanel(new FlowLayout()); pnlBtn.add(btnAdd); pnlBtn.add(btnJian); pnlBtn.add(btnMul); pnlBtn.add(btnDiv); setLayout(new BorderLayout()); add(pnlJTF, BorderLayout.CENTER); add(pnlBtn, BorderLayout.SOUTH); } public static void main(String [] args) { new Calc().setVisible(true); } private void calc(int op) { double d = 0; String data1 = jtfData1.getText(); String data2 = jtfData2.getText(); double d1 = 0; double d2 = 0; if (data1 == null || data1.equals("") || data2 == null || data2.equals("")) { JOptionPane.showMessageDialog(null, "请输入数据"); return; } else { try { d1 = Double.parseDouble(data1); d2 = Double.parseDouble(data2); } catch (Exception e) { JOptionPane.showMessageDialog(null, "请输入数字"); return; } } switch (op) { case 1: d = d1 + d2; break; case 2: d = d1 - d2; break; case 3: d = d1 * d2; break; case 4: if (d2 != 0) d = d1 / d2; else JOptionPane.showMessageDialog(null, "除数不能为0"); return; } jtfResult.setText(String.valueOf(d)); }}
import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;
import javax.swing.GroupLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;
public class Calc extends JFrame {
private JTextField jtfData1; private JTextField jtfData2; private JTextField jtfResult; public Calc() { init(); setSize(500, 130); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void init() { JLabel lblData1 = new JLabel("数据1"); jtfData1 = new JTextField(); JLabel lblData2 = new JLabel("数据2"); jtfData2 = new JTextField(); JLabel lblResult = new JLabel("结果"); jtfResult = new JTextField(); JPanel pnlJTF = new JPanel(); GroupLayout layout = new GroupLayout(pnlJTF); pnlJTF.setLayout(layout); GroupLayout.ParallelGroup vGroup = layout.createParallelGroup(GroupLayout.Alignment.CENTER); layout.setAutoCreateContainerGaps(true); vGroup.addComponent(lblData1).addComponent(jtfData1, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE) .addComponent(lblData2).addComponent(jtfData2, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE) .addComponent(lblResult).addComponent(jtfResult, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE); layout.setVerticalGroup(vGroup); GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); hGroup.addComponent(lblData1); hGroup.addComponent(jtfData1); hGroup.addComponent(lblData2); hGroup.addComponent(jtfData2); hGroup.addComponent(lblResult); hGroup.addComponent(jtfResult); layout.setHorizontalGroup(hGroup); JButton btnAdd = new JButton("加"); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calc(1); } }); JButton btnJian = new JButton("减"); btnJian.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calc(2); } }); JButton btnMul = new JButton("乘"); btnMul.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calc(3); } }); JButton btnDiv = new JButton("除"); btnDiv.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calc(4); } }); JPanel pnlBtn = new JPanel(new FlowLayout()); pnlBtn.add(btnAdd); pnlBtn.add(btnJian); pnlBtn.add(btnMul); pnlBtn.add(btnDiv); setLayout(new BorderLayout()); add(pnlJTF, BorderLayout.CENTER); add(pnlBtn, BorderLayout.SOUTH); } public static void main(String [] args) { new Calc().setVisible(true); } private void calc(int op) { double d = 0; String data1 = jtfData1.getText(); String data2 = jtfData2.getText(); double d1 = 0; double d2 = 0; if (data1 == null || data1.equals("") || data2 == null || data2.equals("")) { JOptionPane.showMessageDialog(null, "请输入数据"); return; } else { try { d1 = Double.parseDouble(data1); d2 = Double.parseDouble(data2); } catch (Exception e) { JOptionPane.showMessageDialog(null, "请输入数字"); return; } } switch (op) { case 1: d = d1 + d2; break; case 2: d = d1 - d2; break; case 3: d = d1 * d2; break; case 4: if (d2 != 0) d = d1 / d2; else JOptionPane.showMessageDialog(null, "除数不能为0"); return; } jtfResult.setText(String.valueOf(d)); }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-11
展开全部
随便手写了一个超级简单的,健壮性,输入异常等 你自己写吧,判断输入的是不是数字!import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{ private JTextField text1,text2,text3; private JButton plusBtn,minusBtn,mutiplyBtn,dividBtn; private JPanel panel1,panel2; public Calculator() { text1 = new JTextField(); text1.setPreferredSize(new Dimension(80,20)); text2 = new JTextField(); text2.setPreferredSize(new Dimension(80,20)); text3 = new JTextField(); text3.setPreferredSize(new Dimension(80,20)); plusBtn = new JButton("+"); minusBtn = new JButton("-"); mutiplyBtn = new JButton("*"); dividBtn = new JButton("/"); panel1 =new JPanel(); panel2 =new JPanel(); panel1.add(text1); panel1.add(text2); panel1.add(text3); panel2.setLayout(new GridLayout(2,2)); panel2.add(plusBtn); panel2.add(minusBtn); panel2.add(mutiplyBtn); panel2.add(dividBtn); add(panel1,BorderLayout.NORTH); add(panel2,BorderLayout.CENTER); plusBtn.addActionListener(this); minusBtn.addActionListener(this); mutiplyBtn.addActionListener(this); dividBtn.addActionListener(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(500, 350);
} @Override public void actionPerformed(ActionEvent e) { String str1; String str2; double result; if(e.getSource() == plusBtn) { str1 = text1.getText(); str2 = text2.getText(); if((!str1.trim().equals("")) && (!str2.trim().equals(""))){ result = Double.parseDouble(str1) + Double.parseDouble(str2); text3.setText(String.valueOf(result)); } } if(e.getSource() == minusBtn) { str1 = text1.getText(); str2 = text2.getText(); if((!str1.trim().equals("")) && (!str2.trim().equals(""))){ result = Double.parseDouble(str1) - Double.parseDouble(str2); text3.setText(String.valueOf(result)); } } if(e.getSource() == mutiplyBtn) { str1 = text1.getText(); str2 = text2.getText(); if((!str1.trim().equals("")) && (!str2.trim().equals(""))){ result = Double.parseDouble(str1) * Double.parseDouble(str2); text3.setText(String.valueOf(result)); } } if(e.getSource() == dividBtn) { str1 = text1.getText(); str2 = text2.getText(); if((!str1.trim().equals("")) && (!str2.trim().equals(""))){ result = Double.parseDouble(str1) / Double.parseDouble(str2); text3.setText(String.valueOf(result)); } } } public static void main(String[] args) { Calculator c = new Calculator(); c.pack(); c.setVisible(true); }}
public class Calculator extends JFrame implements ActionListener{ private JTextField text1,text2,text3; private JButton plusBtn,minusBtn,mutiplyBtn,dividBtn; private JPanel panel1,panel2; public Calculator() { text1 = new JTextField(); text1.setPreferredSize(new Dimension(80,20)); text2 = new JTextField(); text2.setPreferredSize(new Dimension(80,20)); text3 = new JTextField(); text3.setPreferredSize(new Dimension(80,20)); plusBtn = new JButton("+"); minusBtn = new JButton("-"); mutiplyBtn = new JButton("*"); dividBtn = new JButton("/"); panel1 =new JPanel(); panel2 =new JPanel(); panel1.add(text1); panel1.add(text2); panel1.add(text3); panel2.setLayout(new GridLayout(2,2)); panel2.add(plusBtn); panel2.add(minusBtn); panel2.add(mutiplyBtn); panel2.add(dividBtn); add(panel1,BorderLayout.NORTH); add(panel2,BorderLayout.CENTER); plusBtn.addActionListener(this); minusBtn.addActionListener(this); mutiplyBtn.addActionListener(this); dividBtn.addActionListener(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(500, 350);
} @Override public void actionPerformed(ActionEvent e) { String str1; String str2; double result; if(e.getSource() == plusBtn) { str1 = text1.getText(); str2 = text2.getText(); if((!str1.trim().equals("")) && (!str2.trim().equals(""))){ result = Double.parseDouble(str1) + Double.parseDouble(str2); text3.setText(String.valueOf(result)); } } if(e.getSource() == minusBtn) { str1 = text1.getText(); str2 = text2.getText(); if((!str1.trim().equals("")) && (!str2.trim().equals(""))){ result = Double.parseDouble(str1) - Double.parseDouble(str2); text3.setText(String.valueOf(result)); } } if(e.getSource() == mutiplyBtn) { str1 = text1.getText(); str2 = text2.getText(); if((!str1.trim().equals("")) && (!str2.trim().equals(""))){ result = Double.parseDouble(str1) * Double.parseDouble(str2); text3.setText(String.valueOf(result)); } } if(e.getSource() == dividBtn) { str1 = text1.getText(); str2 = text2.getText(); if((!str1.trim().equals("")) && (!str2.trim().equals(""))){ result = Double.parseDouble(str1) / Double.parseDouble(str2); text3.setText(String.valueOf(result)); } } } public static void main(String[] args) { Calculator c = new Calculator(); c.pack(); c.setVisible(true); }}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询