学了那么久了,终于开始决定开始写博客。把每天的收获写出来,时间久了可能有些知识会忘了,这也是一个永久存储知识点的好地方。毕竟我的电脑有事没事出问题重装系统。今天没有上课,说说最近的收获吧。
简易计算器
帮学妹做了一个简易计算器,一个很简单的,没有什么难度,基于本人的能力有限,只实现了2个操作数的加减乘除的功能。代码及思路如下:
1.界面实现
package com.zyb.counter;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class Counter {
protected Shell shell;
public int op;
private Text text;
public double num1;
public double num2;
public Display display;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
Counter window = new Counter();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*
* @wbp.parser.entryPoint
*/
protected void createContents() {
shell = new Shell();
shell.setSize(359, 300);
shell.setText("\u8BA1\u7B97\u5668");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
Composite composite = new Composite(shell, SWT.NONE);
//composite.setBackground(SWTResourceManager
// .getColor(SWT.COLOR_DARK_CYAN));
//composite.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
composite.setLayout(null);
final Button button = new Button(composite, SWT.NONE);
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
}
});
button.setBounds(108, 82, 53, 25);
button.setText("1");
final Button button_1 = new Button(composite, SWT.NONE);
button_1.setBounds(179, 82, 53, 25);
button_1.setText("2");
final Button button_2 = new Button(composite, SWT.NONE);
button_2.setBounds(249, 82, 53, 25);
button_2.setText("3");
final Button button_3 = new Button(composite, SWT.NONE);
button_3.setBounds(108, 124, 53, 25);
button_3.setText("4");
final Button button_4 = new Button(composite, SWT.NONE);
button_4.setBounds(179, 124, 53, 25);
button_4.setText("5");
final Button button_5 = new Button(composite, SWT.NONE);
button_5.setBounds(249, 124, 53, 25);
button_5.setText("6");
final Button button_6 = new Button(composite, SWT.NONE);
button_6.setBounds(108, 170, 53, 25);
button_6.setText("7");
final Button button_7 = new Button(composite, SWT.NONE);
button_7.setBounds(179, 170, 53, 25);
button_7.setText("8");
final Button button_8 = new Button(composite, SWT.NONE);
button_8.setBounds(249, 170, 53, 25);
button_8.setText("9");
Label label_1 = new Label(composite, SWT.NONE);
//label_1.setFont(SWTResourceManager.getFont("新宋体", 15, SWT.NORMAL));
label_1.setBounds(38, 35, 48, 25);
label_1.setText(" =");
Button button_9 = new Button(composite, SWT.NONE);
//button_9.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 15,
// SWT.NORMAL));
button_9.setBounds(38, 82, 53, 25);
button_9.setText("+");
Button button_10 = new Button(composite, SWT.NONE);
button_10.setBounds(38, 124, 53, 25);
button_10.setText("-");
Button btnX = new Button(composite, SWT.NONE);
btnX.setBounds(38, 170, 53, 25);
btnX.setText("x");
Button button_11 = new Button(composite, SWT.NONE);
button_11.setBounds(38, 215, 53, 25);
button_11.setText("\u00F7");
final Button button_12 = new Button(composite, SWT.NONE);
button_12.setBounds(226, 215, 80, 25);
button_12.setText("0");
Button button_13 = new Button(composite, SWT.NONE);
button_13.setBounds(108, 215, 80, 25);
button_13.setText(".");
SashForm sashForm = new SashForm(composite, SWT.NONE);
sashForm.setBounds(38, 70, 271, 181);
text = new Text(composite, SWT.BORDER);
text.setBounds(115, 35, 191, 25);
}
}
2.获取点击button中的值,对于(+-*÷),在监听事件中定义op=?,然后定义第一个操作数。
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_1.getText());
}
});
button_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_2.getText());
}
});
button_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_3.getText());
}
});
button_4.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_4.getText());
}
});
button_5.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_5.getText());
}
});
button_6.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_6.getText());
}
});
button_7.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_7.getText());
}
});
button_8.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_8.getText());
}
});
button_12.addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e) {
text.setText(button_12.getText());
}
});
button_9.addMouseListener(new MouseAdapter() { //+
@Override
public void mouseDown(MouseEvent e) {
op = 1;
num1 = Double.parseDouble(text.getText());
text.setText("");
}
});
button_10.addMouseListener(new MouseAdapter() { // -
@Override
public void mouseDown(MouseEvent e) {
op = 2;
num1 = Double.parseDouble(text.getText());
text.setText("");
}
});
button_11.addMouseListener(new MouseAdapter() { // ÷
@Override
public void mouseDown(MouseEvent e) {
op = 4;
num1 = Double.parseDouble(text.getText());
text.setText("");
}
});
btnX.addMouseListener(new MouseAdapter() { // *
@Override
public void mouseDown(MouseEvent e) {
op = 3;
num1 = Double.parseDouble(text.getText());
text.setText("");
}
});
3.显示结果的框是label属性的,构建双击(=)按钮在label中显示结果的计算方法
label_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
num2 = Double.parseDouble(text.getText());
switch (op) {
case 1:
num1 = num1 + num2;
text.setText(num1+"");
break;
case 2:
num1 = num1 - num2;
text.setText(num1+"");
break;
case 3:
num1 = num1 * num2;
text.setText(num1+"");
break;
case 4:
num1 = num1 / num2;
text.setText(num1+"");
break;
default:
break;
}
}
});
然后就OK啦,这是效果图
PS:我做这个计算器遇到的问题就是传值传错了,将button获取值时写成了button.setText(button_5.getText());其他都就没有什么问题了。
制作网页时关于搜索框的问题
制作网页中的搜索框时,有时需要在搜索框中设置默认文字,鼠标点击输入框然后开始键盘输入时,默认文字消失;清空输入框,点击输入框以外的地方,默认文字出现。我最开始的时候使用了jQuery中的click事件实现,可是这样实现比较麻烦并且不能完全实现效果,然后问了朋友,朋友介绍了一个简单有效的方法。关键在于placeholder,它可以实现效果。
ps:设置background-image属性时不能加repeat等,只能设置路径。