包含java程序设计300例的词条

博主:adminadmin 2023-01-27 16:27:12 638

今天给各位分享java程序设计300例的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

我在学JAVA 想问一下《head first Java》 这本书的电子版有人有吗?

推荐些好的书籍和网站给你:

书籍

Java编程思想

Effective Java

Java并发编程实战

Head First设计模式

大话设计模式

敏捷软件开发:原则、模式与实践

轻量级JavaEE整合开发

深入理解Java虚拟机:JVM高级特性与最佳实践

鸟哥的Linux私房菜 基础学习篇

鸟哥的Linux私房菜服务器篇

Maven权威指南

互联网思维 独孤九剑

//GoodWeb sites

论坛类

(最大优点:业界很多问题解决的提出与解决)

开源社区

(阿里开原社区)

(腾讯开源社区)

官网类

Jar包库

(开源库)

(maven库)

昆明Java培训:如何验证IP地址的有效性

【实例描述】IP地址是网络上每台计算机的标识,在浏览器中输入的网址也是要经过DNS服务器转换为IP地址才能找到服务器的,在很多网络程序中要求输入服务器IP地址或者对方连接的IP地址,IP地址的错误输入将使程序无法运行。

本实例将实现对IP地址的验证功能,实例的运行效果如图4.14所示。

【实现过程】(1)在Eclipse中新建项目CheckIP,并在其中创建一个CheckIP.java文件。

在该类的主方法中创建标准输入流的扫描器对象,接收用户输入的IP地址。

核心代码如下所示:protectedvoiddo_button_actionPerformed(ActionEvente){Stringtext=ipField.getText();//获取用户输入Stringinfo=matches(text);//对输入文本进行IP验证showMessageDialog(null,info);//用对话框输出验证结果}(2)编写IP地址的mathches()方法,该方法利用正则表达式对输入的字符串进行验证,并返回验证结果,关键代码如下所示。

publicStringmatches(Stringtext){if(text!=null!text.isEmpty()){//定义正则表达式Stringregex="^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."+"(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";//判断IP地址是否与正则表达式匹配if(text.matches(regex)){//返回判断信息returntext+"\n是一个合法的IP地址!";}else{//返回判断信息returntext+"\n不是一个合法的IP地址!";}}//返回判断信息return"请输入要验证的IP地址!";}【代码解析】本实例的关键点在于IP地址格式与数字范围的验证,用户在输入IP地址时,程序可以获取的只有字符串类型,所以本实例利用字符串的灵活性和正则表达式搭配进行IP地址格式与范围的验证。

该方法是String字符串类的方法,用于判断字符串与制定的正则表达式是否匹配。

其声明语法如下:publicbooleanmathches(Stringregex);Java程序设计经典300例XXXVIII【知识扩展】在正则表达式中,“.”代表任何一个字符,因此在正则表达式中如果想使用普通意义的点字符“.”,必须使用转义字符“\”。

java程序设计,编写一个记事本程序

全实现,程序太长,发不上去,先实现基本的 图形用户界面

import java.awt.*;

import java.awt.event.*;

public class TestMenu {

public static void main (String[] args) {

new MenuFrame("新建"+" "+"文本文档"+".txt"+" "+"-"+" "+"记事本").launchFrame();

}

}

class MenuFrame extends Frame {

MenuBar mb = null;

MenuFrame (String s) {

super (s);

}

public void launchFrame() {

Menu file = new Menu ("文件");

Menu edit = new Menu ("编辑");

Menu format = new Menu ("格式");

Menu help = new Menu ("帮助");

MenuItem newItem = new MenuItem ("新建");

newItem.addActionListener (new ActionListener () {

public void actionPerformed(ActionEvent e) {

final Frame ff = new Frame ("记事本");

ff.setMenuBar(mb);

ff.setBounds (300,300,400,200);

ff.setVisible (true);

ff. addWindowListener (new WindowAdapter () {

public void windowClosing(WindowEvent e) {

ff.setVisible (false);

}

} );

}

});

MenuItem saveItem = new MenuItem ("保存");

MenuItem exitItem = new MenuItem ("退出");

MenuItem helpTitle = new MenuItem ("帮助主题");

MenuItem line = new MenuItem ("-");

MenuItem about = new MenuItem ("关于记事本");

MenuItem copy = new MenuItem ("粘贴");

MenuItem serach = new MenuItem ("查找");

edit.add (copy);

edit.add (serach);

help.add (helpTitle);

help.add (line);

help.add (about);

exitItem.addActionListener (new ActionListener () {

public void actionPerformed(ActionEvent e) {

System.exit (0);

}

} );

file.add (newItem);

file.add (saveItem);

file.add (exitItem);

mb = new MenuBar();

mb.add (file);

mb.add (edit);

mb.add (format);

mb.add (help);

addWindowListener (new WindowAdapter () {

public void windowClosing(WindowEvent e) {

System.exit (0);

}

} );

setLayout (new FlowLayout());

setMenuBar (mb);

setBounds (300,300,400,200);

setVisible (true);

}

}

java程序设计题

1.int year=(int)(Math.random()*9000)+1000;

if(year%4==0year%100!=0){

System.out.println(year+"是闰年");

}else{

System.out.println(year+"是平年");

}

2.Double money = 0.0; int x = 0;int t = 0;

if(x100){

money = 1.00;

}else if(x300){

money = 0.90*t;

}else if(x500){

money = 0.80*t;

}else if(x1000){

money = 0.70*t;

}else{

money = 0.60*t;

}

3.对存钱这一块的东西不太了解。

4.int sun = 0 , x =2 ;

while(x1000){

sun = sum+x;

x = x+2;

}

5.int x ;一个四位数

int a = x/1000;

int b = (x - a*1000)/100;

int c = (x-a*1000-b*100)/10;

int d = x-a*1000-b*100-c*10 ;

Sytem.out.print(d+c+b+a);

}

我是一名学软件的学生,有那位高手知道如何编写有300行代码的JAVA程序?

差不多300行了!

/* 本程序使用常见组件设计一个用于输入学生信息的用户界面 */

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.io.*;

public class Example0701_Components extends JFrame implements ActionListener

{

JTextField name;

JPasswordField password;

JRadioButton male;

JRadioButton female;

JCheckBox party;

JSpinner age;

JButton selectColor;

JPanel color;

JSlider addition;

JComboBox department;

JList course;

JButton confirm;

JButton save;

JTextArea result;

JProgressBar progbar;

JLabel time;

public Example0701_Components()

{

setTitle("Components usage in inputting the information of students");

setDefaultCloseOperation(EXIT_ON_CLOSE);

buildContentPane();

setExtendedState(MAXIMIZED_BOTH);

setVisible(true);

}

public void buildContentPane()

{

name = new JTextField();

password = new JPasswordField();

male = new JRadioButton("男", true);

female = new JRadioButton("女");

ButtonGroup group = new ButtonGroup();

group.add(male);

group.add(female);

party = new JCheckBox("", false);

age = new JSpinner();

age.setValue(new Integer(20));

age.addChangeListener(new ChangeListener()

{

public void stateChanged(ChangeEvent evt)

{

Integer v = (Integer)age.getValue();

int value = v.intValue();

if (value = 15)

{

age.setValue(new Integer(15));

}

if (value = 25)

{

age.setValue(new Integer(25));

}

}

});

color = new JPanel();

color.setBorder(BorderFactory.createLineBorder(Color.BLUE));

addition = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);

addition.setMajorTickSpacing(10);

addition.setMinorTickSpacing(5);

addition.setPaintTicks(true);

addition.setPaintLabels(true);

addition.setSnapToTicks(true);

String[] departmentNames = {

"计算机科学与技术系",

"电子信息与技术系",

"计算机工程系"

};

department = new JComboBox(departmentNames);

department.setEditable(false);

String[] coursesNames = {

"数据结构",

"操作系统",

"网络原理",

"Java程序设计",

"分布式系统开发技术",

"计算机导论",

"密码学",

"计算机组成原理",

"编译原理",

"图形学"

};

course = new JList(coursesNames);

course.setVisibleRowCount(5);

confirm = new JButton("确认");

confirm.addActionListener(this);

save = new JButton("保存");

save.addActionListener(this);

result = new JTextArea();

time = new JLabel("计时开始...");

progbar = new JProgressBar(JProgressBar.HORIZONTAL,0,100);

progbar.setStringPainted(true);

Timer timer = new Timer(1000, new ActionListener()

{

public void actionPerformed(ActionEvent evt)

{

java.text.SimpleDateFormat sf = new java.text.SimpleDateFormat("yyyy 年 M 月 d 日 hh : mm : ss");

time.setText(sf.format(new java.util.Date()));

if (progbar.getValue() = 100) progbar.setValue(0);

progbar.setValue(progbar.getValue() + 5);

}

});

timer.start();

Box boxName = Box.createHorizontalBox();

boxName.add(new JLabel("姓名"));

boxName.add(name);

Box boxPassword = Box.createHorizontalBox();

boxPassword.add(new JLabel("密码"));

boxPassword.add(password);

Box boxSexPartyAgeColor = Box.createHorizontalBox();

boxSexPartyAgeColor.add(new JLabel("性别"));

boxSexPartyAgeColor.add(male);

boxSexPartyAgeColor.add(female);

boxSexPartyAgeColor.add(Box.createHorizontalGlue());

boxSexPartyAgeColor.add(new JLabel("党否"));

boxSexPartyAgeColor.add(party);

boxSexPartyAgeColor.add(Box.createHorizontalGlue());

boxSexPartyAgeColor.add(new JLabel("年龄"));

boxSexPartyAgeColor.add(age);

boxSexPartyAgeColor.add(Box.createHorizontalGlue());

selectColor = new JButton("选择颜色");

selectColor.addActionListener(this);

boxSexPartyAgeColor.add(selectColor);

boxSexPartyAgeColor.add(Box.createHorizontalStrut(10));

boxSexPartyAgeColor.add(color);

Box boxAge = Box.createHorizontalBox();

Box boxAddition = Box.createHorizontalBox();

boxAddition.add(new JLabel("加分"));

boxAddition.add(addition);

Box box1 = Box.createVerticalBox();

box1.add(Box.createVerticalStrut(20));

box1.add(boxName);

box1.add(Box.createVerticalStrut(20));

box1.add(boxPassword);

box1.add(Box.createVerticalStrut(20));

box1.add(boxSexPartyAgeColor);

box1.add(Box.createVerticalStrut(20));

box1.add(boxAddition);

box1.add(Box.createVerticalStrut(20));

Box butt = Box.createHorizontalBox();

butt.add(confirm);

butt.add(Box.createHorizontalStrut(10));

butt.add(save);

Box box2 = Box.createVerticalBox();

box2.add(new JLabel("系别"));

box2.add(department);

box2.add(Box.createVerticalStrut(10));

box2.add(new JLabel("选课"));

box2.add(new JScrollPane(course));

box2.add(Box.createVerticalStrut(10));

box2.add(butt);

Box box = Box.createHorizontalBox();

box.setBorder(BorderFactory.createTitledBorder("请输入学生信息"));

box.add(box1);

box.add(Box.createHorizontalStrut(20));

box.add(new JSeparator(JSeparator.VERTICAL));

box.add(Box.createHorizontalStrut(20));

box.add(box2);

JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, box, new JScrollPane(result));

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(1,3));

panel.add(new JLabel("作者:QSP 2005 年 10 月"));

panel.add(time);

panel.add(progbar);

JLabel title = new JLabel("学生信息录入系统", new ImageIcon("mouse.gif"), JLabel.CENTER);

title.setHorizontalTextPosition(JLabel.LEFT);

title.setForeground(Color.BLUE);

title.setFont(new Font("黑体", Font.BOLD, 36));

Container con = getContentPane();

con.add(title, "North");

con.add(split, "Center");

con.add(panel, "South");

}

public void actionPerformed(ActionEvent evt)

{

if (evt.getSource() == selectColor)

{

Color c = JColorChooser.showDialog(this, "请选择你所喜欢的颜色", color.getBackground());

color.setBackground(c);

}

if (evt.getSource() == confirm)

{

String nameStr = name.getText();

if (nameStr.equals(""))

{

JOptionPane.showMessageDialog(this, "姓名不能为空!");

name.requestFocus();

return;

}

int ret = JOptionPane.showConfirmDialog(this, "确认信息是正确的吗?");

if (ret != JOptionPane.YES_OPTION) return;

StringBuffer courses = new StringBuffer();

Object[] selectedCourses = course.getSelectedValues();

for (int i = 0 ; i selectedCourses.length ; i++)

{

courses.append((String)selectedCourses[i]);

if (i selectedCourses.length - 1)

{

courses.append(",");

}

}

String student =

nameStr + "," +

"密码(" + new String(password.getPassword()) + ")," +

(male.isSelected() ? "男," : "女,") +

(party.isSelected() ? "党员," : "") +

"现年" + age.getValue() + "岁," +

"喜欢" + color.getBackground() + "颜色," +

"可得到加分" + addition.getValue() + "," +

"在" + department.getSelectedItem() + "学习," ;

+"选修的课程有:" + courses + "\n";

result.append(student);

}

if (evt.getSource() == save)

{

JFileChooser fc = new JFileChooser();

if (JFileChooser.APPROVE_OPTION == fc.showSaveDialog(this))

{

try

{

File f = fc.getSelectedFile();

FileWriter fw = new FileWriter(f);

fw.write(result.getText());

fw.close();

}

catch (IOException e)

{

JOptionPane.showMessageDialog(this, "文件 IO 异常!");

}

}

}

}

public static void main(String[] args)

{

new Example0701_Components();

}

}

关于java程序设计300例和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。