简体   繁体   English

JFrame没有出现红色背景

[英]JFrame doesn't appear with red background

I have strange problem when running my Java program. 运行Java程序时出现奇怪的问题。 It's designed to: 它旨在:

  1. run external app specified in bat file and display fullscreen wallpaper 运行bat文件中指定的外部应用并显示全屏墙纸
  2. "hide" wallpaper for some time when buttons combination pressed 按下按钮组合时“隐藏”墙纸一段时间
  3. warn user that 5sec left so he can save work 警告用户还剩5秒,这样他可以节省工作
  4. when timeout occures display again fullscreen wallpaper and do some other stuff from bats 发生超时时,再次显示全屏墙纸,并从蝙蝠中进行其他操作
  5. quit program when button combination pressed 按下按钮组合时退出程序

warinng user is realized as displaying fullscreen red box for 200ms I'm using visible function to do this. warinng用户被实现为在200ms内显示全屏红色框,我正在使用可见功能来做到这一点。

It shows standard fullscreen frame discaring color settings. 它显示了标准的全屏帧显示颜色设置。 but only when I comment frame.setUndecorated(true) . 但仅当我评论frame.setUndecorated(true) when uncommented I see only icon in taskbar. 当取消注释时,我在任务栏中仅看到图标。

On the other hand when I launch (Using BlueJ) only function visible the red frame is displayed for specified amount of time. 另一方面,当我启动(使用BlueJ)时,仅可见的功能会在指定的时间内显示红框。 Simply standalone function works perfectly (in my oppinion) even if frame.setUndecorated(true) is used . 即使使用frame.setUndecorated(true)简单的独立功能也可以完美地发挥作用(在我frame.setUndecorated(true)

what can be wrong that I can't launch that red frame in fullscreen? 我无法在全屏模式下启动该红色框可能有什么问题?

olympicApp class: olympicApp类:

import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Graphics;
import java.awt.image.*;
import java.io.*;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComponent;

public class olympicApp extends JComponent {
    alertApp alert;  
    BufferedImage img;
    public olympicApp()
    {
        try 
        {
            img = ImageIO.read(new File("wallpaper.jpg"));
        } 
        catch (IOException e) 
        {
        }
    }

    public void paint(Graphics g) 
    {
        g.drawImage(img, 0, 0, null);
    }

    public Dimension getPreferredSize() 
    {
        if (img == null) 
        {
            return new Dimension(200,200);
        } 
        else 
        {
            return new Dimension(img.getWidth(null), img.getHeight(null));
        }
    }

    public static void visible()
    {
        JFrame frame = new JFrame();
        frame.getContentPane().setBackground(Color.red);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.dispose();
        //frame.setUndecorated(true);
        frame.setAlwaysOnTop(true);
        frame.pack();
        frame.setVisible(true);
        try 
        {
            frame.setVisible(true);
            Thread.sleep(500);
            frame.setVisible(false);
        } 
        catch(Exception ex) 
        {
        }
        frame.setAlwaysOnTop(false);
        frame.setVisible(false);
    }

    public static void main(String[] args)
    {
        //alertApp reminder = new alertApp();
        try
        {
            Process process = Runtime.getRuntime().exec("start-lv.bat");
            Thread.sleep(500);
        }
        catch (IOException | InterruptedException e)
        {
        }
        JFrame f = new JFrame("olympic");
        f.setExtendedState(JFrame.MAXIMIZED_BOTH);
        f.setUndecorated(true);
        f.setAlwaysOnTop(true);     
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e) 
            {
                System.exit(0);
            }
        });
        f.add(new olympicApp());
        f.pack();
        f.setVisible(true);
        f.addKeyListener(new KeyListener() 
        {
            public void keyPressed(KeyEvent kevt)
            {
                if(kevt.getKeyChar()=='l') 
                {
                    if(kevt.isAltDown())
                    {
                        f.setAlwaysOnTop(false);
                        f.setVisible(false);
                        try 
                        {
                            Thread.sleep(5*1000);
                            visible();
                            Thread.sleep(5*1000);
                            //Process process = Runtime.getRuntime().exec("saving.bat");
                            Thread.sleep(500);
                            f.setAlwaysOnTop(true);
                            f.setVisible(true);
                            Process process2 = Runtime.getRuntime().exec("kopia.bat");
                        } 
                        catch(IOException | InterruptedException e)
                        {
                        }
                    }                 
                }

                if(kevt.getKeyChar()=='q') 
                {
                    if(kevt.isAltDown())
                    {
                        System.exit(0);
                    }                 
                }
            }

            public void keyTyped(KeyEvent kevt) 
            {
            }

            public void keyReleased(KeyEvent kevt) 
            {
            }
        });
    }
}

I would imagine that you're going to have to do something in here... 我想您将不得不在这里做些什么...

public class alertApp {

    public static void main(String[] args) {
        // Sample loop to flash every 2 seconds
    }
}

This is called when your program starts and where you should put the code to get the program running 这在程序启动时以及应在其中放置代码以使程序运行的位置被调用

I would also strogly recommend that you take a look at Code Conventions for the Java TM Programming Language , it will make it easier for people to read your code and for you to read others 我还建议您仔细阅读Java TM编程语言的代码约定 ,这将使人们更容易阅读您的代码,并使您更容易阅读其他代码。

As well as Concurrency in Swing and How to use Swing Timers which will help you solve other potential issues 以及Swing中的并发性如何使用Swing计时器 ,这将帮助您解决其他潜在问题

I put neccessary code in visible funciton. 我把必要的代码放在可见的函数中。 I can see the frame but color information is discarted. 我可以看到框架,但是颜色信息不合理。 Thanks for Concurrency and Timers, but I think the problem isn't connected to thread.sleep() 感谢并发和计时器,但我认为问题不在于thread.sleep()

Then you didn't read the links... 然后,您没有阅读链接...

public void keyPressed(KeyEvent kevt) {
    if (kevt.getKeyChar() == 'l') {
        if (kevt.isAltDown()) {
            f.setAlwaysOnTop(false);
            f.setVisible(false);
            try {
                Thread.sleep(5 * 1000);
                visible();
                Thread.sleep(5 * 1000);
                //Process process = Runtime.getRuntime().exec("saving.bat");
                Thread.sleep(500);
                f.setAlwaysOnTop(true);
                f.setVisible(true);
                Process process2 = Runtime.getRuntime().exec("kopia.bat");
            } catch (IOException | InterruptedException e) {
            }
        }
    }

keyPressed is executed within the context of the Event Dispatching Thread, so when you call Thread.sleep , it prevents the EDT from processing the Event Queue, preventing it from painting or responding to other events keyPressed是在事件调度线程的上下文中执行的,因此,当您调用Thread.sleep ,它将阻止EDT处理事件队列,从而阻止其绘制或响应其他事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM