关闭时的JFrame退出未出现在Java中

时间:2019-02-26 00:17:02

标签: java swing

Oracle中的示例为例,您的documentation中有一些示例。

我的想法如下:

enter image description here

我已经实现我的应用程序具有透明背景,但是最小化和关闭应用程序按钮没有出现

这是我的代码:

主要

import javax.swing.JFrame;
import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class Textmovie extends JFrame {

    /*
    public Textmovie() {
        //setLayout(new GridBagLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    */

    public static void main(String[] args) {
       JFrame jf = new JFrame("");
       jf.setUndecorated(true);
       jf.setBackground(new Color(0,0,0,10));
       //jf.setOpacity(0.55f);
       jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jf.add(new texscroll());
       jf.setSize(720,480);
       jf.setVisible(true);
    }
}

第2部分

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;


/**
 *
 * @author inide
 */
public class texscroll extends JPanel {
    int  x =510 , y = 25;

    public texscroll() {
        setOpaque(false);
    }


    @Override

    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2 = (Graphics2D)g;

        Font font = new Font("Arial",Font.BOLD + Font.PLAIN,15);
        g2.setFont(font);
        g2.setColor(Color.BLACK);

        String string = "stackoverflow stackoverflow stackoverflow stackoverflow";

        g2.drawString(string ,x,y);
        try{Thread.sleep(14);}
        catch(Exception ex)
        {
        };
        x-=1;

            if(x==-10*string.length()){
              x= 510;  
            }
        repaint();

       // System.out.println(string.length()  );
    }
}

这在NetBeans IDE 8.0.2中运行时会显示

enter image description here

他们可以向我解释要使按钮出现(最小化并关闭应用程序)我必须要做的事情。

3 个答案:

答案 0 :(得分:1)

如果您实际上是根据异常来研究代码:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated
    at java.desktop/java.awt.Frame.setBackground(Frame.java:989)

您会发现不可能使框架透明并进行装饰...

@Override
public void setBackground(Color bgColor) {
    synchronized (getTreeLock()) {
        if ((bgColor != null) && (bgColor.getAlpha() < 255) && !isUndecorated()) {
            throw new IllegalComponentStateException("The frame is decorated");
        }
        super.setBackground(bgColor);
    }
}

教程表明它可以正常工作的事实是无关紧要的,并且在教程方面存在错误。

在API的早期“未发布”版本中(使用AWTUtilities,“可能”已实现,但不再可能

现在,我们已经在paint内...

try {
    Thread.sleep(14);
} catch (Exception ex) {
};
x -= 1;

if (x == -10 * string.length()) {
    x = 510;
}
repaint();

不是您在Swing中制作动画的方式

这只会给您带来无尽的麻烦,因为在paintComponent存在之后,什么都不会提交给本地对等体(这就是双缓冲的工作方式)

有关更多详细信息,请参见Concurrency in Swing

更合适的解决方案可能看起来像...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Textmovie extends JFrame {

    public static void main(String[] args) {
        new Textmovie();
    }

    public Textmovie() throws HeadlessException {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame jf = new JFrame("");
                jf.setUndecorated(true);
                jf.setBackground(new Color(0, 0, 0, 10));
                //jf.setOpacity(0.55f);
                jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                jf.add(new texscroll());
                jf.pack();
                jf.setLocationRelativeTo(null);
                jf.setVisible(true);
            }
        });
    }

    public static class texscroll extends JPanel {

        private int x = 510, y = 25;

        private String string = "stackoverflow stackoverflow stackoverflow stackoverflow";

        public texscroll() {
            Font font = new Font("Arial", Font.BOLD + Font.PLAIN, 15);
            setFont(font); 
            setForeground(Color.BLACK);

            setOpaque(false);
            Timer timer = new Timer(14, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    x -= 1;

                    if (x == -10 * string.length()) {
                        x = 510;
                    }
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(720, 480);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
            Graphics2D g2 = (Graphics2D) g;

            g2.drawString(string, x, y);
        }
    }
}

有关更多详细信息,请参见How to Use Swing Timers

答案 1 :(得分:0)

jf.setUndecorated(true);

使标题栏不可见,并且包括最小和关闭按钮,因此您应该删除该行(因为默认情况下为false)

答案 2 :(得分:0)

这是因为您正在拨打jf.setUndecorated(true)。此方法将删除包含最小和最大化按钮的标题栏。

不幸的是,窗口必须没有装饰才能有系统标题栏,但是外观可以提供标题栏。要启用它,必须在使框架可见之前调用它:

JFrame.setDefaultLookAndFeelDecorated(true);
相关问题