为什么我的JComponent不会显示在backgound JFrame的顶部? [英] Why My JComponent is not displayed on top of backgound JFrame?

查看:268
本文介绍了为什么我的JComponent不会显示在backgound JFrame的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的JComponent不会显示在backgound JFrame的顶部?

请查看以下code:

 类相对框架扩展的JFrame {    / **
     *
     * /
    私有静态最后的serialVersionUID长1L =;
    私人MyPanel = myComponent的新MyPanel();
    私人的JLabel的contentPane =新的JLabel(新的ImageIcon(的getClass()
            .getResource(背景/ 2.JPG)));    相对框架(){
        contentPane.setLayout(新的GridBagLayout());
        setContentPane(contentPane的);
        加(myComponent的);
    }    }    类MyPanel继承JPanel {    / **
     *
     * /
    私有静态最后的serialVersionUID长1L =;
    私人字体myFont;
    私人字符串的目标;
    私人字符串提出=200000;
    私人图像背景;    公共MyPanel(){    }    @覆盖
    保护无效paintComponent(图形G){
        super.paintComponent方法(G);        Graphics2D的数组twoD =(Graphics2D的)克;        RenderingHints的RH =新的RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        twoD.setRenderingHints(RH);        文件f =新的文件(字体/ event.ttf);
        尝试{
            myFont = Font.createFont(Font.TRUETYPE_FONT,F);
            myFont = myFont.deriveFont(90F);
        }赶上(FontFormatException此五){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }        twoD.setColor(Color.BLACK);
        twoD.setFont(myFont);        twoD.drawString(凸起,5,90);
    }}


解决方案

似乎做工精细这里(在code这个SSCCE变种)。

 进口java.awt中的*。
进口的javax.swing *。
进口的java.net.URL;类相对框架的扩展JFrame的{    私有静态最后的serialVersionUID长1L =;
    私人MyPanel = myComponent的新MyPanel();
    私人的JLabel的contentPane;    相对框架(){
        尝试{
            网址URL =新的URL(HTTP://ps$c$c.org/media/stromlo2.jpg);
            的contentPane =新的JLabel(新的ImageIcon(URL));
        }赶上(的Throwable t)的{
            t.printStackTrace();
        }
        contentPane.setLayout(新的GridBagLayout());
        setContentPane(contentPane的);
        加(myComponent的);
    }    公共静态无效的主要(字串[] args){
        //创建事件调度线程框架
        SwingUtilities.invokeLater(Runnable的新(){            @覆盖
            公共无效的run(){
                相对框架RC =新的相对框架();
                rc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                rc.pack();
                rc.setVisible(真);
            }        });
    }
    }    类MyPanel继承JPanel {    私有静态最后的serialVersionUID长1L =;
    私人字符串的目标;
    私人字符串提出=200000;
    私人图像背景;    公共MyPanel(){
        集preferredSize(新尺寸(200,100));
    }    @覆盖
    保护无效paintComponent(图形G){
        super.paintComponent方法(G);        Graphics2D的数组twoD =(Graphics2D的)克;        RenderingHints的RH =新的RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        twoD.setRenderingHints(RH);        twoD.setColor(Color.BLACK);        twoD.drawString(凸起,5,90);
    }
}

我可以从这个得出的唯一结论是:


  1. 您的资源没有被发现。

  2. 您需要学习基本的调试技巧。在这种情况下,specificailly'检查presumptions什么在每一步中发生的实际工作。 三层次的语句,如以下应分解为3语句,与您使用检查每3个结果的System.out.println()或调试器。

调试不友好!

 新的JLabel(新的ImageIcon(的getClass()
        .getResource(背景/ 2.JPG)));

和为未来的注释。为了更好的帮助越早,张贴 SSCCE

Why My JComponent is not displayed on top of backgound JFrame?

Please check the following code:

class CounterFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private MyPanel myComponent = new MyPanel();
    private JLabel contentPane = new JLabel(new ImageIcon(getClass()
            .getResource("background/2.jpg")));

    CounterFrame() {
        contentPane.setLayout(new GridBagLayout());
        setContentPane(contentPane);
        add(myComponent);
    }

    }

    class MyPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private Font myFont;
    private String target;
    private String raised = "200000";
    private Image background;

    public MyPanel() {

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D twoD = (Graphics2D) g;

        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        twoD.setRenderingHints(rh);

        File f = new File("fonts/event.ttf");
        try {
            myFont = Font.createFont(Font.TRUETYPE_FONT, f);
            myFont = myFont.deriveFont(90f);
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        twoD.setColor(Color.BLACK);
        twoD.setFont(myFont);

        twoD.drawString(raised,5, 90);
    }

}

解决方案

Seems to work fine here (in this SSCCE variant of the code).

import java.awt.*;
import javax.swing.*;
import java.net.URL;

class CounterFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private MyPanel myComponent = new MyPanel();
    private JLabel contentPane;

    CounterFrame() {
        try {
            URL url = new URL("http://pscode.org/media/stromlo2.jpg");
            contentPane = new JLabel(new ImageIcon(url));
        } catch(Throwable t) {
            t.printStackTrace();
        }
        contentPane.setLayout(new GridBagLayout());
        setContentPane(contentPane);
        add(myComponent);
    }

    public static void main(String[] args) {
        //Create the frame on the event dispatching thread
        SwingUtilities.invokeLater(new Runnable(){

            @Override
            public void run() {
                CounterFrame rc = new CounterFrame();
                rc.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                rc.pack();
                rc.setVisible(true);
            }

        });
    }
    }

    class MyPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private String target;
    private String raised = "200000";
    private Image background;

    public MyPanel() {
        setPreferredSize(new Dimension(200,100));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D twoD = (Graphics2D) g;

        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        rh.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        twoD.setRenderingHints(rh);

        twoD.setColor(Color.BLACK);

        twoD.drawString(raised,5, 90);
    }
}

The only conclusion I can draw from this is that:

  1. Your resource is not being found.
  2. You need to learn basic debugging skills. In this case, specificailly 'checking the presumptions that what is happening in each step is actually working'. 'Triple level' statements such as the following should be broken down to 3 statements, with you checking each of the 3 results using System.out.println() or a debugger.

Debug unfriendly!

new JLabel(new ImageIcon(getClass()
        .getResource("background/2.jpg")));

And a note for future. For better help sooner, post an SSCCE.

这篇关于为什么我的JComponent不会显示在backgound JFrame的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆