java图像界面开发简单实例
JButton及事件的简单应用,做一个计算器的小例子,代码如下:
import
java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 计算器实例
* @author 左杰 jdk5.0
*/
public class CalculatorFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L ;
public CalculatorFrame() {
setTitle( " 计算器 " );
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
public static void main(String[] args) {
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible( true );
}
}
/**
* 创建显示计算器的面板
*/
class CalculatorPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L ;
private JButton display; // 用于显示结果
private JPanel panel; // 创建用于显示数字及符号按钮的面板
private double result; // 用于存储计算结果
private String lastCommand; // 记录点击的符号
private boolean start; // 开始标志
public CalculatorPanel() {
setLayout( new BorderLayout());
result = 0 ;
lastCommand = " = " ;
start = true ;
// 添加
display = new JButton( " 0 " );
display.setEnabled( false );
add(display, BorderLayout.NORTH);
// 创建事件侦听对象
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
// 添加数字及符号按钮的面板面板
panel = new JPanel();
panel.setLayout( new GridLayout( 4 , 4 )); // 设置面板布局为4行4列
// 添加数字及符号按钮
addButton( " 7 " , insert);
addButton( " 8 " , insert);
addButton( " 9 " , insert);
addButton( " / " , command);
addButton( " 4 " , insert);
addButton( " 5 " , insert);
addButton( " 6 " , insert);
addButton( " * " , command);
addButton( " 1 " , insert);
addButton( " 2 " , insert);
addButton( " 3 " , insert);
addButton( " - " , command);
addButton( " 0 " , insert);
addButton( " . " , insert);
addButton( " = " , command);
addButton( " + " , command);
add(panel, BorderLayout.CENTER);
}
/**
* 用来设置按钮及添加相应的监听
*
* @param label 按钮显示的内容
* @param listener 相应事件侦听对象
*
*/
private void addButton(String label, ActionListener listener) {
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
/**
* 点击数字按钮事件,用于显示数字及将start标志置为false;
*/
private class InsertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (start) {
display.setText( "" );
start = false ;
}
display.setText(display.getText() + input);
}
}
/**
* 点击符号按钮事件,并进行计算
*/
private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
// 如果start为true,表示开始那么点击-号表示输入负数
if (start) {
if (command.equals( " - " )) {
display.setText(command);
start = false ;
} else
lastCommand = command;
} else { // start为false,进行计算
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true ;
}
}
}
/**
* 计算方法更具输入的数据和符号进行计算
*
* @param x 输入的数字
*
*/
public void calculate( double x) {
if (lastCommand.equals( " + " ))
result += x;
else if (lastCommand.equals( " - " ))
result -= x;
else if (lastCommand.equals( " * " ))
result *= x;
else if (lastCommand.equals( " / " ))
result /= x;
else if (lastCommand.equals( " = " ))
result = x;
display.setText( "" + result);
}
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 计算器实例
* @author 左杰 jdk5.0
*/
public class CalculatorFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L ;
public CalculatorFrame() {
setTitle( " 计算器 " );
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
}
public static void main(String[] args) {
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible( true );
}
}
/**
* 创建显示计算器的面板
*/
class CalculatorPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L ;
private JButton display; // 用于显示结果
private JPanel panel; // 创建用于显示数字及符号按钮的面板
private double result; // 用于存储计算结果
private String lastCommand; // 记录点击的符号
private boolean start; // 开始标志
public CalculatorPanel() {
setLayout( new BorderLayout());
result = 0 ;
lastCommand = " = " ;
start = true ;
// 添加
display = new JButton( " 0 " );
display.setEnabled( false );
add(display, BorderLayout.NORTH);
// 创建事件侦听对象
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
// 添加数字及符号按钮的面板面板
panel = new JPanel();
panel.setLayout( new GridLayout( 4 , 4 )); // 设置面板布局为4行4列
// 添加数字及符号按钮
addButton( " 7 " , insert);
addButton( " 8 " , insert);
addButton( " 9 " , insert);
addButton( " / " , command);
addButton( " 4 " , insert);
addButton( " 5 " , insert);
addButton( " 6 " , insert);
addButton( " * " , command);
addButton( " 1 " , insert);
addButton( " 2 " , insert);
addButton( " 3 " , insert);
addButton( " - " , command);
addButton( " 0 " , insert);
addButton( " . " , insert);
addButton( " = " , command);
addButton( " + " , command);
add(panel, BorderLayout.CENTER);
}
/**
* 用来设置按钮及添加相应的监听
*
* @param label 按钮显示的内容
* @param listener 相应事件侦听对象
*
*/
private void addButton(String label, ActionListener listener) {
JButton button = new JButton(label);
button.addActionListener(listener);
panel.add(button);
}
/**
* 点击数字按钮事件,用于显示数字及将start标志置为false;
*/
private class InsertAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String input = event.getActionCommand();
if (start) {
display.setText( "" );
start = false ;
}
display.setText(display.getText() + input);
}
}
/**
* 点击符号按钮事件,并进行计算
*/
private class CommandAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
// 如果start为true,表示开始那么点击-号表示输入负数
if (start) {
if (command.equals( " - " )) {
display.setText(command);
start = false ;
} else
lastCommand = command;
} else { // start为false,进行计算
calculate(Double.parseDouble(display.getText()));
lastCommand = command;
start = true ;
}
}
}
/**
* 计算方法更具输入的数据和符号进行计算
*
* @param x 输入的数字
*
*/
public void calculate( double x) {
if (lastCommand.equals( " + " ))
result += x;
else if (lastCommand.equals( " - " ))
result -= x;
else if (lastCommand.equals( " * " ))
result *= x;
else if (lastCommand.equals( " / " ))
result /= x;
else if (lastCommand.equals( " = " ))
result = x;
display.setText( "" + result);
}
}