import java.awt.*; import java.awt.event.*; import java.io.*; import java.text.SimpleDateFormat; import java.util.*; import javax.swing.*; class Fruit { private String id; private String name; private double weight; private double cost; public Fruit(String id, String name, double weight, double cost) { this.id = id; this.name = name; this.weight = weight; this.cost = cost; } public String getId() { return id; } public String getName() { return name; } public double getWeight() { return weight; } public double getCost() { return cost; } } class Login extends JFrame implements ActionListener { private JTextField usernameField; private JPasswordField passwordField; public Login() { super("登录"); setLayout(new GridLayout(3, 2)); add(new JLabel("工号:")); usernameField = new JTextField(10); add(usernameField); add(new JLabel("密码:")); passwordField = new JPasswordField(10); add(passwordField); JButton loginButton = new JButton("登录"); loginButton.addActionListener(this); add(loginButton); pack(); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); if (username.equals("admin") && password.equals("password")) { new InStorageFrame(); dispose(); } else { JOptionPane.showMessageDialog(this, "工号或密码错误", "错误", JOptionPane.ERROR_MESSAGE); usernameField.setText(""); passwordField.setText(""); usernameField.requestFocus(); } } } class InStorageFrame extends JFrame implements ActionListener { private JComboBox<String> fruitComboBox; private JTextField weightField; private JTextField costField; private JTextArea detailArea; private ArrayList<Fruit> fruits; public InStorageFrame() { super("入库"); fruits = new ArrayList<>(); setLayout(new BorderLayout()); JPanel topPanel = new JPanel(new GridLayout(3, 2)); topPanel.add(new JLabel("水果名称:")); fruitComboBox = new JComboBox<>(new String[]{"苹果", "葡萄"}); topPanel.add(fruitComboBox); topPanel.add(new JLabel("重量(kg):")); weightField = new JTextField(10); topPanel.add(weightField); topPanel.add(new JLabel("成本单价:")); costField = new JTextField(10); topPanel.add(costField); add(topPanel, BorderLayout.NORTH); JPanel centerPanel = new JPanel(new BorderLayout()); centerPanel.add(new JLabel("入库明细:"), BorderLayout.NORTH); detailArea = new JTextArea(10, 30); detailArea.setEditable(false); centerPanel.add(new JScrollPane(detailArea), BorderLayout.CENTER); add(centerPanel, BorderLayout.CENTER); JPanel bottomPanel = new JPanel(); JButton addButton = new JButton("添加"); addButton.addActionListener(this); bottomPanel.add(addButton); JButton confirmButton = new JButton("确认"); confirmButton.addActionListener(this); bottomPanel.add(confirmButton); add(bottomPanel, BorderLayout.SOUTH); pack(); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("添加")) { String name = (String) fruitComboBox.getSelectedItem(); double weight = Double.parseDouble(weightField.getText()); double cost = Double.parseDouble(costField.getText()); String id = UUID.randomUUID().toString(); Fruit fruit = new Fruit(id, name, weight, cost); fruits.add(fruit); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = sdf.format(new Date()); detailArea.append(String.format("%s %s %s %.2fkg %.2f元/kg\n", time, id, name, weight, cost)); weightField.setText(""); costField.setText(""); weightField.requestFocus(); } else if (command.equals("确认")) { try (PrintWriter pw = new PrintWriter(new FileWriter("fruitStorage.txt"))) { for (Fruit fruit : fruits) { pw.println(fruit.getId() + " " + fruit.getName() + " " + fruit.getWeight() + " " + fruit.getCost()); } JOptionPane.showMessageDialog(this, "入库成功", "成功", JOptionPane.INFORMATION_MESSAGE); detailArea.setText(""); fruits.clear(); } catch (IOException ex) { ex.printStackTrace(); } } } } public class exf { public static void main(String[] args) { new Login(); } }
myself
于 2023-04-26 19:36:18 首次发布