import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class lift extends JFrame{
public static void main(String []args){
SetFrame sf=new SetFrame();
sf.setSize(400,300);
sf.setTitle("电梯");
sf.setVisible(true);
sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Action act=new Action(sf);
}
}
//电梯外部按扭的面板
class SetOutterButton extends JPanel{
JButton outbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetOutterButton(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
outbt[i]=new JButton(""+(i+1));
add(outbt[i]);
}
}
}
//电梯内部按扭的面板
class SetInnerButton extends JPanel{
JButton inbt[]=new JButton[10];
SetButtonPanel SetButton;
public SetInnerButton(){
setLayout(new GridLayout(6,2));
for(int i=0;i<10;i++){
inbt[i]=new JButton(""+(i+1));
add(inbt[i]);
}
}
}
//电梯上方显示的面板
class SetShow extends JPanel{
JLabel jlb[]=new JLabel[10];
public SetShow(){
setLayout(new GridLayout(2,5));
for(int i=0;i<10;i++){
jlb[i]=new JLabel(""+(i+1)+"层");
jlb[i].setFont(new Font("",Font.BOLD,12));
add(jlb[i]);
}
}
}
//显示电梯进出人数的面板
class StringPanel extends JPanel{
JLabel num=new JLabel("选择人数");
CheckboxGroup cbg=new CheckboxGroup();
//这块面板谁能帮我设置下,我希望那个JLable独自一行 ,那十个checkbox分成两行
public StringPanel(){
num.setFont(new Font("宋体",Font.BOLD,20));
num.setForeground(Color.BLUE);
add(num);
add(new Checkbox("1",cbg,false));
add(new Checkbox("2",cbg,false));
add(new Checkbox("3",cbg,false));
add(new Checkbox("4",cbg,false));
add(new Checkbox("5",cbg,false));
add(new Checkbox("6",cbg,false));
add(new Checkbox("7",cbg,false));
add(new Checkbox("8",cbg,false));
add(new Checkbox("9",cbg,false));
add(new Checkbox("10",cbg,false));
}
}
class SetFrame extends JFrame{
Container con=getContentPane();
SetOutterButton outb=new SetOutterButton();
SetInnerButton inb = new SetInnerButton();
SetShow ss=new SetShow();
StringPanel sp=new StringPanel();
public SetFrame(){
con.setLayout(new BorderLayout(5,5));
con.add(ss,BorderLayout.NORTH);
con.add(inb,BorderLayout.WEST);
con.add(outb,BorderLayout.EAST);
con.add(sp,BorderLayout.CENTER);
}
}
class Action extends JFrame implements Runnable{
SetFrame sf;
Thread runner;
int floor=0;
int floor_end=1;
int person_num=0;
public Action(SetFrame stfm){
sf=stfm;
sf.ss.jlb[0].setForeground(Color.GREEN);
for(int i=0;i<10;i++){
sf.outb.outbt[i].addActionListener(new MyOutActionListener());
sf.outb.outbt[i].setActionCommand(""+(i+1));
}
for(int i=0;i<10;i++){
sf.inb.inbt[i].addActionListener(new MyInActionListener());
sf.inb.inbt[i].setActionCommand(""+(i+1));
}
}
class MyOutActionListener implements ActionListener{
public void actionPerformed(ActionEvent outae){
floor=Integer.parseInt(outae.getActionCommand());
sf.outb.outbt[floor-1].setForeground(Color.BLUE);
sf.sp.num.setText("门开了,请选选择进去人数");
threadStart();
}
}
class MyInActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
floor=Integer.parseInt(ae.getActionCommand());
sf.inb.inbt[floor-1].setForeground(Color.BLUE);
sf.sp.num.setText("门开了,请选择出去人数");
threadStart();
}
}
public void threadStart(){
runner=new Thread(this);
runner.start();
}
public void run(){
while(true){
if(floor_end<floor){
up();
}else if(floor_end>floor){
down();
}else if(floor_end==floor){
stop();
}
}
}
public void up(){
pause(1000);
sf.ss.jlb[floor_end].setForeground(Color.GREEN);
sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end++;
}
public void down(){
pause(1000);
sf.ss.jlb[floor_end-2].setForeground(Color.GREEN);
sf.ss.jlb[floor_end-1].setForeground(Color.BLACK);
floor_end--;
}
public void stop(){
if(floor==1){
pause(1000);
floor_end=0;
sf.ss.jlb[0].setForeground(Color.GREEN);
sf.ss.jlb[1].setForeground(Color.BLACK);
floor_end=1;
}
sf.outb.outbt[floor-1].setForeground(Color.BLACK);
runner.stop();
}
public void pause(int time){
try{
runner.sleep(time);
}catch(InterruptedException ire){
System.err.println(ire.getMessage());
}
}
}
有谁能帮帮我吗?