在当今的软件开发领域,Java前端开发是一个热门且需求量大的岗位。掌握Java前端技术,不仅能够让你在职场中脱颖而出,还能让你在享受编程乐趣的同时,创造出令人惊叹的网页应用。本文将为你提供一系列实战案例,帮助你轻松入门职场编程。
Java前端基础知识
1. HTML与CSS
HTML(超文本标记语言)是构建网页的基本骨架,而CSS(层叠样式表)则用于美化网页。以下是两个基础案例:
案例1:创建一个简单的网页
<!DOCTYPE html>
<html>
<head>
<title>我的第一个网页</title>
</head>
<body>
<h1>欢迎来到我的网页</h1>
<p>这是一个简单的网页示例。</p>
</body>
</html>
案例2:使用CSS美化网页
<!DOCTYPE html>
<html>
<head>
<title>我的第一个网页</title>
<style>
body {
background-color: #f0f0f0;
}
h1 {
color: #333;
}
p {
font-size: 16px;
color: #666;
}
</style>
</head>
<body>
<h1>欢迎来到我的网页</h1>
<p>这是一个简单的网页示例。</p>
</body>
</html>
2. JavaScript
JavaScript是一种用于网页交互的脚本语言。以下是一个简单的JavaScript案例:
案例3:实现一个点击按钮弹出对话框的功能
<!DOCTYPE html>
<html>
<head>
<title>JavaScript示例</title>
<script>
function showAlert() {
alert("按钮被点击了!");
}
</script>
</head>
<body>
<h1>JavaScript示例</h1>
<button onclick="showAlert()">点击我</button>
</body>
</html>
Java前端实战案例
1. 创建一个简单的待办事项列表
在这个案例中,我们将使用HTML、CSS和JavaScript来创建一个简单的待办事项列表。
案例4:待办事项列表的HTML结构
<!DOCTYPE html>
<html>
<head>
<title>待办事项列表</title>
<style>
/* CSS样式 */
</style>
</head>
<body>
<h1>待办事项列表</h1>
<input type="text" id="todoInput" placeholder="添加待办事项">
<button onclick="addTodo()">添加</button>
<ul id="todoList"></ul>
<script>
// JavaScript代码
</script>
</body>
</html>
案例5:待办事项列表的CSS样式
/* CSS样式 */
body {
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
#todoInput {
width: 300px;
padding: 8px;
margin-bottom: 10px;
}
button {
padding: 8px 15px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
ul {
list-style-type: none;
padding: 0;
}
li {
background-color: #f0f0f0;
margin-bottom: 5px;
padding: 8px;
border: 1px solid #ddd;
}
案例6:待办事项列表的JavaScript代码
// JavaScript代码
let todos = [];
function addTodo() {
const todoInput = document.getElementById("todoInput");
const todoText = todoInput.value.trim();
if (todoText !== "") {
todos.push(todoText);
todoInput.value = "";
renderTodos();
}
}
function renderTodos() {
const todoList = document.getElementById("todoList");
todoList.innerHTML = "";
todos.forEach((todo, index) => {
const li = document.createElement("li");
li.textContent = todo;
const removeButton = document.createElement("button");
removeButton.textContent = "删除";
removeButton.onclick = function() {
todos.splice(index, 1);
renderTodos();
};
li.appendChild(removeButton);
todoList.appendChild(li);
});
}
2. 实现一个简单的计算器
在这个案例中,我们将使用HTML、CSS和JavaScript来实现一个简单的计算器。
案例7:计算器的HTML结构
<!DOCTYPE html>
<html>
<head>
<title>计算器</title>
<style>
/* CSS样式 */
</style>
</head>
<body>
<h1>计算器</h1>
<input type="text" id="display" disabled>
<div>
<button onclick="appendNumber('1')">1</button>
<button onclick="appendNumber('2')">2</button>
<button onclick="appendNumber('3')">3</button>
<button onclick="setOperation('+')">+</button>
</div>
<div>
<button onclick="appendNumber('4')">4</button>
<button onclick="appendNumber('5')">5</button>
<button onclick="appendNumber('6')">6</button>
<button onclick="setOperation('-')">-</button>
</div>
<div>
<button onclick="appendNumber('7')">7</button>
<button onclick="appendNumber('8')">8</button>
<button onclick="appendNumber('9')">9</button>
<button onclick="setOperation('*')">*</button>
</div>
<div>
<button onclick="appendNumber('0')">0</button>
<button onclick="calculate()">=</button>
<button onclick="clearDisplay()">C</button>
<button onclick="setOperation('/')">/</button>
</div>
<script>
// JavaScript代码
</script>
</body>
</html>
案例8:计算器的CSS样式
/* CSS样式 */
body {
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
#display {
width: 100%;
padding: 10px;
margin-bottom: 10px;
text-align: right;
}
button {
width: 25%;
padding: 10px;
margin: 5px;
background-color: #f0f0f0;
border: none;
cursor: pointer;
}
button:hover {
background-color: #ddd;
}
案例9:计算器的JavaScript代码
// JavaScript代码
let currentInput = "";
let previousInput = "";
let operation = null;
function appendNumber(number) {
currentInput += number;
updateDisplay();
}
function setOperation(op) {
if (currentInput === "") return;
if (previousInput !== "") calculate();
operation = op;
previousInput = currentInput;
currentInput = "";
updateDisplay();
}
function calculate() {
let result;
const prev = parseFloat(previousInput);
const current = parseFloat(currentInput);
if (isNaN(prev) || isNaN(current)) return;
switch (operation) {
case '+':
result = prev + current;
break;
case '-':
result = prev - current;
break;
case '*':
result = prev * current;
break;
case '/':
result = prev / current;
break;
default:
return;
}
currentInput = result.toString();
operation = null;
previousInput = "";
updateDisplay();
}
function clearDisplay() {
currentInput = "";
previousInput = "";
operation = null;
updateDisplay();
}
function updateDisplay() {
const display = document.getElementById("display");
display.value = currentInput;
}
3. 实现一个简单的博客系统
在这个案例中,我们将使用HTML、CSS和JavaScript来实现一个简单的博客系统。
案例10:博客系统的HTML结构
<!DOCTYPE html>
<html>
<head>
<title>博客系统</title>
<style>
/* CSS样式 */
</style>
</head>
<body>
<h1>我的博客</h1>
<div>
<h2>标题</h2>
<input type="text" id="titleInput">
</div>
<div>
<h2>内容</h2>
<textarea id="contentInput"></textarea>
</div>
<div>
<button onclick="savePost()">保存</button>
</div>
<div id="posts"></div>
<script>
// JavaScript代码
</script>
</body>
</html>
案例11:博客系统的CSS样式
/* CSS样式 */
body {
font-family: Arial, sans-serif;
}
h1 {
color: #333;
}
input[type="text"],
textarea {
width: 100%;
padding: 10px;
margin-bottom: 10px;
}
button {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#posts {
margin-top: 20px;
}
.post {
background-color: #f0f0f0;
margin-bottom: 20px;
padding: 10px;
border: 1px solid #ddd;
}
.post h2 {
margin: 0;
}
.post p {
margin: 0;
}
案例12:博客系统的JavaScript代码
// JavaScript代码
let posts = [];
function savePost() {
const titleInput = document.getElementById("titleInput");
const contentInput = document.getElementById("contentInput");
const title = titleInput.value.trim();
const content = contentInput.value.trim();
if (title === "" || content === "") return;
const post = {
title,
content
};
posts.push(post);
titleInput.value = "";
contentInput.value = "";
renderPosts();
}
function renderPosts() {
const postsContainer = document.getElementById("posts");
postsContainer.innerHTML = "";
posts.forEach((post, index) => {
const postElement = document.createElement("div");
postElement.className = "post";
const titleElement = document.createElement("h2");
titleElement.textContent = post.title;
const contentElement = document.createElement("p");
contentElement.textContent = post.content;
postElement.appendChild(titleElement);
postElement.appendChild(contentElement);
postsContainer.appendChild(postElement);
});
}
总结
通过以上实战案例,相信你已经对Java前端开发有了初步的了解。在实际工作中,你需要不断学习新技术、新工具,并积累项目经验。希望本文能帮助你轻松入门职场编程,祝你前程似锦!
