📄 sellwindow.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SellWindow extends JFrame implements ActionListener,Runnable{
private JButton stop,back;
private JComboBox room;
private JLabel label = new JLabel("当前时间:"),name,time,price;
private Thread thread = null;
private Movie current = null;
private JButton[] seats;
public SellWindow() {
this.setUndecorated(true);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screen.width-350)/2,(screen.height-200)/2);
thread = new Thread(this);
thread.start();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.setBackground(Color.pink);
panel.setPreferredSize(new Dimension(350,200));
label.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(label);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.pink);
panel1.setLayout(new FlowLayout());
String[] choices = {"请选择影厅","1号影厅","2号影厅","3号影厅","4号影厅"};
room = new JComboBox(choices);
room.addActionListener(this);
name = new JLabel("电影名称:");
panel1.add(room);
panel1.add(name);
panel.add(panel1);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.pink);
time = new JLabel("放映时间:");
price = new JLabel("票价:");
panel2.add(time);
panel2.add(price);
panel.add(panel2);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.pink);
panel3.setLayout(new GridLayout(5,5));
seats = new JButton[25];
for(int i=0;i<25;i++) {
seats[i] = new JButton();
seats[i].setSize(new Dimension(10,10));
seats[i].setBackground(Color.GREEN);
seats[i].addActionListener(this);
panel3.add(seats[i]);
}
panel.add(panel3);
JPanel panel4 = new JPanel();
panel4.setBackground(Color.pink);
stop = new JButton("停售此电影");
back = new JButton("回主菜单");
stop.addActionListener(this);
stop.setEnabled(false);
back.addActionListener(this);
panel4.add(stop);
panel4.add(back);
panel.add(panel4);
this.getContentPane().add(panel);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
for(int i=0;i<25;i++) {
if(source ==seats[i]) {
if(current!=null) {
if(current.getSeat(i)==0) {
int choice = JOptionPane.showConfirmDialog(this,"确认购买 "+(i/5+1)+"排"+(i+1-5*(i/5))+"号 吗?","确认购买此票?",JOptionPane.YES_NO_OPTION);
if(choice==JOptionPane.YES_OPTION) {
seats[i].setBackground(Color.RED);
current.sellOne((i/5+1),(i+1-5*(i/5)));
try {
FileOutputStream fos = new FileOutputStream("room"+room.getSelectedIndex()+".dat");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(current);
out.close();
}
catch(IOException e) {};
}
}
}
}
}
if(source == back) {
if(current!=null) {
try {
FileOutputStream file = new FileOutputStream("room"+room.getSelectedIndex()+".dat");
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(current);
out.close();
current = null;
}
catch (IOException e) {};
}
this.dispose();
new MainWindow();
}
if(source == stop) {
try {
File tempFile = new File("room"+current.getRoom()+".dat");
if(tempFile.exists()) {
tempFile.delete();
}
File file = new File("history.dat");
RandomAccessFile raf;
if(!file.exists()) {
file.createNewFile();
}
raf = new RandomAccessFile(file,"rw");
raf.seek(raf.length());
raf.writeUTF(Integer.toString(current.getStartDate().get(Calendar.YEAR))+"-"+
current.getStartDate().get(Calendar.MONTH)+"-"+current.getStartDate().get(Calendar.DATE));
raf.writeUTF(Integer.toString(current.getStartDate().get(Calendar.HOUR_OF_DAY))+":"+
current.getStartDate().get(Calendar.MINUTE));
raf.writeUTF(Integer.toString(current.getRoom()));
raf.writeUTF(current.getName());
raf.writeUTF(Integer.toString(current.getSold()));
raf.writeUTF(Integer.toString(current.getPrice()));
raf.writeUTF(Integer.toString(current.getSold()*current.getPrice()));
raf.close();
current = null;
}
catch(IOException e) {};
}
if(source == room) {
try {
if(room.getSelectedIndex()!=0) {
File file = new File("room"+room.getSelectedIndex()+".dat");
if(file.exists()) {
FileInputStream fis =new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fis);
current = (Movie)in.readObject();
name.setText("电影名称:" + current.getName());
time.setText("放映时间:" + current.getTime());
price.setText("票价:" + current.getPrice());
for(int i = 0;i<25;i++) {
if(current.getSeat(i)==1) {
seats[i].setBackground(Color.RED);
}
else {
seats[i].setBackground(Color.GREEN);
}
}
in.close();
stop.setEnabled(true);
}
else {
name.setText("电影名称:");
time.setText("放映时间:");
price.setText("票价:");
for(int i=0;i<25;i++) {
seats[i].setBackground(Color.GREEN);
}
stop.setEnabled(false);
current=null;
}
}
else {
name.setText("电影名称:");
time.setText("放映时间:");
price.setText("票价:");
for(int i=0;i<25;i++) {
seats[i].setBackground(Color.GREEN);
}
stop.setEnabled(false);
current=null;
}
}
catch(ClassNotFoundException e) {}
catch(IOException e) {};
}
}
public void run() {
while(true) {
Date now = new Date();
label.setText("当前时间:"+now.toString());
try {
thread.sleep(1000);
} catch (InterruptedException event) {}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -