大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
只能给你第一个:
成都创新互联为企业级客户提高一站式互联网+设计服务,主要包括成都网站设计、网站制作、APP应用开发、微信小程序开发、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JNotePadUI extends JFrame {
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuClose;
private JMenu editMenu;
private JMenuItem menuCut;
private JMenuItem menuCopy;
private JMenuItem menuPaste;
private JMenuItem menuAbout;
private JTextArea textArea;
private JLabel stateBar;
private JFileChooser fileChooser;
private JPopupMenu popUpMenu;
public JNotePadUI() {
super("新建文本文件");
setUpUIComponent();
setUpEventListener();
setVisible(true);
}
private void setUpUIComponent() {
setSize(640, 480);
// 菜单栏
JMenuBar menuBar = new JMenuBar();
// 设置「文件」菜单
JMenu fileMenu = new JMenu("文件");
menuOpen = new JMenuItem("打开");
// 快捷键设置
menuOpen.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_O,
InputEvent.CTRL_MASK));
menuSave = new JMenuItem("保存");
menuSave.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menuSaveAs = new JMenuItem("另存为");
menuClose = new JMenuItem("关闭");
menuClose.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_Q,
InputEvent.CTRL_MASK));
fileMenu.add(menuOpen);
fileMenu.addSeparator(); // 分隔线
fileMenu.add(menuSave);
fileMenu.add(menuSaveAs);
fileMenu.addSeparator(); // 分隔线
fileMenu.add(menuClose);
// 设置「编辑」菜单
JMenu editMenu = new JMenu("编辑");
menuCut = new JMenuItem("剪切");
menuCut.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));
menuCopy = new JMenuItem("复制");
menuCopy.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
menuPaste = new JMenuItem("粘贴");
menuPaste.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK));
editMenu.add(menuCut);
editMenu.add(menuCopy);
editMenu.add(menuPaste);
// 设置「关于」菜单
JMenu aboutMenu = new JMenu("关于");
menuAbout = new JMenuItem("关于JNotePad");
aboutMenu.add(menuAbout);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(aboutMenu);
setJMenuBar(menuBar);
// 文字编辑区域
textArea = new JTextArea();
textArea.setFont(new Font("宋体", Font.PLAIN, 16));
textArea.setLineWrap(true);
JScrollPane panel = new JScrollPane(textArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Container contentPane = getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
// 状态栏
stateBar = new JLabel("未修改");
stateBar.setHorizontalAlignment(SwingConstants.LEFT);
stateBar.setBorder(
BorderFactory.createEtchedBorder());
contentPane.add(stateBar, BorderLayout.SOUTH);
popUpMenu = editMenu.getPopupMenu();
fileChooser = new JFileChooser();
}
private void setUpEventListener() {
// 按下窗口关闭钮事件处理
addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
closeFile();
}
}
);
// 菜单 - 打开
menuOpen.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
}
);
// 菜单 - 保存
menuSave.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
}
);
// 菜单 - 另存为
menuSaveAs.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFileAs();
}
}
);
// 菜单 - 关闭文件
menuClose.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
closeFile();
}
}
);
// 菜单 - 剪切
menuCut.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
cut();
}
}
);
// 菜单 - 复制
menuCopy.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
}
);
// 菜单 - 粘贴
menuPaste.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
paste();
}
}
);
// 菜单 - 关于
menuAbout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 显示对话框
JOptionPane.showOptionDialog(null,
"程序名称:\n JNotePad \n" +
"程序设计:\n ???\n" +
"简介:\n 一个简单的文字编辑器\n",
"关于JNotePad",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, null, null);
}
}
);
// 编辑区键盘事件
textArea.addKeyListener(
new KeyAdapter() {
public void keyTyped(KeyEvent e) {
processTextArea();
}
}
);
// 编辑区鼠标事件
textArea.addMouseListener(
new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON3)
popUpMenu.show(editMenu, e.getX(), e.getY());
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1)
popUpMenu.setVisible(false);
}
}
);
}
private void openFile() {
if(isCurrentFileSaved()) { // 文件是否为保存状态
open(); // 打开
}
else {
// 显示对话框
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
// 确认文件保存
case JOptionPane.YES_OPTION:
saveFile(); // 保存文件
break;
// 放弃文件保存
case JOptionPane.NO_OPTION:
open();
break;
}
}
}
private boolean isCurrentFileSaved() {
if(stateBar.getText().equals("未修改")) {
return true;
}
else {
return false;
}
}
private void open() {
// fileChooser 是 JFileChooser 的实例
// 显示文件选取的对话框
int option = fileChooser.showDialog(null, null);
// 使用者按下确认键
if(option == JFileChooser.APPROVE_OPTION) {
/*
TODO: 添加读取文件的代码
*/
}
}
private void saveFile() {
/*
TODO: 添加保存文件的代码
*/
}
private void saveFileAs() {
/*
TODO: 添加另存为的代码
*/
}
private void closeFile() {
// 是否已保存文件
if(isCurrentFileSaved()) {
// 释放窗口资源,而后关闭程序
dispose();
}
else {
int option = JOptionPane.showConfirmDialog(
null, "文件已修改,是否保存?",
"保存文件?", JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null);
switch(option) {
case JOptionPane.YES_OPTION:
saveFile();
break;
case JOptionPane.NO_OPTION:
dispose();
}
}
}
private void cut() {
textArea.cut();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
private void copy() {
textArea.copy();
popUpMenu.setVisible(false);
}
private void paste() {
textArea.paste();
stateBar.setText("已修改");
popUpMenu.setVisible(false);
}
private void processTextArea() {
stateBar.setText("已修改");
}
public static void main(String[] args) {
new JNotePadUI();
}
}
上面 wuzhikun12同学写的不错,但我想还不能运行,并且还不太完善。我给个能运行的:(注意:文件名为:Test.java)
//要实现对象间的比较,就必须实现Comparable接口,它里面有个compareTo方法
//Comparable最好使用泛型,这样,无论是速度还是代码量都会减少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //学号
private String studentName; //姓名
private double englishScore; //英语成绩
private double computerScore; //计算机成绩
private double mathScore; //数学成绩
private double totalScore; //总成绩
//空构造函数
public Student() {}
//构造函数
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this点抗 puterScore = computerScore;
this.mathScore = mathScore;
}
//计算总成绩
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//计算评测成绩
public double testScore() {
return sum()/3;
}
//实现compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重写toString方法
public String toString(){
return "学号:"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英语成绩:"+this.getEnglishScore()+" 数学成绩:"+this.getMathScore()+" 计算机成绩:"+this.getComputerScore()+" 总成绩:"+this.getTotalScore();
}
//重写equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照现实来说,比较是不是同一个学生,应该只是看他的学号是不是相同
return true;
} else {
return false;
}
}
/*以下为get和set方法,我个人认为,totalScore的set的方法没必要要,因为它是由其它成绩计算出来的
在set方法中,没设置一次值,调用一次sum方法,即重新计算总成绩
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this点抗 puterScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子类学习委员类的实现
class StudentXW extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的构造函数
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子类班长类的实现
class StudentBZ extends Student {
//重写父类Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的构造函数
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//测试类
public class Test {
public static void main(String[] args) {
//生成若干个student类、StudentXW类、StudentBZ类
Student student1 = new Student("s001","张三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"学生的评测成绩为:"+ avgScore+"分");
}
}
}
运行结果为:
张三学生的评测成绩为:69.66666666666667分
李四学生的评测成绩为:80.5分
王五学生的评测成绩为:78.0分
李六学生的评测成绩为:98.5分
朱漆学生的评测成绩为:60.03333333333333分
//这是个聊天程序, 在ECLIPSE 运行 Client.java 就可以了。 连接是:localhost
//Server 代码,
package message;
import java.io.*;
import java点虐 .*;
import java.util.*;
public class Server {
public static void main(String[] args) throws Exception{
System.out.print("Server");
ServerSocket socket=new ServerSocket(8888);
Vector v=new Vector();
while(true){
Socket sk=socket.accept();
DataInputStream in=new DataInputStream(sk.getInputStream());
DataOutputStream out=new DataOutputStream(sk.getOutputStream());
v.add(sk);
new ServerThread(in,v).start();
}
}
}
//ServerThread.java 代码
package message;
import java点虐 .*;
import java.io.*;
import java.util.*;
public class ServerThread extends Thread{
DataInputStream in;
Vector all;
public ServerThread(DataInputStream in,Vector v){
this.in=in;
this.all=v;
}
public void run()
{
while(true)
{
try{
String s1=in.readUTF();
for(int i=0;iall.size();i++)
{
Object obj=all.get(i);
Socket socket=(Socket)obj;
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
out.writeUTF(s1);
System.out.print(i);
out.flush();
}
System.out.print("Message send over!");
}catch(Exception e){e.printStackTrace();};
}
}
}
//ClientFrame.java 代码
package message;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java点虐 .*;
import java.io.*;
public class ClientFrame extends JFrame implements ActionListener{
JButton b1=new JButton ("SendMessage");
JButton b2=new JButton("Link Server");
JTextField t1=new JTextField(20);
JTextField t2=new JTextField(20);
JLabel l=new JLabel("输入服务器名字:");
JTextArea area=new JTextArea(10,20);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
Socket socket;
public ClientFrame()
{
this.getContentPane().add(p1);
p2.add(new JScrollPane(area));
p3.add(t1);
p3.add(b1);
p4.add(l);
p4.add(t2);
p4.add(b2);
p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
p1.setLayout(new BorderLayout());
p1.add("North",p2);
p1.add("Center",p3);
p1.add("South",p4);
b1.addActionListener(this);
b2.addActionListener(this);
this.pack();
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Link Server"))
{
try{
socket=new Socket(t2.getText(),8888);
b2.setEnabled(false);
JOptionPane.showMessageDialog(this, "Connection Success");
DataInputStream in=new DataInputStream(socket.getInputStream());
new ClientThread(in,area).start();
}
catch(Exception e1){
JOptionPane.showMessageDialog(this, "Connection Error");
e1.printStackTrace();};
}
else if(e.getActionCommand().equals("SendMessage"))
{
try{
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
out.writeUTF(t1.getText());
t1.setText("");
}catch(Exception e1){e1.printStackTrace();};
}
}
}
//ClientThread.java 代码
package message;
import java点虐 .*;
import java.io.*;
import javax.swing.*;
public class ClientThread extends Thread {
DataInputStream in;
JTextArea area;
public ClientThread(DataInputStream in,JTextArea area){
this.in=in;
this.area=area;
}
public void run()
{
while(true){
try{
String s=in.readUTF();
area.append(s);
}
catch(Exception e){e.printStackTrace();};
}
}
}
//Client.java代码
package message;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
new ClientFrame();
}
}
// 每段代码都是个类,不要弄在一个文件里。 运行 Client.java
good luck to you!
给你介绍4种排序方法及源码,供参考
1.冒泡排序
主要思路: 从前往后依次交换两个相邻的元素,大的交换到后面,这样每次大的数据就到后面,每一次遍历,最大的数据到达最后面,时间复杂度是O(n^2)。
public static void bubbleSort(int[] arr){
for(int i =0; i arr.length - 1; i++){
for(int j=0; j arr.length-1; j++){
if(arr[j] arr[j+1]){
arr[j] = arr[j]^arr[j+1];
arr[j+1] = arr[j]^arr[j+1];
arr[j] = arr[j]^arr[j+1];
}
}
}
}
2.选择排序
主要思路:每次遍历序列,从中选取最小的元素放到最前面,n次选择后,前面就都是最小元素的排列了,时间复杂度是O(n^2)。
public static void selectSort(int[] arr){
for(int i = 0; i arr.length -1; i++){
for(int j = i+1; j arr.length; j++){
if(arr[j] arr[i]){
arr[j] = arr[j]^arr[i];
arr[i] = arr[j]^arr[i];
arr[j] = arr[j]^arr[i];
}
}
}
}
3.插入排序
主要思路:使用了两层嵌套循环,逐个处理待排序的记录。每个记录与前面已经排好序的记录序列进行比较,并将其插入到合适的位置,时间复杂度是O(n^2)。
public static void insertionSort(int[] arr){
int j;
for(int p = 1; p arr.length; p++){
int temp = arr[p]; //保存要插入的数据
//将无序中的数和前面有序的数据相比,将比它大的数,向后移动
for(j=p; j0 temp arr[j-1]; j--){
arr[j] = arr[j-1];
}
//正确的位置设置成保存的数据
arr[j] = temp;
}
}
4.希尔排序
主要思路:用步长分组,每个分组进行插入排序,再慢慢减小步长,当步长为1的时候完成一次插入排序, 希尔排序的时间复杂度是:O(nlogn)~O(n2),平均时间复杂度大致是O(n^1.5)
public static void shellSort(int[] arr){
int j ;
for(int gap = arr.length/2; gap 0 ; gap/=2){
for(int i = gap; i arr.length; i++){
int temp = arr[i];
for(j = i; j=gap temparr[j-gap]; j-=gap){
arr[j] = arr[j-gap];
}
arr[j] = temp;
}
}
}