3

为什么我的 JComponent 没有显示在背景 JFrame 之上?

请检查以下代码:

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

}
4

2 回答 2

6

似乎在这里工作正常(在代码的这个 SSCCE 变体中)。

工作代码

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

我能从中得出的唯一结论是:

  1. 找不到您的资源。
  2. 您需要学习基本的调试技能。在这种情况下,特别是“检查每个步骤中发生的事情是否确实有效的假设”。如下所示的“三级”语句应分解为 3 个语句,您可以使用System.out.println()调试器或调试器检查 3 个结果中的每一个。

调试不友好!

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

以及未来的笔记。为了尽快获得更好的帮助,请发布SSCCE

于 2012-04-13T08:01:18.740 回答
2

尝试使用contentPane.add(myComponent);而不是add(myComponent);

于 2012-04-13T06:05:03.740 回答