Загрузка данных
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class ClothesShop extends JFrame {
public ClothesShop() {
setTitle("Магазин одежды");
setSize(700, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(null);
getContentPane().setBackground(new Color(35, 35, 35));
JLabel title = new JLabel("МАГАЗИН ОДЕЖДЫ");
title.setBounds(180, 30, 400, 40);
title.setForeground(Color.WHITE);
title.setFont(new Font("Arial", Font.BOLD, 30));
add(title);
JButton topBtn = createButton("ВЕРХ", 220, 120);
JButton bottomBtn = createButton("НИЗ", 220, 220);
JButton hatsBtn = createButton("ГОЛОВНЫЕ УБОРЫ", 220, 320);
add(topBtn);
add(bottomBtn);
add(hatsBtn);
topBtn.addActionListener(e ->
new CategoryFrame(
this,
"Верх",
new String[]{
"Худи - Теплое и стильное",
"Куртка - Осенняя коллекция",
"Футболка - Хлопок 100%"
}));
bottomBtn.addActionListener(e ->
new CategoryFrame(
this,
"Низ",
new String[]{
"Джинсы - Синие классические",
"Шорты - Летняя модель",
"Спортивные штаны - Удобные"
}));
hatsBtn.addActionListener(e ->
new CategoryFrame(
this,
"Головные уборы",
new String[]{
"Кепка - Черная",
"Шапка - Зимняя",
"Панама - Белая"
}));
setVisible(true);
}
JButton createButton(String text, int x, int y) {
JButton btn = new JButton(text);
btn.setBounds(x, y, 260, 55);
btn.setBackground(new Color(255, 120, 0));
btn.setForeground(Color.WHITE);
btn.setFont(new Font("Arial", Font.BOLD, 18));
btn.setFocusPainted(false);
return btn;
}
public static void main(String[] args) {
new ClothesShop();
}
}
class CategoryFrame extends JFrame {
JFrame previousFrame;
public CategoryFrame(JFrame prev,
String titleText,
String[] items) {
previousFrame = prev;
setTitle(titleText);
setSize(750, 550);
setLocationRelativeTo(null);
setLayout(null);
getContentPane().setBackground(new Color(245,245,245));
JLabel title = new JLabel(titleText);
title.setBounds(300, 20, 300, 40);
title.setFont(new Font("Arial", Font.BOLD, 28));
add(title);
JButton backBtn = new JButton("← Назад");
backBtn.setBounds(20, 20, 120, 35);
backBtn.setBackground(new Color(60,60,60));
backBtn.setForeground(Color.WHITE);
add(backBtn);
backBtn.addActionListener(e -> dispose());
int y = 90;
for(String item : items) {
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(80, y, 560, 90);
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
JLabel label = new JLabel(item);
label.setBounds(20, 15, 320, 30);
label.setFont(new Font("Arial", Font.PLAIN, 18));
JButton favBtn = new JButton("❤ Избранное");
favBtn.setBounds(360, 25, 160, 35);
favBtn.setBackground(new Color(255, 80, 80));
favBtn.setForeground(Color.WHITE);
favBtn.setFont(new Font("Arial", Font.BOLD, 14));
favBtn.addActionListener(e -> saveFavorite(item));
panel.add(label);
panel.add(favBtn);
add(panel);
y += 110;
}
setVisible(true);
}
private void saveFavorite(String item) {
try {
File file = new File("favorites.txt");
int count = 0;
if(file.exists()) {
BufferedReader br =
new BufferedReader(
new FileReader(file));
String line = br.readLine();
if(line != null) {
count = Integer.parseInt(line);
}
br.close();
}
count++;
BufferedWriter bw =
new BufferedWriter(
new FileWriter(file));
bw.write(String.valueOf(count));
bw.close();
JOptionPane.showMessageDialog(
this,
item +
"\nДобавлено в избранное!" +
"\nВсего товаров: " + count
);
} catch(Exception e) {
JOptionPane.showMessageDialog(
this,
"Ошибка файла"
);
}
}
}