<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-Do List</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 flex items-center justify-center h-screen">
<div class="bg-white p-8 rounded-lg shadow-lg w-full max-w-md">
<h1 class="text-2xl font-bold mb-4">To-Do List</h1>
<form id="todo-form" class="flex mb-4">
<input id="todo-input" type="text" placeholder="Add a new task" class="flex-grow p-2 border rounded-l-lg focus:outline-none">
<button type="submit" class="bg-blue-500 text-white px-4 py-2 rounded-r-lg hover:bg-blue-700">Add</button>
</form>
<div class="mb-4">
<button id="filter-all" class="filter-btn bg-gray-200 text-black px-4 py-2 rounded-l-lg">All</button>
<button id="filter-active" class="filter-btn bg-gray-200 text-black px-4 py-2">Active</button>
<button id="filter-completed" class="filter-btn bg-gray-200 text-black px-4 py-2 rounded-r-lg">Completed</button>
</div>
<ul id="todo-list" class="list-disc pl-5">
<!-- To-Do items will be added here -->
</ul>
</div>
<script>
const todoForm = document.getElementById('todo-form');
const todoInput = document.getElementById('todo-input');
const todoList = document.getElementById('todo-list');
const filterButtons = document.querySelectorAll('.filter-btn');
document.addEventListener('DOMContentLoaded', loadTodos);
todoForm.addEventListener('submit', function(event) {
event.preventDefault();
addTodoItem(todoInput.value);
todoInput.value = '';
});
filterButtons.forEach(button => {
button.addEventListener('click', () => {
document.querySelectorAll('.filter-btn').forEach(btn => btn.classList.remove('bg-blue-500', 'text-white'));
button.classList.add('bg-blue-500', 'text-white');
filterTodos(button.id);
});
});
function addTodoItem(task) {
if (task === '') return;
const listItem = document.createElement('li');
listItem.classList.add('flex', 'items-center', 'justify-between', 'py-2', 'border-b', 'border-gray-200');
const taskText = document.createElement('span');
taskText.textContent = task;
taskText.classList.add('flex-grow', 'cursor-pointer');
taskText.addEventListener('click', toggleComplete);
const editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.classList.add('bg-yellow-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-yellow-700');
editButton.addEventListener('click', () => editTodoItem(listItem, taskText));
const priorityButton = document.createElement('button');
priorityButton.textContent = 'Important';
priorityButton.classList.add('bg-green-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-green-700');
priorityButton.addEventListener('click', () => togglePriority(taskText));
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.classList.add('bg-red-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-red-700');
deleteButton.addEventListener('click', () => deleteTodoItem(listItem));
listItem.appendChild(taskText);
listItem.appendChild(editButton);
listItem.appendChild(priorityButton);
listItem.appendChild(deleteButton);
todoList.appendChild(listItem);
saveTodos();
}
function toggleComplete(event) {
event.target.classList.toggle('line-through');
event.target.classList.toggle('text-gray-500');
saveTodos();
}
function togglePriority(taskText) {
taskText.classList.toggle('font-bold');
saveTodos();
}
function editTodoItem(listItem, taskText) {
const newTask = prompt('Edit your task:', taskText.textContent);
if (newTask !== null && newTask !== '') {
taskText.textContent = newTask;
saveTodos();
}
}
function deleteTodoItem(listItem) {
listItem.remove();
saveTodos();
}
function saveTodos() {
const todos = [];
document.querySelectorAll('#todo-list li').forEach((item) => {
todos.push({
text: item.querySelector('span').textContent,
completed: item.querySelector('span').classList.contains('line-through'),
important: item.querySelector('span').classList.contains('font-bold')
});
});
localStorage.setItem('todos', JSON.stringify(todos));
}
function loadTodos() {
const savedTodos = JSON.parse(localStorage.getItem('todos')) || [];
savedTodos.forEach((todo) => {
const listItem = document.createElement('li');
listItem.classList.add('flex', 'items-center', 'justify-between', 'py-2', 'border-b', 'border-gray-200');
const taskText = document.createElement('span');
taskText.textContent = todo.text;
taskText.classList.add('flex-grow', 'cursor-pointer');
if (todo.completed) {
taskText.classList.add('line-through', 'text-gray-500');
}
if (todo.important) {
taskText.classList.add('font-bold');
}
taskText.addEventListener('click', toggleComplete);
const editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.classList.add('bg-yellow-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-yellow-700');
editButton.addEventListener('click', () => editTodoItem(listItem, taskText));
const priorityButton = document.createElement('button');
priorityButton.textContent = 'Important';
priorityButton.classList.add('bg-green-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-green-700');
priorityButton.addEventListener('click', () => togglePriority(taskText));
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.classList.add('bg-red-500', 'text-white', 'px-2', 'py-1', 'rounded', 'ml-2', 'hover:bg-red-700');
deleteButton.addEventListener('click', () => deleteTodoItem(listItem));
listItem.appendChild(taskText);
listItem.appendChild(editButton);
listItem.appendChild(priorityButton);
listItem.appendChild(deleteButton);
todoList.appendChild(listItem);
});
}
function filterTodos(filter) {
const allTodos = document.querySelectorAll('#todo-list li');
allTodos.forEach((todo) => {
switch (filter) {
case 'filter-all':
todo.style.display = 'flex';
break;
case 'filter-active':
todo.querySelector('span').classList.contains('line-through') ? todo.style.display = 'none' : todo.style.display = 'flex';
break;
case 'filter-completed':
todo.querySelector('span').classList.contains('line-through') ? todo.style.display = 'flex' : todo.style.display = 'none';
break;
}
});
}
</script>
</body>
</html>
Tailwind CSS入門 インタラクティブにスタイルを変えてみよう
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<button
class="bg-orange-400 text-white m-4 py-2 px-4 rounded-full shadow-black/30 shadow-md hover:opacity-80 transition duration-500 active:translate-y-1">
Buy now!
</button>
</body>
</html>
Tailwind CSS入門 ボタンのスタイリングをしてみよう
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<button class="bg-orange-400 text-white m-4 py-2 px-4 rounded-full shadow-black/30 shadow-md">
Buy now!
</button>
</body>
</html>
Tailwind CSS入門 境界線を設定してみよう
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="bg-red-400 w-20 h-20 border-solid border-gray-200 border-8">Box 1</div>
<div class="bg-sky-400 w-20 h-20 border-dashed border-gray-200 border-b-8">Box 2</div>
</body>
</html>
Java シンプルRPG
簡単なRPGを作成するために、以下のコードを提供します。このコードは、プレイヤーキャラクターと敵キャラクターが戦うシンプルなテキストベースのRPGです。
java
Copy code
import java.util.Scanner;
public class SimpleRPG {
// キャラクタークラス
public static class Character {
private String name;
private int health;
private int attackPower;
public Character(String name, int health, int attackPower) {
this.name = name;
this.health = health;
this.attackPower = attackPower;
}
public String getName() {
return name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getAttackPower() {
return attackPower;
}
public void attack(Character enemy) {
System.out.println(name + " attacks " + enemy.getName() + " for " + attackPower + " damage.");
enemy.setHealth(enemy.getHealth() - attackPower);
}
}
// メインメソッド
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// プレイヤーキャラクターの作成
Character player = new Character("Player", 100, 20);
Character enemy = new Character("Goblin", 50, 10);
System.out.println("A wild " + enemy.getName() + " appears!");
// 戦闘ループ
while (player.getHealth() > 0 && enemy.getHealth() > 0) {
System.out.println("Your HP: " + player.getHealth());
System.out.println(enemy.getName() + " HP: " + enemy.getHealth());
System.out.println("1. Attack");
System.out.println("2. Run");
int choice = scanner.nextInt();
if (choice == 1) {
player.attack(enemy);
if (enemy.getHealth() > 0) {
enemy.attack(player);
} else {
System.out.println("You defeated the " + enemy.getName() + "!");
}
} else if (choice == 2) {
System.out.println("You ran away!");
break;
}
if (player.getHealth() <= 0) {
System.out.println("You were defeated by the " + enemy.getName() + "...");
}
}
scanner.close();
}
}
このコードをテキストエディタにコピーして、SimpleRPG.javaとして保存してください。次に、以下のコマンドを使ってコンパイルし、実行してください。
sh
Copy code
javac SimpleRPG.java
java SimpleRPG
このコードは、プレイヤーが敵と戦うシンプルなRPGです。プレイヤーは敵を攻撃するか、逃げるかを選択できます。戦闘が進むにつれて、各キャラクターのHPが減少し、最終的に勝敗が決まります。
Java 電卓
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator extends JFrame implements ActionListener {
private JTextField display;
private JPanel panel;
private StringBuilder currentInput;
private double result;
private String operator;
public Calculator() {
// Frame settings
setTitle("Calculator");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Display settings
display = new JTextField();
display.setEditable(false);
display.setFont(new Font("Arial", Font.BOLD, 24));
add(display, BorderLayout.NORTH);
// Button panel settings
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
// Create and add buttons
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.BOLD, 24));
button.addActionListener(this);
panel.add(button);
}
add(panel, BorderLayout.CENTER);
currentInput = new StringBuilder();
result = 0;
operator = "";
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("0123456789.".contains(command)) {
currentInput.append(command);
display.setText(currentInput.toString());
} else if (command.equals("=")) {
calculate();
display.setText(String.valueOf(result));
currentInput.setLength(0);
} else {
if (currentInput.length() > 0) {
calculate();
operator = command;
display.setText(String.valueOf(result));
currentInput.setLength(0);
}
}
}
private void calculate() {
double input = currentInput.length() > 0 ? Double.parseDouble(currentInput.toString()) : 0;
switch (operator) {
case "+":
result += input;
break;
case "-":
result -= input;
break;
case "*":
result *= input;
break;
case "/":
result /= input;
break;
default:
result = input;
break;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Calculator calculator = new Calculator();
calculator.setVisible(true);
});
}
}
javac Calculator.java
java Calculator
Java メモ帳
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class SimpleNotepad extends JFrame implements ActionListener {
private JTextArea textArea;
private JMenuItem openItem, saveItem, exitItem;
public SimpleNotepad() {
// Frame settings
setTitle("Simple Notepad");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Text area settings
textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
// Menu bar creation
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
openItem = new JMenuItem("Open");
saveItem = new JMenuItem("Save");
exitItem = new JMenuItem("Exit");
openItem.addActionListener(this);
saveItem.addActionListener(this);
exitItem.addActionListener(this);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(exitItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == openItem) {
openFile();
} else if (e.getSource() == saveItem) {
saveFile();
} else if (e.getSource() == exitItem) {
System.exit(0);
}
}
private void openFile() {
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try (BufferedReader reader = new BufferedReader(new FileReader(fileChooser.getSelectedFile()))) {
textArea.read(reader, null);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "File could not be opened", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private void saveFile() {
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showSaveDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
try (FileWriter writer = new FileWriter(fileChooser.getSelectedFile())) {
textArea.write(writer);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "File could not be saved", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
SimpleNotepad notepad = new SimpleNotepad();
notepad.setVisible(true);
});
}
}
Tailwind CSS 余白設定してみよう
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="bg-red-400 w-40 h-40 m-4">Box 1</div>
<div class="bg-sky-400 w-[160px] mb-4">Box 2</div>
<div class="bg-orange-400 w-3/4 mx-auto">Box 3</div>
<div class="bg-green-400 w-[75%]-pl-4">Box 4</div>
</body>
</html>
Tailwind CSS 要素のサイズを設定してみよう
index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="bg-red-400 w-40 h-40">Box 1</div>
<div class="bg-sky-400 w-[160px]">Box 2</div>
<div class="bg-orange-400 w-3/4">Box 3</div>
<div class="bg-green-400 w-[75%]">Box 4</div>
</body>
</html>
Tailwind CSS 要素のサイズを設定してみよう
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Tailwind CSS</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div class="bg-red-400 w-40 h-40">Box 1</div>
<div class="bg-sky-400 w-[160px]">Box 2</div>
<div class="bg-orange-400 w-3/4">Box 3</div>
<div class="bg-green-400 w-[75%]">Box 4</div>
</body>
</html>