Java代码四百行 几百行代码
求JAVA编程代码…………
下面是WINDOW界面的代码,用JAVA SWING写的。
专注于为中小企业提供网站制作、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业桓台免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlusWhile {
//主面板
private JFrame mainTop = null;
private JPanel inPanel = null; //输入面板
private JPanel outPanel = null;//输出面板
private JButton count = null; //按钮
private JTextField inText = null; // 输入文本框
private JTextField result = null;//结果文本框
private JLabel resultOut = null;//输出提示
private JLabel remindIn = null;//输入提示
//初始化方法
private void init() {
//组件实例化
mainTop = new JFrame("阶乘计算器");
inPanel = new JPanel();
outPanel = new JPanel();
count = new JButton("计算");
inText = new JTextField(10);
result = new JTextField(10);
resultOut = new JLabel("计算的结果:");
remindIn = new JLabel("请输入要计算的正整数:");
//添加输入组件
inPanel.add(remindIn);
inPanel.add(inText);
inPanel.add(count);
//添加输出组件
outPanel.add(resultOut);
outPanel.add(result);
result.setEnabled(false);
//设置布局样式
mainTop.setLayout(new BorderLayout());
//主面板添加组件
mainTop.add(inPanel, BorderLayout.NORTH);
mainTop.add(outPanel, BorderLayout.SOUTH);
//设置监听
count.addActionListener(new ButtonListener());
//设置屏显位置
mainTop.setLocation(350 , 300);
mainTop.pack();
mainTop.setVisible(true);
mainTop.setAlwaysOnTop(true);
//设置关闭事件
mainTop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//监听类
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
int inputNum = 0;
String input = inText.getText();
//若输入为非数字,提示错误
try {
inputNum = Integer.parseInt(input);
}catch(Exception e) {
inputNum = -1;
}
//输入数值小于0,提示错误
if(inputNum 0) {
inText.setText("请输入正确的数值!");
result.setText("");
}
else if(inputNum == 0) {//输入值为0处理
result.setText("1");
}
else {//输入值为正整数,计算阶乘
int temp = inputNum;
while(inputNum 1) {
temp *= --inputNum;
}
result.setText(temp + "");
}
}
}
//入口方法
public static void main(String[] args) {
new PlusWhile().init();
}
}
有什么不明白的继续补充,呵呵。 加点儿分不。
一个简单的Java程序代码?
package com.zpp;public class Charge {
public static void main(String [] args) {
if(args.length ==0) {
System.out.println("parameter error!");
System.out.println("java com.zpp.Charge [int]");
return;
}
int min = Integer.parseInt(args[0]);
double money = 0.0;
if (min = 0) {
money =0.0;
System.out.println("not money");
} else if (min = 60) {
money = 2.0;
} else {
money = 2.0 + (min - 60) * 0.01;
}
System.out.println("please pay: " + money);
}
} 编译:javac -d . Charge.java运行:java com.zpp.Charge 111
利用java编写代码实现如下功能,需要全部代码
很简单的应用,为了节省字数,代码注释我就不加了
首先是显示层,LoinWindow:
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class LoinWindow extends JFrame implements ActionListener, FocusListener {
private JPanel mainPanel, namePanel, btnPanel;
private JTextField tfName, tfPsd;
private JButton btnLogin, btnCancel;
private static final int WIDTH = 300;
private static final int HEIGHT = 200;
private LoginService service = new LoginService();
public LoinWindow() {
super("登录窗体");
}
public void launch() {
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
GridLayout mainLayout = new GridLayout(2, 1);
mainLayout.setVgap(10);
mainPanel = new JPanel(mainLayout);
GridBagLayout nameLayout = new GridBagLayout();
namePanel = new JPanel(nameLayout);
namePanel.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel nameLabel = new JLabel("姓名:");
tfName = new JTextField();
JLabel psdLabel = new JLabel("密码:");
tfPsd = new JTextField();
JLabel blank = new JLabel(" ");
namePanel.add(nameLabel);
namePanel.add(tfName);
namePanel.add(blank);
namePanel.add(psdLabel);
namePanel.add(tfPsd);
GridBagConstraints s = new GridBagConstraints();
s.fill = GridBagConstraints.BOTH;
s.gridwidth = 1;
s.weightx = 0;
s.weighty = 0;
nameLayout.setConstraints(nameLabel, s);
s.gridwidth = 0;
s.weightx = 1;
s.weighty = 0;
nameLayout.setConstraints(tfName, s);
s.gridwidth = 0;
s.weightx = 4;
s.weighty = 0;
nameLayout.setConstraints(blank, s);
s.gridwidth = 1;
s.weightx = 0;
s.weighty = 0;
nameLayout.setConstraints(psdLabel, s);
s.gridwidth = 3;
s.weightx = 1;
s.weighty = 0;
nameLayout.setConstraints(tfPsd, s);
FlowLayout btnLayout = new FlowLayout();
btnLayout.setAlignment(FlowLayout.CENTER);
btnPanel = new JPanel(btnLayout);
btnLogin = new JButton("确定");
btnCancel = new JButton("取消");
btnPanel.add(btnLogin);
btnPanel.add(btnCancel);
btnCancel.addActionListener(this);
btnLogin.addActionListener(this);
mainPanel.add(namePanel);
mainPanel.add(btnPanel);
setContentPane(mainPanel);
tfName.addFocusListener(this);
tfPsd.addFocusListener(this);
pack();
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == btnCancel) {
System.exit(0);
} else if(source == btnLogin) {
String username = tfName.getText();
String password = tfPsd.getText();
boolean success = service.login(username, password);
if(success) {
warn("成功", "登录成功!");
} else {
warn("失败", "您输入的用户名或密码错误 !");
}
}
}
@Override
public void focusGained(FocusEvent arg0) {
}
@Override
public void focusLost(FocusEvent e) {
Object source = e.getSource();
if(source == tfName) {
String username = tfName.getText();
try {
service.matchUsername(username);
} catch (LoginException e1) {
warn("验证错误", e1.getMessage());
}
} else if(source == tfPsd) {
String password = tfPsd.getText();
try {
service.matchPassword(password);
} catch (LoginException e1) {
warn("验证错误", e1.getMessage());
}
}
}
private void warn(String title, String msg) {
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
new LoinWindow().launch();
}
}
然后是模型层:LoginDao
public class LoginDao {
public boolean login(String username, String password) {
if(username.equals("admin") password.equals("12345")) {
return true;
}
return false;
}
}
LoginService
import java.util.regex.Pattern;
public class LoginService {
private static final Pattern LOGIN_PATTERN = Pattern.compile("[a-zA-Z]+");
private static final Pattern PASSWORD_PATTERN = Pattern.compile("[1-9]+");
private LoginDao dao = new LoginDao();
public boolean matchUsername(String username) throws LoginException {
if(null == username || username.isEmpty()) {
return false;
}
if(!LOGIN_PATTERN.matcher(username).matches()) {
throw new LoginException("您输入的用户名不合法,请输入英文!");
}
return true;
}
public boolean matchPassword(String password) throws LoginException {
if(null == password || password.isEmpty()) {
return false;
}
if(!PASSWORD_PATTERN.matcher(password).matches()) {
throw new LoginException("您输入的密码不合法,请输入数字!");
}
return true;
}
public boolean login(String username, String password) {
if(null == username || username.isEmpty()) {
return false;
}
if(null == password || password.isEmpty()) {
return false;
}
if(!dao.login(username, password)) {
return false;
}
return true;
}
}
LoginException
public class LoginException extends Exception {
public LoginException(String arg0) {
super(arg0);
}
}
不知道分层设计思想是不是我想的这样
跪求一个JAVA小程序,一定是可运行!
import java.awt.BorderLayout;
import java.awt.Color;
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;
import javax.swing.JTextField;
/**
* 一个计算器,与Windows附件自带计算器的标准版功能、界面相仿。
* 但还不支持键盘操作。
*/
public class Calculator extends JFrame implements ActionListener {
/** 计算器上的键的显示名字 */
private final String[] KEYS = { "7", "8", "9", "/", "sqrt", "4", "5", "6",
"*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "=" };
/** 计算器上的功能键的显示名字 */
private final String[] COMMAND = { "Backspace", "CE", "C" };
/** 计算器左边的M的显示名字 */
private final String[] M = { " ", "MC", "MR", "MS", "M+" };
/** 计算器上键的按钮 */
private JButton keys[] = new JButton[KEYS.length];
/** 计算器上的功能键的按钮 */
private JButton commands[] = new JButton[COMMAND.length];
/** 计算器左边的M的按钮 */
private JButton m[] = new JButton[M.length];
/** 计算结果文本框 */
private JTextField resultText = new JTextField("0");
// 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
private boolean firstDigit = true;
// 计算的中间结果。
private double resultNum = 0.0;
// 当前运算的运算符
private String operator = "=";
// 操作是否合法
private boolean operateValidFlag = true;
/**
* 构造函数
*/
public Calculator(){
super();
//初始化计算器
init();
//设置计算器的背景颜色
this.setBackground(Color.LIGHT_GRAY);
this.setTitle("计算器");
//在屏幕(500, 300)坐标处显示计算器
this.setLocation(500, 300);
//不许修改计算器的大小
this.setResizable(false);
//使计算器中各组件大小合适
this.pack();
}
/**
* 初始化计算器
*/
private void init() {
// 文本框中的内容采用右对齐方式
resultText.setHorizontalAlignment(JTextField.RIGHT);
// 不允许修改结果文本框
resultText.setEditable(false);
// 设置文本框背景颜色为白色
resultText.setBackground(Color.white);
//初始化计算器上键的按钮,将键放在一个画板内
JPanel calckeysPanel = new JPanel();
//用网格布局器,4行,5列的网格,网格之间的水平方向间隔为3个象素,垂直方向间隔为3个象素
calckeysPanel.setLayout(new GridLayout(4, 5, 3, 3));
for (int i = 0; i KEYS.length; i++) {
keys[i] = new JButton(KEYS[i]);
calckeysPanel.add(keys[i]);
keys[i].setForeground(Color.blue);
}
//运算符键用红色标示,其他键用蓝色表示
keys[3].setForeground(Color.red);
keys[8].setForeground(Color.red);
keys[13].setForeground(Color.red);
keys[18].setForeground(Color.red);
keys[19].setForeground(Color.red);
//初始化功能键,都用红色标示。将功能键放在一个画板内
JPanel commandsPanel = new JPanel();
//用网格布局器,1行,3列的网格,网格之间的水平方向间隔为3个象素,垂直方向间隔为3个象素
commandsPanel.setLayout(new GridLayout(1, 3, 3, 3));
for (int i = 0; i COMMAND.length; i++) {
commands[i] = new JButton(COMMAND[i]);
commandsPanel.add(commands[i]);
commands[i].setForeground(Color.red);
}
//初始化M键,用红色标示,将M键放在一个画板内
JPanel calmsPanel = new JPanel();
//用网格布局管理器,5行,1列的网格,网格之间的水平方向间隔为3个象素,垂直方向间隔为3个象素
calmsPanel.setLayout(new GridLayout(5, 1, 3, 3));
for (int i = 0; i M.length; i++) {
m[i] = new JButton(M[i]);
calmsPanel.add(m[i]);
m[i].setForeground(Color.red);
}
//下面进行计算器的整体布局,将calckeys和command画板放在计算器的中部,
//将文本框放在北部,将calms画板放在计算器的西部。
//新建一个大的画板,将上面建立的command和calckeys画板放在该画板内
JPanel panel1 = new JPanel();
//画板采用边界布局管理器,画板里组件之间的水平和垂直方向上间隔都为3象素
panel1.setLayout(new BorderLayout(3, 3));
panel1.add("North", commandsPanel);
panel1.add("West", calckeysPanel);
//建立一个画板放文本框
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
top.add("Center", resultText);
//整体布局
getContentPane().setLayout(new BorderLayout(3, 5));
getContentPane().add("North", top);
getContentPane().add("Center", panel1);
getContentPane().add("West", calmsPanel);
//为各按钮添加事件侦听器
//都使用同一个事件侦听器,即本对象。本类的声明中有implements ActionListener
for (int i = 0; i KEYS.length; i++) {
keys[i].addActionListener(this);
}
for (int i = 0; i COMMAND.length; i++) {
commands[i].addActionListener(this);
}
for (int i = 0; i M.length; i++) {
m[i].addActionListener(this);
}
}
/**
* 处理事件
*/
public void actionPerformed(ActionEvent e) {
//获取事件源的标签
String label = e.getActionCommand();
if (label.equals(COMMAND[0])){
//用户按了"Backspace"键
handleBackspace();
} else if (label.equals(COMMAND[1])) {
//用户按了"CE"键
resultText.setText("0");
} else if (label.equals(COMMAND[2])){
//用户按了"C"键
handleC();
} else if ("0123456789.".indexOf(label) = 0) {
//用户按了数字键或者小数点键
handleNumber(label);
// handlezero(zero);
} else {
//用户按了运算符键
handleOperator(label);
}
}
/**
* 处理Backspace键被按下的事件
*/
private void handleBackspace() {
String text = resultText.getText();
int i = text.length();
if (i 0) {
//退格,将文本最后一个字符去掉
text = text.substring(0, i - 1);
if (text.length() == 0) {
//如果文本没有了内容,则初始化计算器的各种值
resultText.setText("0");
firstDigit = true;
operator = "=";
} else {
//显示新的文本
resultText.setText(text);
}
}
}
/**
* 处理数字键被按下的事件
* @param key
*/
private void handleNumber(String key) {
if (firstDigit) {
//输入的第一个数字
resultText.setText(key);
} else if ((key.equals(".")) (resultText.getText().indexOf(".") 0)){
//输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
resultText.setText(resultText.getText() + ".");
} else if (!key.equals(".")) {
//如果输入的不是小数点,则将数字附在结果文本框的后面
resultText.setText(resultText.getText() + key);
}
//以后输入的肯定不是第一个数字了
firstDigit = false;
}
/**
* 处理C键被按下的事件
*/
private void handleC() {
//初始化计算器的各种值
resultText.setText("0");
firstDigit = true;
operator = "=";
}
/**
* 处理运算符键被按下的事件
* @param key
*/
private void handleOperator(String key) {
if (operator.equals("/")) {
//除法运算
//如果当前结果文本框中的值等于0
if (getNumberFromText() == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("除数不能为零");
} else {
resultNum /= getNumberFromText();
}
} else if (operator.equals("1/x")) {
//倒数运算
if (resultNum == 0.0){
//操作不合法
operateValidFlag = false;
resultText.setText("零没有倒数");
} else {
resultNum = 1 / resultNum;
}
} else if (operator.equals("+")){
//加法运算
resultNum += getNumberFromText();
} else if (operator.equals("-")){
//减法运算
resultNum -= getNumberFromText();
} else if (operator.equals("*")){
//乘法运算
resultNum *= getNumberFromText();
} else if (operator.equals("sqrt")) {
//平方根运算
resultNum = Math.sqrt(resultNum);
} else if (operator.equals("%")){
//百分号运算,除以100
resultNum = resultNum / 100;
} else if (operator.equals("+/-")){
//正数负数运算
resultNum = resultNum * (-1);
} else if (operator.equals("=")){
//赋值运算
resultNum = getNumberFromText();
}
if (operateValidFlag) {
//双精度浮点数的运算
long t1;
double t2;
t1 = (long) resultNum;
t2 = resultNum - t1;
if (t2 == 0) {
resultText.setText(String.valueOf(t1));
} else {
resultText.setText(String.valueOf(resultNum));
}
}
//运算符等于用户按的按钮
operator = key;
firstDigit = true;
operateValidFlag = true;
}
/**
* 从结果文本框中获取数字
* @return
*/
private double getNumberFromText() {
double result = 0;
try {
result = Double.valueOf(resultText.getText()).doubleValue();
} catch (NumberFormatException e){
}
return result;
}
public static void main(String args[]) {
Calculator calculator1 = new Calculator();
calculator1.setVisible(true);
calculator1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
期末JAVA课程考试,老师让做一个400行的程序
额,四百行?随便贴几个类给你就够了。不过你老师也真变态,不会要你们在一个类里写四百行吧,哪有老师叫人家写程序是按行算的?!!!!!!悲剧的老师
单片机程序400行代码算多吗
这个代码有点多,应该精简。你应该想想如何用更少的代码完成相应的功能,什么时候能用100行代码完成你400行代码的功能时,你才发现自己进步了
网站题目:Java代码四百行 几百行代码
分享URL:http://ybzwz.com/article/hgeiio.html