
import javautilScanner;
public class Input {
public static void main(String[] args) {
Scanner sc = new Scanner(Systemin);
String str = "";
Systemoutprintln("请输入,以quit退出");
while(true){
str = scnext();
if("quit"equals(strtrim()toLowerCase())){
break;
}else{
Systemoutprintln("继续输入!");
}
}
Systemoutprintln("应用程序结束!");
}
}
/
学生类
@author Shurrik
/
public class Student {
private String sid;
private String sname;
private int sage;
private char gender;
private String specialty;
private int grade;
public Student(String sid, String sname, int sage, char gender,
String specialty, int grade) {
super();
thissid = sid;
thissname = sname;
thissage = sage;
thisgender = gender;
thisspecialty = specialty;
thisgrade = grade;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
thissid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
thissname = sname;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
thissage = sage;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
thisgender = gender;
}
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
thisspecialty = specialty;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
thisgrade = grade;
}
}
----------------------偶是分割线------------------------
import javaioBufferedReader;
import javaioBufferedWriter;
import javaioFile;
import javaioFileNotFoundException;
import javaioFileReader;
import javaioFileWriter;
import javaioIOException;
/
学生管理者类
@author Shurrik
/
public class StudentManager {
private String filePath;
private Student stu;
public StudentManager(String filePath,Student stu) {
thisfilePath = filePath;
thisstu = stu;
}
/
按学号读取并返回学生信息
@param sid 学号
@return 学生信息
/
public String loadStudentInfo(String sid) {
File StudentInfoFile = new File(thisfilePath+"\\"+sid+"txt");
String StudentInfo = "";
try {
FileReader fr = new FileReader(StudentInfoFile);
BufferedReader reader = new BufferedReader(fr);
String line = "";
while((line = readerreadLine()) != null){
StudentInfo += line+"\r\n";
}
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
return StudentInfo;
}
/
保存学生信息以文本文档的形式存入硬盘中
/
public void saveStudentInfo() {
File StudentInfoFile = new File(thisfilePath+"\\"+stugetSid()+"txt");
try {
FileWriter fw = new FileWriter(StudentInfoFile);
BufferedWriter writer = new BufferedWriter(fw);
writerwrite("学号:"+stugetSid()+"\r\n");
writerwrite("姓名:"+stugetSname()+"\r\n");
writerwrite("年龄:"+stugetSage()+"\r\n");
writerwrite("性别:"+stugetGender()+"\r\n");
writerwrite("专业:"+stugetSpecialty()+"\r\n");
writerwrite("成绩:"+stugetGrade());
writerclose();
fwclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
----------------------偶是分割线------------------------
import javaioFile;
/
测试类
@author Shurrik
/
public class Test {
public static void main(String[] args) {
Student stu = new Student("S2010001", "Jack", 21, '男', "Java", 94);
String filePath = "D:\\StudentInfo";
File folder = new File(filePath);
if(!folderexists()){
foldermkdirs();
}
StudentManager manager = new StudentManager(filePath, stu);
managersaveStudentInfo();
String StudentInfo = managerloadStudentInfo("S2010001");
Systemoutprintln(StudentInfo);
}
}
基本满足了你的要求,其他功能很容易扩展,自己写写看吧。
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJTextArea;
import javaxswingJTextField;
import javaxswingWindowConstants;
public class SushuPanel extends javaxswingJPanel {
private JTextArea output;
private JTextField input;
private JButton ok;
private JLabel jLabel1;
/
Auto-generated main method to display this
JPanel inside a new JFrame
/
public static void main(String[] args) {
JFrame frame = new JFrame();
framegetContentPane()add(new SushuPanel());
framesetDefaultCloseOperation(WindowConstantsDISPOSE_ON_CLOSE);
framepack();
framesetVisible(true);
}
public SushuPanel() {
super();
initGUI();
}
private void initGUI() {
try {
thissetPreferredSize(new javaawtDimension(400, 197));
thissetLayout(null);
thissetBackground(new javaawtColor(148,179,77));
{
output = new JTextArea();
thisadd(output);
outputsetBounds(12, 12, 376, 127);
}
{
input = new JTextField();
thisadd(input);
inputsetBounds(72, 151, 163, 22);
}
{
jLabel1 = new JLabel();
thisadd(jLabel1);
jLabel1setText("请输入:");
jLabel1setBounds(22, 155, 61, 15);
}
{
ok = new JButton();
thisadd(ok);
oksetText("判断并输出");
oksetBounds(255, 151, 133, 22);
okaddActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String info="";
try{
String text=inputgetText();
if(textequals(""))return;
int i=IntegerparseInt(text);
int s=(int) Mathsqrt(i);
boolean issushu=true;
for(;s>1;s--){
if(i%s==0){
issushu=false;
break;
}
}
info=text+(issushu" 为素数":"不是素数");
textreplace(""," ");
info+="\n";
info+=textreplace(""," ")trim();
}catch(NumberFormatException ex){
info="对不起,请输入数字";
}
outputsetText(info);
}});
}
} catch (Exception e) {
eprintStackTrace();
}
}
}
Application 程序如下:
public class Welcome {
public static void main(String[] args)
{
Systemoutprintln("欢迎来到java编程世界");
}
}
Applet 程序如下:
import javaawtContainer;
import javaxswingJLabel;
import javaxswingJApplet;
import javaxswingJButton;
import javaxswingJPanel;
public class WelcomeApplet extends JApplet
{
public void init()
{
Container con = getContentPane();
JPanel panel = new JPanel();
JLabel label = new JLabel("欢迎来到java编程世界");
paneladd(label);
JButton okButton = new JButton("OK");
paneladd(okButton);
conadd(panel);
}
}
把源文件编译成class文件,再创建一个HTML文件,代码如下:
<applet code="WelcomeAppletclass" width=300 height=100>
</applet>
//:SaveContentsjava
import javaawtevent;
import javaio;
import javaxswing;
public class SaveContents extends JFrame implements ActionListener{
public static void main(String[] args) {new SaveContents()setVisible(true);}
private JTextField cont;
private JButton save;
private String filePath="c:/conttxt";//文件的路径
public SaveContents(){
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
thisgetContentPane()setLayout(null);
thissetSize(205,130);
thissetLocationRelativeTo(null);
cont = new JTextField();
contsetBounds(10,10,180,20);
add(cont);
save=new JButton("Save");
savesetBounds(110,50,78,22);
add(save);
saveaddActionListener(this);
}
public void actionPerformed(ActionEvent e) {
try {save();} catch (Exception e1) {}
}
//保存内容
private void save() throws Exception{
File f = new File(filePath);
FileWriter fw = new FileWriter(f);
fwwrite(contgetText());
fwflush();
fwclose();
}
}
import javaio;
import javautil;
import javatext;
class CreateFile{
static public void main(String[] str) throws IOException{
File root=new File("D:/");
SimpleDateFormat mat=new SimpleDateFormat
("yyyy-MM-dd hh:mm:ss");
for(int i=0;i<101010;i++){
String name=StringvalueOf((int)Mathrandom()123456)+"txt";
File temp=new File(root,name);
tempcreateNewFile();
PrintWriter p=new PrintWriter(temp);
pprintln(matformat(new Date()));
pclose();
}
}
}
新建类CloseFramejava,代码如下:
import javaawtBorderLayout;
import javaawtFlowLayout;
import javaawteventActionEvent;
import javaawteventActionListener;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJMenu;
import javaxswingJMenuBar;
import javaxswingJMenuItem;
import javaxswingJPanel;
public class CloseFrame extends JFrame implements ActionListener {
JMenuBar menu;
JMenu file;
JMenuItem closeMenu;
public CloseFrame() {
menu = new JMenuBar();
file = new JMenu("文件");
closeMenu = new JMenuItem("关闭");
closeMenuaddActionListener(this);
JButton closeButton = new JButton(" 关闭 ");
closeButtonaddActionListener(this);
JPanel closePanel = new JPanel();
closePanelsetLayout(new FlowLayout());
closePaneladd(closeButton);
thisadd(closePanel, BorderLayoutCENTER);
thisadd(menu, BorderLayoutNORTH);
menuadd(file);
fileadd(closeMenu);
thissetBounds(200, 100, 200, 120);
thissetVisible(true);
thissetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Systemexit(0);
}
public static void main(String[] args) {
new CloseFrame();
}
}
运行结果如下:
以上就是关于编写一个Java Application程序全部的内容,包括:编写一个Java Application程序、求作业 编写一个图形界面的Java Application程序、编写一个JAVA Application程序,等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)