显示自定义 JComponent 时出现问题

发布于 2024-11-07 15:21:08 字数 2699 浏览 0 评论 0原文

嘿伙计们,我遇到了一些问题,看起来应该更简单。我只是无法让我的简单自定义 JComponent 显示出来!我选择使用绝对定位,我不确定这是否会导致一些问题。任何建议和/或>解决方案<非常感谢。谢谢!

(这是我的代码)

public class XtremePaintballNetwork {

    private static JFrame _xpbnWindow;
    private static JTextField _chatTextField;
    //private static Map _map;
    private static Map _map;

    public static void main(String[] args) {
        // Set up main window
        _xpbnWindow = new JFrame("Xtreme Paintball Network");
        _xpbnWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _xpbnWindow.setSize(400, 300);;
        //_xpbnWindow.setBackground(Color.white);
        //_xpbnWindow.getContentPane().setBackground(Color.white);
        //_xpbnWindow.pack();
        _xpbnWindow.setVisible(true);


        addComponentsToPane();

        _xpbnWindow.addComponentListener( new ComponentListener() {
            public void componentHidden(ComponentEvent e) {}
            public void componentMoved(ComponentEvent e){}
            public void componentResized(ComponentEvent e) {
                adjustBounds();
            }
            public void componentShown(ComponentEvent e) {}
        });

        _xpbnWindow.repaint();
    }

    private static void addComponentsToPane() {
        Container pane = _xpbnWindow.getContentPane();

        // Use Absolute Positioning
        pane.setLayout(null);

        // Create GUI components
        _map = new Map();
        _chatTextField = new JTextField();

        // Add components to pane
        pane.add(_map);
        pane.add(_chatTextField);

        // Calculate and set Bounds
        adjustBounds();
    }

    private static void adjustBounds() {
        Container pane = _xpbnWindow.getContentPane();


        // Use 'null' layout -> Absolute Positioning
        Insets insets = pane.getInsets();
        Dimension _windowDimension = pane.getSize();
        Dimension _chatDimension = _chatTextField.getPreferredSize();
        /*_map.setBounds(0, insets.top, _windowDimension.width - insets.left - insets.right,
                _windowDimension.height - insets.top - insets.bottom);*/
        _map.setBounds(10, 10, 100, 100);
        _chatTextField.setBounds(0, _windowDimension.height - _chatDimension.height - insets.top - insets.bottom,
             _windowDimension.width - insets.left - insets.right, _chatDimension.height);

    }

}

这是简单的 JComponent 类

public class Map extends JComponent{


    //@Override
    protected void PaintComponent(Graphics g){
        super.paintComponent(g);
        g.drawLine(0, 0, 70, 70);
        g.drawString("string",20,20);
    }
}

基本上,我的问题是我的 JFrame 中没有显示任何内容...:/帮助!

Hey guys, I'm having problems with something that seems like it should be so much more simple. I just cant get my simple custom JComponent to show up! I'm using Absolute Positioning by choice, and I'm not sure if that could be causing some of the problems. Any advice and or a >solution< is greatly appreciated. Thankss!

(heres my code)

public class XtremePaintballNetwork {

    private static JFrame _xpbnWindow;
    private static JTextField _chatTextField;
    //private static Map _map;
    private static Map _map;

    public static void main(String[] args) {
        // Set up main window
        _xpbnWindow = new JFrame("Xtreme Paintball Network");
        _xpbnWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _xpbnWindow.setSize(400, 300);;
        //_xpbnWindow.setBackground(Color.white);
        //_xpbnWindow.getContentPane().setBackground(Color.white);
        //_xpbnWindow.pack();
        _xpbnWindow.setVisible(true);


        addComponentsToPane();

        _xpbnWindow.addComponentListener( new ComponentListener() {
            public void componentHidden(ComponentEvent e) {}
            public void componentMoved(ComponentEvent e){}
            public void componentResized(ComponentEvent e) {
                adjustBounds();
            }
            public void componentShown(ComponentEvent e) {}
        });

        _xpbnWindow.repaint();
    }

    private static void addComponentsToPane() {
        Container pane = _xpbnWindow.getContentPane();

        // Use Absolute Positioning
        pane.setLayout(null);

        // Create GUI components
        _map = new Map();
        _chatTextField = new JTextField();

        // Add components to pane
        pane.add(_map);
        pane.add(_chatTextField);

        // Calculate and set Bounds
        adjustBounds();
    }

    private static void adjustBounds() {
        Container pane = _xpbnWindow.getContentPane();


        // Use 'null' layout -> Absolute Positioning
        Insets insets = pane.getInsets();
        Dimension _windowDimension = pane.getSize();
        Dimension _chatDimension = _chatTextField.getPreferredSize();
        /*_map.setBounds(0, insets.top, _windowDimension.width - insets.left - insets.right,
                _windowDimension.height - insets.top - insets.bottom);*/
        _map.setBounds(10, 10, 100, 100);
        _chatTextField.setBounds(0, _windowDimension.height - _chatDimension.height - insets.top - insets.bottom,
             _windowDimension.width - insets.left - insets.right, _chatDimension.height);

    }

}

and here's the simple JComponent class

public class Map extends JComponent{


    //@Override
    protected void PaintComponent(Graphics g){
        super.paintComponent(g);
        g.drawLine(0, 0, 70, 70);
        g.drawString("string",20,20);
    }
}

Basically, my problem is that nothing shows up within my JFrame... :/ Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捂风挽笑 2024-11-14 15:21:08

方法名称是paintComponent。它以小写开头: 链接

The methods name is paintComponent. It starts lower case: Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文