冬晨新曦有热风 2018-12-06 01:52 采纳率: 0%
浏览 629

java为什么使用等待语句后弹出的窗口组件都不显示了?

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);

但是如果在购票系统的类里面把等待的语句删除掉就可以正常显示了,不知道为什么,求大神解答

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-07 09:44
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    从你的描述来看,你可能遇到了以下问题:

    1. mainwin类中,当点击“确认购票”按钮时,确实会有三个子组件(输入框、票价文本框和找到的金额)显示。然而,在这些子组件上添加了事件监听器(actionListener),并且在调用setVisible(false)之前,这三个组件都隐藏了。

    2. 当你尝试将上述事件监听器移到mainwin类之外,并在购票系统的窗体中添加相同的事件监听器时,它们仍然无法正确工作,因为这些事件监听器被绑定到了mainwin类中。

    3. 为了使这些组件能够正常显示并响应用户操作,你需要确保你在购票系统的窗体中也添加相应的事件监听器来处理这些组件的行为。

    以下是修改后的购票系统的部分代码示例,其中包含了事件监听器的添加:

    package Windows;
    
    import javax.swing.*;
    
    public class mainwin extends JFrame {
    
        private JPanel contentPane;
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                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.addActionListener(e -> {
                confirm frame = new confirm();
                frame.setVisible(true);
                frame.cmp = 1;
                try {
                    Thread.sleep(3000); // 延迟3秒
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                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);
    
            setVisible(true);
        }
    }
    

    在这个修改后的版本中,我将事件监听器移出了mainwin类,并分别添加到了每个子组件上。这样做的目的是确保所有组件都能正确地响应用户的操作。

    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA