java源代码可用的java源代码小程序
java源代码 时间:2021-08-05 阅读:(
)
求JAVA源代码!!紧急~~~
只能给你第一个:
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("打开");
// 快捷键设置
elerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_O,
InputEvent.CTRL_MASK));
menuSave = new JMenuItem("保存");
elerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menuSaveAs = new JMenuItem("另存为");
menuClose = new JMenuItem("关闭");
elerator(
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("剪切");
elerator(
KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));
menuCopy = new JMenuItem("复制");
elerator(
KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
menuPaste = new JMenuItem("粘贴");
elerator(
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,
"程序名称:
JNotePad
" +
"程序设计:
???
" +
"简介:
一个简单的文字编辑器
",
"关于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();
}
}在线等一个java程序源代码 急用!!!
第一题
import java.util.Random;
import java.util.Scanner;
public class Guess{
public static void main(String[] args) {
int rightNum = new Random().nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
int input = 0;
do{
System.out.print("清猜数字(1-100)!");
input = scanner.nextInt();
if(input > rightNum){
System.out.println("猜大了!");
}
else if(input < rightNum){
System.out.println("猜小了!");
}
}while(input != rightNum);
System.out.println("猜对了" + rightNum);
}
}
第二题
import java.util.* ;
public class A{
public static void main(String args[]){
int i,j,k,temp;
int a[][]=new int[2][3];
a[0][0]=(int)(100*Math.random());
a[0][1]=(int)(100*Math.random());
a[0][2]=(int)(100*Math.random());
a[1][0]=(int)(100*Math.random());
a[1][1]=(int)(100*Math.random());
a[1][2]=(int)(100*Math.random());
for(j=0;j<3;j++)
System.out.println("a[0]["+j+"]="+a[0][j]);
System.out.println(" ");
for(j=0;j<3;j++)
System.out.println("a[1]["+j+"]="+a[1][j]);
System.out.println(" ");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
for(k=j;k<2;k++){
if(a[i][j]>a[i][k+1]){
temp=a[i][j];
a[i][j]=a[i][k+1];
a[i][k+1]=temp;
}
}
}
}
System.out.println("第一行按从小到大排列:");
for(j=0;j<3;j++){
System.out.println("a[0]["+j+"]="+a[0][j]);
}
System.out.println("第二行按从小到大排列:");
for(j=0;j<3;j++)
System.out.println("a[1]["+j+"]=" +a[1][j]);
}
}
春春??还不快采纳嘛用JAVA 编程的源代码
接口的方法都是空方法,代码很简单的:
学费,工资,应该都是double类型。
---------------------------------------
public interface StudentManageInterface{
void setFee(double fee);
double getFee();
}
pubilc interface TeacherManageInterface{
void setPay(double pay);
double getPay();
}可用的java源代码小程序
我才写的一个简易计算器代码,能运行。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorTest {
public static void main(String[] args) {
CalculatorFrame frame = new CalculatorFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//frame.dispose();
}
}
class CalculatorFrame extends JFrame{
public CalculatorFrame(){
//获取屏幕尺寸
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenheight = screenSize.height;
//使框架定位于屏幕正中
setBounds(screenWidth/4,screenheight/4,screenWidth /2,screenheight/2 );
setTitle("kangta's Calculator");
CalculatorPanel panel = new CalculatorPanel();
add(panel);
pack();
/*colorPanel colorPanel = new colorPanel();
panel.setBackground(Color.green);
add(colorPanel);
*/
}
}
/*class colorPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
}
}
*/
//生成一个计算器并显示结果的面板
class CalculatorPanel extends JPanel{
public CalculatorPanel(){
setLayout(new BorderLayout());
result = 0;
lastCommand = "=";
start = true;
//添加显示按钮
display = new JButton("0");
display.setEnabled(false);
add(display,BorderLayout.NORTH);
ActionListener insert = new InsertAction();
mand = new CommandAction();
//添加4x4按钮
panel1 = new JPanel();
panel1.setLayout(new GridLayout(4,4));
addButton("7",insert);
addButton("8",insert);
addButton("9",insert);
addButton("/"mand);
addButton("4",insert);
addButton("5",insert);
addButton("6",insert);
addButton("*"mand);
addButton("1",insert);
addButton("2",insert);
addButton("3",insert);
addButton("-"mand);
addButton("0",insert);
addButton(".",insert);
addButton("="mand);
addButton("+"mand);
add(panel1,BorderLayout.CENTER);
}
//将按钮添加到面板中央
private void addButton(String label,ActionListener listener){
JButton button = new JButton(label);
button.addActionListener(listener);
panel1.add(button);
}
private class InsertAction implements ActionListener{
public void actionPerformed(ActionEvent event){
String input = event.getActionCommand();
if(start){
display.setText("");
start = false;
}
display.setText(display.getText() + input);
}
}
private class CommandAction implements ActionListener{
public void actionPerformed(ActionEvent event){
mand = event.getActionCommand();
if(start){
mand.equals("-")){
mand);
start = false;
}
else
lastCommand =mand;
}
else{
calculate(Double.parseDouble(display.getText()));
lastCommand =mand;
start = true;
}
}
}
public void calculate(double x){
if(lastCommand.equals("+")) result += x;
else if(lastCommand.equals("-")) result -=x;
else if(lastCommand.equals("*")) result *=x;
else if(lastCommand.equals("/")) result /=x;
else if(lastCommand.equals("=")) result =x;
display.setText(""+result);
}
private JButton display;
private JPanel panel1;
private double result;
private String lastCommand;
private boolean start;
}
鲨鱼机房(Sharktech)我们也叫它SK机房,是一家成立于2003年的老牌国外主机商,提供的产品包括独立服务器租用、VPS主机等,自营机房在美国洛杉矶、丹佛、芝加哥和荷兰阿姆斯特丹等,主打高防产品,独立服务器免费提供60Gbps/48Mpps攻击防御。机房提供1-10Gbps带宽不限流量服务器,最低丹佛/荷兰机房每月49美元起,洛杉矶机房最低59美元/月起。下面列出部分促销机型的配置信息。机房...
ihostart怎么样?ihostart是一家国外新商家,主要提供cPanel主机、KVM VPS、大硬盘存储VPS和独立服务器,数据中心位于罗马尼亚,官方明确说明无视DMCA,对版权内容较为宽松。有需要的可以关注一下。目前,iHostART给出了罗马尼亚vps的优惠信息,罗马尼亚VPS无视DMCA、抗投诉vps/2核4G内存/40GB SSD/100M端口月流量2TB,€20/年。点击直达:ih...
俄罗斯vps云服务器商家推荐!俄罗斯VPS,也叫毛子主机(毛子vps),因为俄罗斯离中国大陆比较近,所以俄罗斯VPS的延迟会比较低,国内用户也不少,例如新西伯利亚机房和莫斯科机房都是比较热门的俄罗斯机房。这里为大家整理推荐一些好用的俄罗斯VPS云服务器,这里主要推荐这三家:justhost、ruvds、justg等俄罗斯vps主机,方便大家对比购买适合自己的俄罗斯VPS。一、俄罗斯VPS介绍俄罗斯...
java源代码为你推荐
oracle11g下载Oracle哪里有下载.免费的版本呢免费qq号有免费的QQ号和密码可以用的?provisionedNIST的云计算定义豆瓣fm电台豆瓣电台怎么听自己喜欢歌手的歌电视蚂蚁电视蚂蚁是不是不能用了?我在国外该怎样看奥运?电子听诊器听诊器的原理wizardryHogwarts for pikeys是什么意思???实数的定义实数的定义微店是什么开微店和开淘宝店有什么区别吗第五人格抄袭那个模仿第五人格的游戏叫什么
虚拟主机试用30天 快速域名备案 buyvm 5折 免备案空间 警告本网站 云图标 国内加速器 asp免费空间申请 国外代理服务器地址 phpmyadmin配置 中国网通测速 搜索引擎提交入口 江苏双线服务器 smtp服务器地址 新加坡空间 lamp的音标 114dns 成都主机托管 中国联通宽带测试 更多