1.我在设计一个XX购票系统的时候要求有一个购票中的窗口,在那个窗口等待几秒后,会自动关闭,然后弹出购买成功的窗口,如果在那个窗口点取消就会返回到购票系统的窗口,但是我在购票系统的窗口设置了一个等待三秒的语句,设置后弹出的窗口里面的组件例如取消按钮啊什么的,全都不显示了.求大神解答,跪求.
代码如下:
购票系统的代码:
package Windows;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class mainwin extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainwin frame = new mainwin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public mainwin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(860,480);
setLocationRelativeTo(null);
setTitle("獒獒自助购票系统");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton confirm =new JButton("确认购票");
confirm.setFont(new Font("宋体", Font.PLAIN, 15));
confirm.setSize(844, 30);
confirm.setLocation(0, 411);
confirm.addActionListener(new ActionListener()//为confirm按钮创建出发后的事件
{
public void actionPerformed(ActionEvent e)
{
confirm frame = new confirm();
frame.setVisible(true);
frame.cmp=1;
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
}
if(frame.cmp==1)
{
frame.setVisible(false);
success frame1 = new success();
frame1.setVisible(true);
}
System.out.println(frame.cmp);
}
});
JTextField arv=new JTextField();//这里是输入目的地的文本框
arv.setSize(160, 40);
arv.setLocation(120, 88);
JTextField py=new JTextField();//这里是输入乘客姓名的文本框
py.setSize(75, 30);
py.setLocation(113, 199);
JTextField pyn=new JTextField();//这里是输入乘客身份证号的文本框
pyn.setSize(426,30);
pyn.setLocation(300,199);
JTextField inc=new JTextField();//这里是输入收到金额的文本框
inc.setSize(100,40);
inc.setLocation(102, 312);
JTextField fee=new JTextField();//这里是显示票价的文本框
fee.setSize(100,40);
fee.setLocation(365, 312);
fee.setEditable(false);
JTextField chrg=new JTextField();//这里是显示找零的文本框
chrg.setSize(100,40);
chrg.setLocation(615, 312);
chrg.setEditable(false);
JLabel title=new JLabel("獒獒自助购票系统");
title.setFont(new Font("楷体", Font.PLAIN, 55));
title.setSize(450,50);
title.setLocation(197, 20);
JLabel arv1=new JLabel("目的地:");
arv1.setFont(new Font("楷体", Font.PLAIN, 20));
arv1.setSize(70, 50);
arv1.setLocation(40, 80);
JLabel py1=new JLabel("乘客姓名:");
py1.setFont(new Font("楷体", Font.PLAIN, 15));
py1.setSize(75,50);
py1.setLocation(35,188);
JLabel pyn1=new JLabel("身份证号:");
pyn1.setFont(new Font("楷体", Font.PLAIN, 15));
pyn1.setSize(75,50);
pyn1.setLocation(215,188);
JLabel inc1=new JLabel("收到金额:");
inc1.setFont(new Font("楷体", Font.PLAIN, 15));
inc1.setSize(75,50);
inc1.setLocation(35, 306);
JLabel fee1=new JLabel("票价:");
fee1.setFont(new Font("楷体", Font.PLAIN, 15));
fee1.setSize(45, 50);
fee1.setLocation(310, 306);
JLabel chrg1=new JLabel("找零:");
chrg1.setFont(new Font("楷体", Font.PLAIN, 15));
chrg1.setSize(50, 50);
chrg1.setLocation(567, 306);
contentPane.setLayout(null);
getContentPane().add(title);
getContentPane().add(chrg1);
getContentPane().add(fee1);
getContentPane().add(py1);
getContentPane().add(pyn1);
getContentPane().add(inc1);
getContentPane().add(arv1);
getContentPane().add(confirm);
getContentPane().add(arv);
getContentPane().add(py);
getContentPane().add(pyn);
getContentPane().add(inc);
getContentPane().add(fee);
getContentPane().add(chrg);
}
}
这个是弹出的窗口代码:
package Windows;//弹出窗口的代码
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class mainwin extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainwin frame = new mainwin();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public mainwin() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(860,480);
setLocationRelativeTo(null);
setTitle("獒獒自助购票系统");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton confirm =new JButton("确认购票");
confirm.setFont(new Font("宋体", Font.PLAIN, 15));
confirm.setSize(844, 30);
confirm.setLocation(0, 411);
confirm.addActionListener(new ActionListener()//为confirm按钮创建出发后的事件
{
public void actionPerformed(ActionEvent e)
{
confirm frame = new confirm();
frame.setVisible(true);
frame.cmp=1;
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
}
if(frame.cmp==1)
{
frame.setVisible(false);
success frame1 = new success();
frame1.setVisible(true);
}
System.out.println(frame.cmp);
}
});
JTextField arv=new JTextField();//这里是输入目的地的文本框
arv.setSize(160, 40);
arv.setLocation(120, 88);
JTextField py=new JTextField();//这里是输入乘客姓名的文本框
py.setSize(75, 30);
py.setLocation(113, 199);
JTextField pyn=new JTextField();//这里是输入乘客身份证号的文本框
pyn.setSize(426,30);
pyn.setLocation(300,199);
JTextField inc=new JTextField();//这里是输入收到金额的文本框
inc.setSize(100,40);
inc.setLocation(102, 312);
JTextField fee=new JTextField();//这里是显示票价的文本框
fee.setSize(100,40);
fee.setLocation(365, 312);
fee.setEditable(false);
JTextField chrg=new JTextField();//这里是显示找零的文本框
chrg.setSize(100,40);
chrg.setLocation(615, 312);
chrg.setEditable(false);
JLabel title=new JLabel("獒獒自助购票系统");
title.setFont(new Font("楷体", Font.PLAIN, 55));
title.setSize(450,50);
title.setLocation(197, 20);
JLabel arv1=new JLabel("目的地:");
arv1.setFont(new Font("楷体", Font.PLAIN, 20));
arv1.setSize(70, 50);
arv1.setLocation(40, 80);
JLabel py1=new JLabel("乘客姓名:");
py1.setFont(new Font("楷体", Font.PLAIN, 15));
py1.setSize(75,50);
py1.setLocation(35,188);
JLabel pyn1=new JLabel("身份证号:");
pyn1.setFont(new Font("楷体", Font.PLAIN, 15));
pyn1.setSize(75,50);
pyn1.setLocation(215,188);
JLabel inc1=new JLabel("收到金额:");
inc1.setFont(new Font("楷体", Font.PLAIN, 15));
inc1.setSize(75,50);
inc1.setLocation(35, 306);
JLabel fee1=new JLabel("票价:");
fee1.setFont(new Font("楷体", Font.PLAIN, 15));
fee1.setSize(45, 50);
fee1.setLocation(310, 306);
JLabel chrg1=new JLabel("找零:");
chrg1.setFont(new Font("楷体", Font.PLAIN, 15));
chrg1.setSize(50, 50);
chrg1.setLocation(567, 306);
contentPane.setLayout(null);
getContentPane().add(title);
getContentPane().add(chrg1);
getContentPane().add(fee1);
getContentPane().add(py1);
getContentPane().add(pyn1);
getContentPane().add(inc1);
getContentPane().add(arv1);
getContentPane().add(confirm);
getContentPane().add(arv);
getContentPane().add(py);
getContentPane().add(pyn);
getContentPane().add(inc);
getContentPane().add(fee);
getContentPane().add(chrg);
}
}
这个是最后要求的出票结果:
package Windows;//出票结果的代码
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
public class success extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
success frame = new success();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public success() {
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setLocationRelativeTo(null);
setSize(450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel sf=new JLabel("始发");//显示出发站(西安)
sf.setFont(new Font("楷体", Font.PLAIN, 20));
sf.setBounds(62,34,72,50);
JLabel dd=new JLabel("到达");//显示到达站
dd.setFont(new Font("楷体", Font.PLAIN, 20));
dd.setBounds(302, 34, 72, 50);
JLabel py=new JLabel("乘客姓名:");//显示乘客姓名
py.setFont(new Font("楷体", Font.PLAIN, 17));
py.setBounds(41,105,93,50);
JLabel pyn=new JLabel("身份证号:");//显示乘客身份证号
pyn.setFont(new Font("楷体", Font.PLAIN, 15));
pyn.setBounds(41,147,333,29);
JLabel fee=new JLabel("票价:人民币");//显示票价
fee.setFont(new Font("楷体", Font.PLAIN, 16));
fee.setBounds(41,182,148,39);
JLabel zw=new JLabel("座位号:");//显示座位号
zw.setFont(new Font("楷体", Font.PLAIN, 16));
zw.setBounds(236,182,125,39);
JLabel ts=new JLabel("→");//显示特殊符号
ts.setFont(new Font("楷体", Font.PLAIN, 20));
ts.setBounds(193,47,25,24);
contentPane.setLayout(null);
getContentPane().add(sf);
但是如果在购票系统的类里面把等待的语句删除掉就可以正常显示了,不知道为什么,求大神解答