Java 是一種編程語言,它本身的作用是通過編寫應(yīng)用程序,幫助人們解決日常工作、生活和學習遇到的問題。
成都創(chuàng)新互聯(lián)專注于珠暉企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城系統(tǒng)網(wǎng)站開發(fā)。珠暉網(wǎng)站建設(shè)公司,為珠暉等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
一些常見的java功能代碼。如復(fù)制文件夾及文件到指定目錄,遍歷指定盤符所有文件,檢索字符串是否符合指定要求。
import?java.util.Scanner;
import?java.awt.*;
import?java.awt.event.*;
public?class?Test?extends?WindowAdapter?{
Panel?p1?=?new?Panel();
Panel?p2?=?new?Panel();
Panel?p3?=?new?Panel();
TextField?txt;
private?Button[]?b?=?new?Button[17];
private?String?ss[]?=?{?"7",?"8",?"9",?"+",?"4",?"5",?"6",?"-",?"1",?"2",
"3",?"*",?"clear",?"0",?"=",?"/",?"close"?};
static?double?a;
static?String?s,?str;//?定義變量?創(chuàng)建對像
public?static?void?main(String?args[])?{
(new?Test()).frame();
}
public?void?frame()?{
Frame?fm?=?new?Frame("簡單計算器");
for?(int?i?=?0;?i?=?16;?i++)?{
b[i]?=?new?Button(ss[i]);
}
for?(int?i?=?0;?i?=?15;?i++)?{
p2.add(b[i]);
}?//?創(chuàng)建按鈕?并添加到P2
b[16].setBackground(Color.yellow);
txt?=?new?TextField(15);
txt.setEditable(false);
for?(int?i?=?0;?i?=?16;?i++)?{
b[i].addActionListener(new?buttonlistener());//?添加監(jiān)聽器
}
b[16].addActionListener(new?close());
fm.addWindowListener(this);
fm.setBackground(Color.red);
p1.setLayout(new?BorderLayout());
p1.add(txt,?"North");
p2.setLayout(new?GridLayout(4,?4));
p3.setLayout(new?BorderLayout());
p3.add(b[16]);
fm.add(p1,?"North");
fm.add(p2,?"Center");
fm.add(p3,?"South");
fm.pack();
fm.setVisible(true);//?都是些窗中設(shè)置?添加相關(guān)組件和監(jiān)聽器
}
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);//?退出系統(tǒng)
}
class?buttonlistener?implements?ActionListener?{//?編寫監(jiān)聽器事件?通過按鍵得出給果
public?void?actionPerformed(ActionEvent?e)?{
Button?btn?=?(Button)?e.getSource();
if?(btn.getLabel()?==?"=")?{
jisuan();
str?=?String.valueOf(a);
txt.setText(str);
s?=?"";
}?else?if?(btn.getLabel()?==?"+")?{
jisuan();
txt.setText("");
s?=?"+";
}?else?if?(btn.getLabel()?==?"-")?{
jisuan();
txt.setText("");
s?=?"-";
}?else?if?(btn.getLabel()?==?"/")?{
jisuan();
txt.setText("");
s?=?"/";
}?else?if?(btn.getLabel()?==?"*")?{
jisuan();
txt.setText("");
s?=?"*";
}?else?{
txt.setText(txt.getText()?+?btn.getLabel());
if?(btn.getLabel()?==?"clear")
txt.setText("");
}
}
public?void?jisuan()?{//?編寫具體計算方法
if?(s?==?"+")
a?+=?Double.parseDouble(txt.getText());
else?if?(s?==?"-")
a?-=?Double.parseDouble(txt.getText());
else?if?(s?==?"*")
a?*=?Double.parseDouble(txt.getText());
else?if?(s?==?"/")
a?/=?Double.parseDouble(txt.getText());
else
a?=?Double.parseDouble(txt.getText());
}
}
}
class?close?implements?ActionListener?{//?退出
public?void?actionPerformed(ActionEvent?e)?{
System.exit(0);
}
}
NewPhone類
package?com.baidu.question;
public?class?NewPhone?extends?Phone?{
private?boolean?mute?=?true;
@Override
public?void?call()?{
if(mute){
super.call();
}else{
System.out.println("語音已關(guān)閉");
}
}
//這里是直接設(shè)置
public?void?setMute(boolean?mute){
this.mute=mute;
}
//擔心你的題目是要求兩種方法,寫的第二種,下面兩個方法負責開關(guān)
public?void?openMute(){
this.mute=true;
/*
*?也可以這樣寫
*?setMute(true);
*?下邊的方法一樣
*?*/
}
public?void?closeMute(){
this.mute?=?false;
}
}
Phone類
package?com.baidu.question;
public?class?Phone?{
public?void?call(){
System.out.println("打電話");
}
}
測試類
package?com.baidu.question;
public?class?PhoneTest?{
public?static?void?main(String[]?args)?{
Phone?phone?=?new?Phone();
phone.call();
NewPhone?newPhone?=?new?NewPhone();
newPhone.call();
newPhone.setMute(false);
newPhone.call();
newPhone.openMute();
newPhone.call();
newPhone.closeMute();
newPhone.call();
}
}
測試結(jié)果
打電話
打電話
語音已關(guān)閉
打電話
語音已關(guān)閉
很簡單的應(yīng)用,為了節(jié)省字數(shù),代碼注釋我就不加了
首先是顯示層,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("您輸入的密碼不合法,請輸入數(shù)字!");
}
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);
}
}
不知道分層設(shè)計思想是不是我想的這樣
網(wǎng)頁題目:java功能代碼 java功能代碼在哪
當前鏈接:http://jinyejixie.com/article16/doscogg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站建設(shè)、建站公司、網(wǎng)站制作、關(guān)鍵詞優(yōu)化、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)