import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test extends JFrame
{
private static final long serialVersionUID = 1L;
public Test ()
{
setTitle ("表达式");
setSize (300, 100);
setResizable (false);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
addComponents ();
setVisible (true);
}
private void addComponents ()
{
final JPanel panel = new JPanel (new BorderLayout ());
final JLabel label = new JLabel ("计算结果: ");
panel.add (label, BorderLayout.NORTH);
final JTextField field = new JTextField ();
field.setHorizontalAlignment(JTextField.RIGHT);
panel.add (field, BorderLayout.CENTER);
JPanel bottom = new JPanel (new GridLayout (1, 2));
JButton btn1 = new JButton ("计算");
ScriptEngineManager sem = new ScriptEngineManager ();
final ScriptEngine se = sem.getEngineByName ("js");
btn1.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
try
{
String str = field.getText ().trim ();
Object obj = se.eval (str);
label.setForeground (Color.BLACK);
label.setText ("计算结果: " + (null == obj ? "" : obj));
field.requestFocus ();
}
catch (ScriptException e1)
{
label.setForeground (Color.RED);
label.setText ("输入出错 !!!");
}
}
});
JButton btn2 = new JButton ("换色");
btn2.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
int r = (int) ( Math.random () * 255 );
int g = (int) ( Math.random () * 255 );
int b = (int) ( Math.random () * 255 );
panel.setBackground (new Color (r, g, b));
}
});
bottom.add (btn1);
bottom.add (btn2);
panel.add (bottom, BorderLayout.SOUTH);
add (panel);
}
public static void main ( String[] args )
{
new Test ();
}
}
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Calculate extends JFrame implements ActionListener{
JTextField jtf1=new JTextField(10);
JTextField jtf2=new JTextField(10);
JLabel jl=new JLabel();
JButton jb1=new JButton("+");
JButton jb2=new JButton("*");
Calculate(){
setLayout(new FlowLayout());
jb1.addActionListener(this);
jb2.addActionListener(this);
add(jtf1);add(jtf2);add(jl);add(jb1);add(jb2);
setResizable(false);
setSize(250, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb1){
if(jtf1.getText().trim().equals("")){
JOptionPane.showMessageDialog(this, "文本框不能为空");
jtf1.requestFocus();
}else if(jtf2.getText().trim().equals("")){
JOptionPane.showMessageDialog(this, "文本框不能为空");
jtf2.requestFocus();
}else{
int a=Integer.parseInt(jtf1.getText().trim());
int b=Integer.parseInt(jtf2.getText().trim());
jl.setText(String.valueOf(a+b));
}
}
if(ae.getSource()==jb2){
if(jtf1.getText().trim().equals("")){
JOptionPane.showMessageDialog(this, "文本框不能为空");
jtf1.requestFocus();
}else if(jtf2.getText().trim().equals("")){
JOptionPane.showMessageDialog(this, "文本框不能为空");
jtf2.requestFocus();
}else{
int a=Integer.parseInt(jtf1.getText().trim());
int b=Integer.parseInt(jtf2.getText().trim());
jl.setText(String.valueOf(a*b));
}
}
}
public static void main(String[] args) {
new Calculate();
}
}