⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loadwindow.java

📁 电影院售票系统我自己亲手做的的了很高的分数
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class LoadWindow extends JFrame implements ActionListener{
    private JButton back,load;
    private JComboBox room;
    private JTextField name,year,month,day,startHour,startMinute,endHour,endMinute,price;
    public LoadWindow() {
        this.setUndecorated(true);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screen.width-300)/2,(screen.height-200)/2);
        
        
        JPanel panel = new JPanel();
        panel.setBackground(Color.pink);
        panel.setPreferredSize(new Dimension(300,200));
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
        panel.add(Box.createRigidArea(new Dimension(0,10)));
        
        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.pink);
        String[] choices = {"请选择影厅","1号影厅","2号影厅","3号影厅","4号影厅"};
        room = new JComboBox(choices);
        room.addActionListener(this);
        JLabel label1 = new JLabel("电影名称 :");
        name = new JTextField(10);
        panel1.add(room);
        panel1.add(label1);
        panel1.add(name);
        panel.add(panel1);
        
        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.pink);
        JLabel label2 = new JLabel("放映时间 :");
        panel2.add(label2);
        year = new JTextField(4);
        JLabel label3 = new JLabel("年");
        month = new JTextField(2);
        JLabel label4 = new JLabel("月");
        day = new JTextField(2);
        JLabel label5 = new JLabel("日");
        panel2.add(year);
        panel2.add(label3);
        panel2.add(month);
        panel2.add(label4);
        panel2.add(day);
        panel2.add(label5);
        panel.add(panel2);
        panel.add(Box.createRigidArea(new Dimension(0,10)));
        
        JPanel panel3 = new JPanel();
        panel3.setBackground(Color.pink);
        startHour = new JTextField(2);
        JLabel label6 = new JLabel(":");
        startMinute = new JTextField(2);
        JLabel label7 = new JLabel("至");
        endHour = new JTextField(2);
        JLabel label8 = new JLabel(":");
        endMinute = new JTextField(2);
        JLabel label9 = new JLabel("票价:");
        price = new JTextField(3);
        panel3.add(startHour);
        panel3.add(label6);
        panel3.add(startMinute);
        panel3.add(label7);
        panel3.add(endHour);
        panel3.add(label8);
        panel3.add(endMinute);
        panel3.add(label9);
        panel3.add(price);
        panel.add(panel3);
        
        JPanel panel4 = new JPanel();
        panel4.setBackground(Color.pink);
        load = new JButton("载入数据");
        load.addActionListener(this);
        panel4.add(load);
        back = new JButton("回主菜单");
        room.addActionListener(this);
        back.addActionListener(this);
        panel4.add(back);
        panel.add(panel4);
        this.getContentPane().add(panel);
        this.pack();
        this.setVisible(true);
    }
    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if(source==back) {
            this.dispose();
            new MainWindow();
        }
        if(source ==room) {
            if(room.getSelectedIndex()!=0) {
                try {
                    File file = new File("room"+room.getSelectedIndex()+".dat");
                    FileInputStream fis;
                    ObjectInputStream in;
                    if(file.exists()) {
                        fis = new FileInputStream(file);
                        in = new ObjectInputStream(fis);
                        Movie movie = (Movie)in.readObject();
                        name.setText(movie.getName());
                        year.setText(Integer.toString(movie.getStartDate().get(Calendar.YEAR)));
                        month.setText(Integer.toString(movie.getStartDate().get(Calendar.MONTH)));
                        day.setText(Integer.toString(movie.getStartDate().get(Calendar.DATE)));
                        startHour.setText(Integer.toString(movie.getStartDate().get(Calendar.HOUR_OF_DAY)));
                        startMinute.setText(Integer.toString(movie.getStartDate().get(Calendar.MINUTE)));
                        endHour.setText(Integer.toString(movie.getEndDate().get(Calendar.HOUR_OF_DAY)));
                        endMinute.setText(Integer.toString(movie.getEndDate().get(Calendar.MINUTE)));
                        price.setText(Integer.toString(movie.getPrice()));
                        load.setEnabled(false);
                        in.close();
                    }
                    else {
                        name.setText("");
                        year.setText("");
                        month.setText("");
                        day.setText("");
                        startHour.setText("");
                        startMinute.setText("");
                        endHour.setText("");
                        endMinute.setText("");
                        price.setText("");
                        load.setEnabled(true);
                    }
                }
                catch(IOException e) {}
                catch(ClassNotFoundException e) {};
            }
        }
        if(source==load) {
            try {
                if(room.getSelectedIndex()==0)
                    JOptionPane.showMessageDialog(this,"请选择影厅");
                else if(name.getText().length()==0)
                    JOptionPane.showMessageDialog(this,"请输入电影名称");
                else if(year.getText().length()==0||month.getText().length()==0||day.getText().length()==0)
                    JOptionPane.showMessageDialog(this,"请输入放映日期(年,月,日均不能为空)");
                else if(startHour.getText().length()==0||startMinute.getText().length()==0||
                        endHour.getText().length()==0||endMinute.getText().length()==0)
                    JOptionPane.showMessageDialog(this,"请输入放映时间(时,分均不能为空)");
                else if(price.getText().length()==0)
                    JOptionPane.showMessageDialog(this,"请输入票价");
                else {
                    String tempName = name.getText();
                    int tempRoom = room.getSelectedIndex();
                    GregorianCalendar tempStartDate = new GregorianCalendar(Integer.parseInt(year.getText()),Integer.parseInt(month.getText()),
                            Integer.parseInt(day.getText()),Integer.parseInt(startHour.getText()),Integer.parseInt(startMinute.getText()));
                    GregorianCalendar tempEndDate = new GregorianCalendar(Integer.parseInt(year.getText()),Integer.parseInt(month.getText()),
                            Integer.parseInt(day.getText()),Integer.parseInt(endHour.getText()),Integer.parseInt(endMinute.getText()));
                    int tempPrice = Integer.parseInt(price.getText());
                    File file = new File("room"+room.getSelectedIndex()+".dat");
                    if(!file.exists())
                        file.createNewFile();
                    FileOutputStream fos = new FileOutputStream(file);
                    ObjectOutputStream out = new ObjectOutputStream(fos);
                    out.writeObject(new Movie(tempName,tempRoom,tempStartDate,tempEndDate,tempPrice));
                    JOptionPane.showMessageDialog(this,"电影:" + name.getText()+"已载入");
                    name.setText("");
                    year.setText("");
                    month.setText("");
                    day.setText("");
                    startHour.setText("");
                    startMinute.setText("");
                    endHour.setText("");
                    endMinute.setText("");
                    price.setText("");
                }
            } 
            catch(IOException e) {}
            catch(NumberFormatException e) {
                JOptionPane.showMessageDialog(this,"输入信息格式有误,请按格式填写");
            };
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -