
【2回目】Swing演習問題(30問)
初級問題(9問) 中級問題(15問) 上級問題(6問) Swing演習問題 解答例 初級問題解答 JFrame frame = new JFrame("My A […]
Swing初級から中級者向けに、4つの異なるアプリケーションを段階的に開発する方法を解説します。各アプリケーションは独立しており、難易度順に並んでいます。
// 電卓のフレーム作成
JFrame frame = new JFrame("簡単な電卓");
frame.setSize(300, 400);
frame.setLayout(new BorderLayout());
// ディスプレイ
JTextField display = new JTextField();
display.setEditable(false);
frame.add(display, BorderLayout.NORTH);
// ボタンパネル
JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 5, 5));
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"C", "0", "=", "+"
};
for (String text : buttons) {
JButton button = new JButton(text);
button.addActionListener(e -> {
String cmd = e.getActionCommand();
// ここに処理を実装
});
buttonPanel.add(button);
}
// 計算ロジックの例
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
// 他の演算子も同様に
}
解答例は別の記事にしてます。
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
// メニューバーの作成
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("ファイル");
JMenuItem newItem = new JMenuItem("新規作成");
// 他のメニュー項目も追加
newItem.addActionListener(e -> {
textArea.setText("");
currentFile = null;
});
// ファイル保存の例
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (PrintWriter writer = new PrintWriter(file)) {
writer.write(textArea.getText());
} catch (IOException ex) {
ex.printStackTrace();
}
}
// フォント変更の例
FontDialog fontDialog = new FontDialog(frame, true);
fontDialog.setVisible(true);
if (fontDialog.isApproved()) {
textArea.setFont(fontDialog.getSelectedFont());
}
解答例は別の記事にしてます。
class ImagePanel extends JPanel {
private BufferedImage image;
private double scale = 1.0;
public void setImage(BufferedImage image) {
this.image = image;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
int w = (int)(image.getWidth() * scale);
int h = (int)(image.getHeight() * scale);
g.drawImage(image, 0, 0, w, h, this);
}
}
}
JToolBar toolBar = new JToolBar();
JButton openButton = new JButton("開く");
JButton zoomInButton = new JButton("拡大");
JButton zoomOutButton = new JButton("縮小");
openButton.addActionListener(e -> {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
try {
BufferedImage img = ImageIO.read(chooser.getSelectedFile());
imagePanel.setImage(img);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
// 画像回転の例
AffineTransform transform = new AffineTransform();
transform.rotate(Math.toRadians(90), image.getWidth()/2, image.getHeight()/2);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
image = op.filter(image, null);
解答例は別の記事にしてます。
class Student {
private String id;
private String name;
private int score;
// コンストラクタ、ゲッター、セッター
}
// テーブルモデル
class StudentTableModel extends AbstractTableModel {
private List students = new ArrayList<>();
private String[] columnNames = {"学籍番号", "氏名", "成績"};
@Override
public int getRowCount() { return students.size(); }
// 他の必須メソッド
}
// テーブル
JTable table = new JTable(new StudentTableModel());
JScrollPane scrollPane = new JScrollPane(table);
// ボタンパネル
JButton addButton = new JButton("追加");
JButton editButton = new JButton("編集");
JButton deleteButton = new JButton("削除");
// 学生追加/編集ダイアログ
class StudentDialog extends JDialog {
private JTextField idField = new JTextField(10);
private JTextField nameField = new JTextField(10);
private JSpinner scoreSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 100, 1));
public StudentDialog(Frame owner, boolean modal) {
super(owner, "学生情報", modal);
// レイアウト設定
}
public Student getStudent() {
return new Student(idField.getText(), nameField.getText(),
(Integer)scoreSpinner.getValue());
}
}
// 保存
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.dat"))) {
oos.writeObject(students);
}
// 読み込み
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students.dat"))) {
students = (List)ois.readObject();
}
// テーブル更新の例
((StudentTableModel)table.getModel()).addStudent(dialog.getStudent());
table.revalidate();
table.repaint();
解答例は別の記事にしてます。
System.out.println("現在の値: " + value);
private static final String SAVE_FILE = "students.dat";
各アプリケーションを完成させたら、以下のような拡張に挑戦してみましょう:
このステップバイステップのアプローチで、Swingのスキルを確実に向上させることができます!