import java.awt.*;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
private class WangPeng extends JFrame implements CaretListener
{ private JTextField text_1,text_2,text_3;
private JComboBox combox_char;
int i;
public WangPeng(Object calculate[])
{super("整数的算术运算");
this.setBounds(300,240,234,300);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout());text_1=new JTextField ("");
this.getContentPane().add(text_1);
combox_char=new JComboBox(calculate);
this.getContentPane().add(combox_char);
text_2=new JTextField ("");
this.getContentPane().add(text_2);
text_2.addCaretListener(this);
this.getContentPane().add(new JLabel("="));
text_1=new JTextField ("");
this.getContentPane().add(text_3);
this.setVisible(true);
}
public void caretUpdate(CaretEvent e)
{if(e.getSource()==text_2)
i=combox_char.getSelectedIndex();
text_3.getText()=text_2.getText()+"calculate[i]"+text_1getText();
}
public static void main(String args[])
{ Object calculate[]={"+","-","*","/"};
new WangPeng(calculate[]);
}}
就是一个标签作为=,三个文本框中有两个是作为输入整数 ,另一个作为答案,中间有一个组合框里面有— + * 、/我到底拿错了
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
private class WangPeng extends JFrame implements CaretListener
{ private JTextField text_1,text_2,text_3;
private JComboBox combox_char;
int i;
public WangPeng(Object calculate[])
{super("整数的算术运算");
this.setBounds(300,240,234,300);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout());text_1=new JTextField ("");
this.getContentPane().add(text_1);
combox_char=new JComboBox(calculate);
this.getContentPane().add(combox_char);
text_2=new JTextField ("");
this.getContentPane().add(text_2);
text_2.addCaretListener(this);
this.getContentPane().add(new JLabel("="));
text_1=new JTextField ("");
this.getContentPane().add(text_3);
this.setVisible(true);
}
public void caretUpdate(CaretEvent e)
{if(e.getSource()==text_2)
i=combox_char.getSelectedIndex();
text_3.getText()=text_2.getText()+"calculate[i]"+text_1getText();
}
public static void main(String args[])
{ Object calculate[]={"+","-","*","/"};
new WangPeng(calculate[]);
}}
就是一个标签作为=,三个文本框中有两个是作为输入整数 ,另一个作为答案,中间有一个组合框里面有— + * 、/我到底拿错了
new WangPeng(calculate);
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class WangPeng extends JFrame implements CaretListener
{
private JTextField text_1,text_2,text_3;
private JComboBox combox_char;
private Object[] calculate;
int i;
public WangPeng(Object calculate[])
{
super("整数的算术运算");
this.calculate = calculate; this.setBounds(300,240,234,300);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout()); text_1 = new JTextField (10);
this.getContentPane().add(text_1);
combox_char=new JComboBox(calculate);
this.getContentPane().add(combox_char); text_2 = new JTextField (10);
this.getContentPane().add(text_2);
text_2.addCaretListener(this);
this.getContentPane().add(new JLabel("="));
text_3 = new JTextField (10);
this.getContentPane().add(text_3);
this.setVisible(true);
}
public void caretUpdate(CaretEvent e)
{
if(e.getSource()==text_2)
i=combox_char.getSelectedIndex();
text_3.setText(text_2.getText() + calculate[i] + text_1.getText());
}
public static void main(String args[])
{
Object[] calculate = {"+","-","*","/"};
new WangPeng(calculate);
}
}
改掉了错误
如
public void caretUpdate(CaretEvent e)
{
if(e.getSource()==text_2)
i=combox_char.getSelectedIndex();
int result;
try{
int a = Integer.parseInt(text_1.getText());
int b = Integer.parseInt(text_2.getText());
if (i==0) result = a+b;
else if (i==1) result = a-b;
else if (i==2) result = a*b;
else result = a/b;
text_3.setText(result);
}catch(Exception e){}
}
text_3.setText("" + result);
} catch (Exception e2) {}
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class WangPeng extends JFrame implements CaretListener
{
private JTextField text_1,text_2,text_3;
private JComboBox combox_char;
int i;
public WangPeng(Object calculate[])
{
super("整数的算术运算");
this.setBounds(300,240,234,300);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout()); text_1=new JTextField ("");
this.getContentPane().add(text_1);
combox_char=new JComboBox(calculate);
this.getContentPane().add(combox_char); text_2=new JTextField ("");
this.getContentPane().add(text_2);
text_2.addCaretListener(this);
this.getContentPane().add(new JLabel("="));
text_3=new JTextField ("");
this.getContentPane().add(text_3);
this.setVisible(true);
}
public void caretUpdate(CaretEvent e)
{
if(e.getSource()==text_2)
i=combox_char.getSelectedIndex();
text_3.setText(text_2.getText() + "calculate[i]" + text_1.getText());
}
public static void main(String args[])
{
Object calculate[]={"+","-","*","/"};
new WangPeng(calculate);
}
}
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentListener;
import java.text.NumberFormat;
import javax.swing.event.DocumentEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
public class WangPeng extends JFrame
{
private static final String[] calculate = {"+","-","*","/"};
private JFormattedTextField text_1,text_2,text_3;
private NumberFormat formatter = NumberFormat.getIntegerInstance();
private JComboBox combox_char; public WangPeng()
{
super("整数的算术运算"); this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout()); text_1 = new JFormattedTextField(formatter);
text_1.setColumns(10);
text_1.setValue(Long.valueOf(0));
this.getContentPane().add(text_1);
combox_char=new JComboBox(calculate);
this.getContentPane().add(combox_char); text_2 = new JFormattedTextField (formatter);
text_2.setValue(Long.valueOf(0));
text_2.setColumns(10);
this.getContentPane().add(text_2);
this.getContentPane().add(new JLabel("="));
text_3 = new JFormattedTextField (formatter);
text_3.setColumns(10);
text_3.setEditable(false);
this.getContentPane().add(text_3);
this.pack();
this.setVisible(true);
xxxxxxx();
} private void xxxxxxx(){
FocusListener listener = new FocusListener(){
@Override
public void focusGained(FocusEvent e){
compute();
}
@Override
public void focusLost(FocusEvent e){
compute();
}
};
text_1.addFocusListener(listener);
text_2.addFocusListener(listener);
text_3.addFocusListener(listener);
combox_char.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
compute();
}
});
} private void compute(){
long n1 = ((Long)text_1.getValue()).longValue();
long n2 = ((Long)text_2.getValue()).longValue();
switch (combox_char.getSelectedIndex()) {
case 0:
text_3.setValue(Long.valueOf(n1+n2));
break;
case 1:
text_3.setValue(Long.valueOf(n1-n2));
break;
case 2:
text_3.setValue(Long.valueOf(n1*n2));
break;
case 3:
if (n2 != 0) {
text_3.setValue(Long.valueOf(n1/n2));
}
break;
default:
break;
}
} public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new java.lang.Runnable(){
@Override public void run(){
new WangPeng();
}
});
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
public class WangPeng extends JFrame implements CaretListener
{
private static final long serialVersionUID = 1L;
private JTextField text_1,text_2,text_3;
private JComboBox combox_char;
int i;
public WangPeng(Object calculate[])
{
super("整数的算术运算");
this.setBounds(300,240,534,100);
this.setBackground(java.awt.Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout()); text_1=new JTextField (10);
text_1.addCaretListener(this);
this.getContentPane().add(text_1);
combox_char=new JComboBox(calculate);
combox_char.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(text_2.getText().length()>0){
try {
int index=combox_char.getSelectedIndex();
int result=0;
switch(index){
case 0:result=Integer.parseInt(text_1.getText())+Integer.parseInt(text_2.getText());break;
case 1:result=Integer.parseInt(text_1.getText())-Integer.parseInt(text_2.getText());break;
case 2:result=Integer.parseInt(text_1.getText())*Integer.parseInt(text_2.getText());break;
case 3:result=Integer.parseInt(text_1.getText())/Integer.parseInt(text_2.getText());break;
}
text_3.setText(""+result);
} catch (Exception e2) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "输入的数字有误!");
text_2.setText("1");
}
}
}
});
this.getContentPane().add(combox_char); text_2=new JTextField (10);
this.getContentPane().add(text_2);
text_2.addCaretListener(this);
this.getContentPane().add(new JLabel("="));
text_3=new JTextField (10);
this.getContentPane().add(text_3);
this.setVisible(true);
}
public void caretUpdate(CaretEvent e)
{
if(text_2.getText().length()>0&&(e.getSource()==text_2||e.getSource()==text_1)){
try {
int index=combox_char.getSelectedIndex();
int result=0;
switch(index){
case 0:result=Integer.parseInt(text_1.getText())+Integer.parseInt(text_2.getText());break;
case 1:result=Integer.parseInt(text_1.getText())-Integer.parseInt(text_2.getText());break;
case 2:result=Integer.parseInt(text_1.getText())*Integer.parseInt(text_2.getText());break;
case 3:result=Integer.parseInt(text_1.getText())/Integer.parseInt(text_2.getText());break;
}
text_3.setText(""+result);
} catch (Exception e2) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "输入的数字有误!");
text_2.setText("1");
}
}
}
public static void main(String args[])
{
Object[] calculate={"+","-","*","/"};
new WangPeng(calculate);
}
}
楼主我帮你改好了 几乎没什么大的改动 而且text_3能显示结果