《Java程序设计》实 验 报 告(一)
实验名称: Java开发环境的熟悉 实验地点:10-414
所使用的工具软件及环境:
环境:Windows 8.1;jdk 1.8;
工具:eclipse
实验目的:
在Eclipse下编辑、编译、运行、调试简单的Java程序 。
实验内容:
1.熟悉Java Application编程;
例子: Scanner 类p125.
标准对话框p155
2.熟悉WindowBuilder UI编程,建立一个简单的输入输出框,如图。
程序1:
package com;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
for(int i=0;i<5;i++){
Scanner input = new Scanner(System.in);
System.out.println("请输入学生的分数:");
double score = input.nextDouble();
int level1,level2;
if(score>=90.0 && score<=100.0){
level1 = 1;}
else if(score>=75.0){
level1 = 2;}
else if(score>=60.0){
level1 = 3;}
else if(score>=40.0){
level1 = 4;}
else{
level1 = 5;}
if(score>=85.0&&score<=100.0){
level2 = 1;}
else if(score>=70.0){
level2 = 2;}
else if(score>=60.0){
level2 = 3;}
else{
level2 = 4;}
switch(level1)
{
case 1:
System.out.print("该生的成绩绩点是: 5\n");
break;
case 2:
System.out.print("该生的成绩绩点是: 4\n");
break;
case 3:
System.out.print("该生的成绩绩点是: 3\n");
break;
case 4:
System.out.print("该生的成绩绩点是: 2\n");
break;
case 5:
System.out.print("该生的成绩绩点是: 1\n");
break;
}
}
}
}
程序2:
package com;
import javax.swing.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.HashMap;
public class Swing01 extends JFrame implements ActionListener {
// public class OptionPaneDemo extends JFrame implements ActionListener {
private JPanel p;
private JButton btnInput, btnMsg, btnConfirm, btnOption;
private JTextField txtResult;
public Swing01() {
super("标准对话框");
p = new JPanel();
btnInput = new JButton("输入");
btnMsg = new JButton("消息");
btnConfirm = new JButton("确认");
btnOption = new JButton("选项");
txtResult = new JTextField(20);
// 注册监听
btnInput.addActionListener(this);
btnMsg.addActionListener(this);
btnConfirm.addActionListener(this);
btnOption.addActionListener(this);
p.add(btnInput);
p.add(btnMsg);
p.add(btnConfirm);
p.add(btnOption);
p.add(txtResult);
this.add(p);
this.setSize(300, 150);
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
// 获取事件源
Object source = e.getSource();
if (source == btnInput) {
String strIn = JOptionPane.showInputDialog(btnInput, "请输入姓名:");
txtResult.setText(strIn);
}
if (source == btnMsg) {
JOptionPane.showMessageDialog(btnMsg, "这是一个消息对话框", "提示",
JOptionPane.INFORMATION_MESSAGE);
}
if (source == btnConfirm) {
int r = JOptionPane.showConfirmDialog(btnConfirm, "您确定要删除吗?", "删除",
JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.YES_OPTION) {
txtResult.setText("删除!");
}
}
if (source == btnOption) {
Object[] options = {
"Red", "Green", "Blue" };
int sel = JOptionPane.showOptionDialog(btnOption, "选择颜色:", "选择",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
if (sel != JOptionPane.CLOSED_OPTION) {
txtResult.setText("颜色: " + options[sel]);
}
}
}
public static void main(String[] args) {
Swing01 f = new Swing01();
f.setVisible(true);
}
}
程序3:
package com;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
import java.awt.Font;
public class index extends JFrame {
private JTextField textField;
public index() {
getContentPane().setLayout(null);
textField = new JTextField();
textField.setFont(new Font("宋体", Font.PLAIN, 15));
textField.setText("登录成功");
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setBounds(123, 62, 172, 70);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblNewLabel = new JLabel("恭喜您");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
lblNewLabel.setBounds(181, 161, 114, 27);
getContentPane().add(lblNewLabel);
}
}
package com;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JTextPane;
import javax.swing.JProgressBar;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Dialog.ModalExclusionType;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
public class mainWindow {
private JFrame frame,f2;
private JTextField acc;
private JTextField psw;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainWindow window = new mainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public mainWindow() {
initialize();
}
private void initialize() {
frame = new JFrame();
f2= new index();
f2.setVisible(false);
f2.setSize(500,300);
frame.setTitle("请验证用户名和密码");
frame.getContentPane().setFont(new Font("宋体", Font.PLAIN, 15));
frame.getContentPane().setLayout(null);
frame.setSize(500,300);
acc = new JTextField();
acc.setBackground(Color.WHITE);
acc.setBounds(135, 37, 222, 32);
frame.getContentPane().add(acc);
acc.setColumns(10);
JLabel lblNewLabel = new JLabel("用户名");
lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 15));
lblNewLabel.setBounds(67, 41, 58, 23);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("密码");
lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 15));
lblNewLabel_1.setBounds(67, 107, 58, 23);
frame.getContentPane().add(lblNewLabel_1);
JButton btnNewButton = new JButton("确认");
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 16));
btnNewButton.setBounds(79, 168, 97, 38);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
check();
}
});
JButton btnNewButton_1 = new JButton("取消");
btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 16));
btnNewButton_1.setBounds(274, 168, 97, 38);
frame.getContentPane().add(btnNewButton_1);
psw = new JTextField();
psw.setBounds(135, 103, 222, 32);
frame.getContentPane().add(psw);
psw.setColumns(10);
}
public void check() {
if(psw.getText().equals("123456")&&acc.getText().equals("root")) {
//JOptionPane.showMessageDialog(null, "登录成功!");
f2.setVisible(true);
frame.setVisible(false);
}
else if (psw.getText().trim().length() == 0 || new String(acc.getText()).trim().length() == 0) {
JOptionPane.showMessageDialog(null, "用户名或密码不能为空!");
}
else {
JOptionPane.showMessageDialog(null, "登录失败(用户名或密码错误)!");
}
}
}
实验体会:
1.对于程序的编写要有良好的缩进习惯和注释习惯,否则既不便于别人阅读自己的代码,也不方便自己日后对自己的代码进行复习和巩固。
2.对于包的引入和管理要仔细,很多时候报错就是因为相应的类和包没有引入造成的。
3.对于继承和面向对象的编程方式还是要勤加练习,才能更加熟悉这种编程方式。
4.对于图形界面的布局格式要从一开始就想好,不要后续再改来改去,会比较难以调整,造成不必要的麻烦。
总结:
学习到了java对于编写图形界面的基本技巧和一些布局相关的基础知识,对swing和AWT的使用有了更加深入的了解。同时也加深了我对java面向对象编程方式的熟悉和认识。并且,我还感受了java整个生态体系的广大和编程的方便性,对于自己的学习有了更多的认知,还是要更多地探索未知自己的未知的技术领域。