资源简介
包括日历,加减计算器,文件选择,以及用SWT Designer设计的界面,都是实习指导书上的练习,希望大家可以指正
代码片段和文件信息
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends Jframe{
private int num1;
private int num2;
private int sign;
private JTextField text;
private String string;
private String cr;
public static void main(String[]args)
{
Calculator cr = new Calculator();
cr.setVisible(true);
}
public Calculator()
{
super();
setBounds(100100363325);
settitle(“小型计算器“);
setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
sign = 0;
num1 = 0;
num2 = 0;
string = ““;
cr = ““;
text = new JTextField();
text.setHorizontalAlignment(SwingConstants.RIGHT);
text.setColumns(18);
final JPanel viewPanel = new JPanel();
getContentPane().add(viewPanelBorderLayout.NORTH);//把文本框视图面板放进容器中
viewPanel.add(text);//将文本框放入视图面板容器
final JButton buttons[][] = new JButton[4][4];
String names[][] = {{“1““2““3““+“}{“4““5““6““-“}{“7““8““9““*“}{“0““=““/““clear“}};
JPanel buttonPanel = new JPanel();
GridLayout gridLayout = new GridLayout(40);
gridLayout.setVgap(10);
gridLayout.setHgap(10);
buttonPanel.setLayout(gridLayout);
getContentPane().add(buttonPanelBorderLayout.CENTER);
for(int i = 0;i < names.length;i++)
{
for(int j = 0;j < names.length;j++)
{
buttons[i][j]= new JButton(names[i][j]);
buttons[i][j].addActionListener(new ButtonActionListener());
buttonPanel.add(buttons[i][j]);
}
}
final JLabel leftLabel = new JLabel();
leftLabel.setPreferredSize(new Dimension(100));
getContentPane().add(leftLabelBorderLayout.WEST);
final JLabel rightLabel = new JLabel();
rightLabel.setPreferredSize(new Dimension(100));
getContentPane().add(rightLabelBorderLayout.EAST);
final JLabel bottom = new JLabel();
bottom.setPreferredSize(new Dimension(100));
getContentPane().add(bottomBorderLayout.SOUTH);
}
class ButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
String name = button.getActionCommand();
string+=name;
if(name.equals(“+“)||name.equals(“-“)||name.equals(“*“)||name.equals(“/“))
{
text.setText(string);
sign = 2;//被加数输入完毕
cr = name;
}
else if(name.equals(“=“))
{
text.setText(string);
if(cr.equals(“+“))
{
string += num1+num2;
text.setText(string);
}
else if(cr.equals(“-“))
{
string += num1-num2;
text.setText(string);
}
else if(cr.equals(“*“))
{
string += num1*num2;
text.setText(string);
}
else
{
if(num2==0)
{
string = “Error“;
num1=0;
num2=0;
sign = 0;
cr = ““;
text.setText(string);
}
else
{
string += (double)num1/num2;
text.setText(string);
}
}
}
else if(name.equals(“clear“))
{
text.setText(““);
num1=0;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3543 2010-04-24 19:03 swing\1\Calculator.java
文件 3864 2010-04-24 18:53 swing\2\Calender.java
文件 2520 2010-05-06 14:51 swing\3\Add.java
文件 1998 2010-05-06 14:45 swing\4\Display.java
文件 1477 2010-04-23 19:15 swing\5\Enqury.java
文件 1328 2010-05-06 19:22 swing\67\my\login.java
文件 2506 2010-05-06 19:22 swing\67\my\mywindow.java
目录 0 2010-05-06 19:34 swing\67\my
目录 0 2010-05-06 19:36 swing\1
目录 0 2010-05-06 19:36 swing\2
目录 0 2010-05-06 19:36 swing\3
目录 0 2010-05-06 19:37 swing\4
目录 0 2010-05-06 19:37 swing\5
目录 0 2010-05-06 19:37 swing\67
目录 0 2010-04-30 09:08 swing
----------- --------- ---------- ----- ----
17236 15
- 上一篇:java推箱子小游戏源码完整版
- 下一篇:Hill密码java实现
评论
共有 条评论