java代码示例importjava.awt.*;importjava.awt.event.*;classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4;Buttonbutton1,button2,button3,button4,button5;TextAreatext;Panelpanel1,panel2;staticfloatsum=0.0f;ShopFrame(Strings){super(s);setLayout(newBorderLayout());label1=newLabel("面纸:3元",Label.LEFT);label2=newLabel("钢笔:5元",Label.LEFT);label3=newLabel("书:10元",Label.LEFT);label4=newLabel("袜子:8元",Label.LEFT);button1=newButton("加入购物车");button2=newButton("加入购物车");button3=newButton("加入购物车");button4=newButton("加入购物车");button5=newButton("查看购物车");text=newTextArea("商品有:" "\n",5,10);text.setEditable(false);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);panel1=newPanel();panel2=newPanel();panel1.add(label1);panel1.add(button1);panel1.add(label2);panel1.add(button2);panel1.add(label3);panel1.add(button3);panel1.add(label4);panel1.add(button4);panel2.setLayout(newBorderLayout());panel2.add(button5,BorderLayout.NORTH);panel2.add(text,BorderLayout.SOUTH);this.add(panel1,BorderLayout.CENTER);this.add(panel2,BorderLayout.SOUTH);setBounds(100,100,350,250);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一个面纸、");sum=sum 3;}elseif(e.getSource()==button2){text.append("一只钢笔、");sum=sum 5;}elseif(e.getSource()==button3){text.append("一本书、");sum=sum 10;}elseif(e.getSource()==button4){text.append("一双袜子、");sum=sum 8;}elseif(e.getSource()==button5){text.append("\n" "总价为:" "\n" sum);}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("购物车");}}我没用Swing可能显示不出来你的效果 。不满意得话我在给你编一个 。
java例题代码import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入密码:");
try{
String pass = in.readLine();
if(pass.length()6 || pass.length()4){
System.err.println("长度错误 , 密码长度必须是4-6位");
return;
}
int password = Integer.parseInt(pass);
System.out.println("信息合法");
}catch(Exception e){
System.err.println("错误,输入的密码必须为数字");
}
}
}
求java树形目录代码范例给你一个 。一共三个类 。是个资源管理器的代码
// FileList.java
package tl.exercise.swing;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
public class FileList
extends JList {
// PathNode theNode;
FileListModel dataModel;
static final long serialVersionUID = 10;
public FileList() {
dataModel = new FileListModel();
setModel(dataModel);
this.setCellRenderer(new MyCellRenderer());
}
public void fireTreeSelectionChanged(I_fileSystem node) {
// Vector files = node.getFiles();
// theNode = node;
dataModel.setNode(node);
updateUI();
}
}
class FileListModel implements ListModel {
FileList theList;
I_fileSystem node;
char fileType = I_fileSystem.ALL;
public void setNode(I_fileSystem node) {
this.node = node;
}
public Object getElementAt(int index) {
if (node != null) {
return ((I_fileSystem) node).getChild(fileType, index);
} else {
return null;
}
}
public int getSize() {
if (node != null) {
return ((I_fileSystem) node).getChildCount(fileType);
} else {
return 0;
}
}
public void addListDataListener(ListDataListener l) {
}
public void removeListDataListener(ListDataListener l) {
}
【java典型代码范例 java代码含义】}
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
FolderNode node = (FolderNode) value;
setIcon(node.getIcon());
setText(value.toString());
setBackground(isSelected ? Color.BLUE.darker().darker() : Color.WHITE);
setForeground(isSelected ? Color.WHITE : Color.BLACK);
return this;
}
}
package tl.exercise.swing;
//JExplorer.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.border.BevelBorder;
public class JExplorer {
public static void main(String[] args) {
// JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new UI(frame));
frame.pack();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int left = (screen.width - frame.getWidth()) / 2;
int top = (screen.height - frame.getHeight()) / 2;
frame.setLocation(left, top);
frame.setVisible(true);
}
}
class UI extends JPanel {
// implements I_menuHandler{
static final long serialVersionUID = 0l;
static int LEFT_WIDTH = 200;
static int RIGHT_WIDTH = 300;
static int WINDOW_HEIGHT = 300;
JFrame frame = null;
public UI(JFrame frame) {
// EmptyBorder eb = new EmptyBorder(1,1,1,1);
this.frame = frame;
setPreferredSize(new Dimension(800, 600));
setBorder(new BevelBorder(BevelBorder.LOWERED));
setLayout(new BorderLayout());
FileList list = new FileList();
FileTree tree = new FileTree(list);
tree.setDoubleBuffered(true);
list.setDoubleBuffered(true);
JScrollPane treeView = new JScrollPane(tree);
treeView.setPreferredSize(
new Dimension(LEFT_WIDTH, WINDOW_HEIGHT));
JScrollPane listView = new JScrollPane(list);
listView.setPreferredSize(
new Dimension(RIGHT_WIDTH, WINDOW_HEIGHT));
JSplitPane pane =
new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeView,
listView);
pane.setDividerLocation(300);
pane.setDividerSize(4);
// pane.setDoubleBuffered(true);
add(pane);
}
}
package tl.exercise.swing;
//FileTree.java
/***********************************************************
*Author: Jason
*email: tl21cen@hotmail.com
* CSDN blog:
***********************************************************/
import java.awt.Component;
import java.io.File;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
public class FileTree extends JTree {
static final long serialVersionUID = 0;
private FileList theList;
public FileTree(FileList list) {
theList = list;
setModel(new FileSystemModel(new FolderNode()));
this.setCellRenderer(new FolderRenderer());
addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent tse) {
}
});
this.setSelectionRow(0);
}
public void fireValueChanged(TreeSelectionEvent tse) {
TreePath tp = tse.getNewLeadSelectionPath();
Object o = tp.getLastPathComponent();
// theList.fireTreeSelectionChanged((PathNode)o);
theList.fireTreeSelectionChanged((FolderNode) o);
}
public void fireTreeCollapsed(TreePath path) {
super.fireTreeCollapsed(path);
TreePath curpath = getSelectionPath();
if (path.isDescendant(curpath)) {
setSelectionPath(path);
}
}
public void fireTreeWillExpand(TreePath path) {
System.out.println("Path will expand is "path);
}
public void fireTreeWillCollapse(TreePath path) {
System.out.println("Path will collapse is "path);
}
class ExpansionListener implements TreeExpansionListener {
FileTree tree;
public ExpansionListener(FileTree ft) {
tree = ft;
}
public void treeCollapsed(TreeExpansionEvent tee) {
}
public void treeExpanded(TreeExpansionEvent tee) {
}
}
}
class FileSystemModel implements TreeModel {
I_fileSystem theRoot;
char fileType = I_fileSystem.DIRECTORY;
public FileSystemModel(I_fileSystem fs) {
theRoot = fs;
}
public Object getRoot() {
return theRoot;
}
public Object getChild(Object parent, int index) {
return ((I_fileSystem) parent).getChild(fileType, index);
}
public int getChildCount(Object parent) {
return ((I_fileSystem) parent).getChildCount(fileType);
}
public boolean isLeaf(Object node) {
return ((I_fileSystem) node).isLeaf(fileType);
}
public int getIndexOfChild(Object parent, Object child) {
return ((I_fileSystem) parent).getIndexOfChild(fileType, child);
}
public void valueForPathChanged(TreePath path, Object newValue) {
}
public void addTreeModelListener(TreeModelListener l) {
}
public void removeTreeModelListener(TreeModelListener l) {
}
}
interface I_fileSystem {
final public static char DIRECTORY = 'D';
final public static char FILE = 'F';
final public static char ALL = 'A';
public Icon getIcon();
public I_fileSystem getChild(char fileType, int index);
public int getChildCount(char fileType);
public boolean isLeaf(char fileType);
public int getIndexOfChild(char fileType, Object child);
}
/**
* A data model for a JTree. This model explorer windows file system directly.
*
* p
* Perhaps there is a fatal bug with this design. For speed, each of instances
* of this model contains file objects of subdirectory, up to now, there isn't
* any method to release them until program be end. I'm afraid that the memory
* would be full of if the file system is large enough and JVM memery size
* setted too small.
*
* p
* I won't pay more attention to solve it. it isn't goal of current a exercise.
*
* @author Jason
*/
class FolderNode implements I_fileSystem {
// private static FolderNode theRoot;
private static FileSystemView fsView;
private static boolean showHiden = true;;
private File theFile;
private VectorFile all = new VectorFile();
private VectorFile folder = new VectorFile();
/**
* set that whether apply hiden file.
*
* @param ifshow
*/
public void setShowHiden(boolean ifshow) {
showHiden = ifshow;
}
public Icon getIcon() {
return fsView.getSystemIcon(theFile);
}
public String toString() {
// return fsView.
return fsView.getSystemDisplayName(theFile);
}
/**
* create a root node. by default, it should be the DeskTop in window file
* system.
*
*/
public FolderNode() {
fsView = FileSystemView.getFileSystemView();
theFile = fsView.getHomeDirectory();
prepareChildren();
}
private void prepareChildren() {
File[] files = fsView.getFiles(theFile, showHiden);
for (int i = 0; ifiles.length; i) {
all.add(files[i]);
if (files[i].isDirectory()
!files[i].toString().toLowerCase().endsWith(".lnk")) {
folder.add(files[i]);
}
}
}
private FolderNode(File file) {
theFile = file;
prepareChildren();
}
public FolderNode getChild(char fileType, int index) {
if (I_fileSystem.DIRECTORY == fileType) {
return new FolderNode(folder.get(index));
} else if (I_fileSystem.ALL == fileType) {
return new FolderNode(all.get(index));
} else if (I_fileSystem.FILE == fileType) {
return null;
} else {
return null;
}
}
public int getChildCount(char fileType) {
if (I_fileSystem.DIRECTORY == fileType) {
return folder.size();
} else if (I_fileSystem.ALL == fileType) {
return all.size();
} else if (I_fileSystem.FILE == fileType) {
return -1;
} else {
return -1;
}
}
public boolean isLeaf(char fileType) {
if (I_fileSystem.DIRECTORY == fileType) {
return folder.size() == 0;
} else if (I_fileSystem.ALL == fileType) {
return all.size() == 0;
} else if (I_fileSystem.FILE == fileType) {
return true;
} else {
return true;
}
}
public int getIndexOfChild(char fileType, Object child) {
if (child instanceof FolderNode) {
if (I_fileSystem.DIRECTORY == fileType) {
return folder.indexOf(((FolderNode) child).theFile);
} else if (I_fileSystem.ALL == fileType) {
return all.indexOf(((FolderNode) child).theFile);
} else if (I_fileSystem.FILE == fileType) {
return -1;
} else {
return -1;
}
} else {
return -1;
}
}
}
class FolderRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 1L;
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
I_fileSystem node = (I_fileSystem) value;
Icon icon = node.getIcon();
setLeafIcon(icon);
setOpenIcon(icon);
setClosedIcon(icon);
return super.getTreeCellRendererComponent(tree, value, sel, expanded,
leaf, row, hasFocus);
}
}
求一个50行左右的JAVA代码,最好每行带注释 , 谢谢啦/*这个相当详细了.
程序也不算太难.而且给老师看java典型代码范例的时候效果比较好.因为有图形化界面,又实现一个比较实用java典型代码范例的功能.老师会比较高兴java典型代码范例的.
建立一个文件名为Change.java就可以编译了*/
/*
* 这个程序实现输入身高算出标准体重,输入体重,算出身高的功能
*/
import java.awt.*; //导入相关类包,这才样使用相应awt图形界面的类
import java.awt.event.*;//同上
public class Change extends Frame { //定义一个类Change, 父类是Frame(图形界面的)
Button b = new Button("互查"); //创建一个按钮的对象b,显示为"互查"
Label l1 = new Label("身高(cm)");//创建一个lable.显示身高
Label l2 = new Label("体重(kg)");//创建一个lable 显示体重
double heigth, weigth; //定义变量
double x, y; //定义变量
TextField tf1 = new TextField(null, 10);//添加Text框
TextField tf2 = new TextField(null, 10);//添加Text框
public Change() {//类的构造函数,完成初始化
super("互查表");//创建窗口,标题为互查表
setLayout(new FlowLayout(FlowLayout.LEFT));//设置布局
add(l1);//把lable 身高放到window里
add(tf1);//把Text 框 放到窗口上
add(l2); //把lable 体重放到window里
add(tf2);//Test放到窗口里
add(b);//把button放到窗口上
pack();//自动放到窗口里排列上边的组件
setVisible(true);//可以让用户看到窗口
addWindowListener(new WindowAdapter() {//如果按 X, 关闭窗口
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b.addActionListener(new ButtonListener());//添加button监听函数
}
class ButtonListener implements ActionListener {//实现click button时功能操作
public void actionPerformed(ActionEvent e) {//当click调用
if (tf1.getText()!=null) {//检查tf1 test 是否为空
try {//取异常
x = Double.parseDouble(tf1.getText());//字符转为double型
weigth = (x - 100) * 0.9;//算重量
tf2.setText(""weigth);//显示重量
} catch (NumberFormatException ex) {
tf1.setText("");//如果输入不是数字,设为空
}
}
if (tf1.getText().equals("")==true){//tf1是否为空
y = Double.parseDouble(tf2.getText());//把tf2里的文本转为double 型 的
heigth = y / 0.9100;//算身高根据重量
tf1.setText(""heigth);}//显示身高
}
}
public static void main(String[] args) {//主函数,程序入口
new Change(); //建立类Change的对象,并调用他的构造函数Change().显示窗口
}
}
关于java典型代码范例和java代码含义的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。
推荐阅读
- 路由器用手机号怎么连接,路由器和手机号怎么绑定
- 包含映泰h61mgv配什么cpu的词条
- jquery设置背景,jquery设置backgroundcolor
- 关闭vb.net窗体 关闭窗体的vb代码怎么写
- c语言中include中都有什么,c语言中include的用法
- 虚拟美颜空间软件,美颜相机虚拟背景
- 传统媒体要如何发展新媒体,新媒体环境下传统媒体该如何发展
- python余弦函数名 python怎么计算余弦值
- 包含jquery中的或的词条