简体   繁体   English

Java Swing未绘制paintComponent

[英]Java swing not drawing paintComponent

Im trying to draw a simple rectangle on a frame with some scrollbars and textfields(just testing) but the paintComponent its not showing, ive seen some similar cases here but i cant manage to make it work, any help please? 我试图在一个带有一些滚动条和文本字段的框架上绘制一个简单的矩形(只是测试),但是paintComponent没有显示,我在这里看到过类似的情况,但是我无法使其工作,请帮忙吗?

package appletdeslizadores;

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

public class frame extends JPanel {

    JFrame f1;
    JPanel p1, p2;
    JLabel lbl1, lbl2, lbl3;
    JTextField txtfld1, txtfld2, txtfld3;
    JScrollBar sbar1, sbar2, sbar3;       


    public frame() {

        f1 = new JFrame("Applet ScrollBars");
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setSize(380, 350);
        f1.setLayout(new FlowLayout());  
        p1 = new JPanel(new GridLayout(3,3,10,10));
        lbl1 = new JLabel("Scroll Bar 1");
        lbl2 = new JLabel("Scroll Bar 2");
        lbl3 = new JLabel("Scroll Bar 3");
        sbar1 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 10, 0, 255);
        sbar2 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 10, 0, 255);
        sbar3 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 10, 0, 255);
        txtfld1 = new JTextField(3);
        txtfld1.setText(String.valueOf(sbar1.getValue()));
        txtfld1.setEditable(false);
        txtfld2 = new JTextField(3);
        txtfld2.setText(String.valueOf(sbar2.getValue()));
        txtfld2.setEditable(false);
        txtfld3 = new JTextField(3);
        txtfld3.setText(String.valueOf(sbar3.getValue()));
        txtfld3.setEditable(false);
        p1.add(lbl1);
        p1.add(lbl2);
        p1.add(lbl3);
        p1.add(sbar1);
        p1.add(sbar2);
        p1.add(sbar3);
        p1.add(txtfld1);
        p1.add(txtfld2);
        p1.add(txtfld3);
        f1.add(p1);
        f1.setVisible(true);

    }


    public void paintComponent(Graphics2D g) {

        g.drawRect(50,50,70,100);
        g.setColor(Color.red);        

    }

    public static void main(String[] args) {

        new frame();


    }

}

Problem 问题

You're not sticking to conventions. 您不遵守约定。 This is causing problems small mistakes. 这是导致问题的小错误。 Your frame class is actually a JPanel , not a JFrame . 您的frame类实际上是一个JPanel ,而不是JFrame

There are two main problems: You never added the panel to the frame, and the paintComponent() method has a Graphics object, not a Graphics2D object as a parameter. 主要有两个问题:从未将面板添加到框架, paintComponent()方法将Graphics对象而不是Graphics2D对象作为参数。

The changes to your code are at the bottom of this answer. 对代码的更改位于此答案的底部。


Solution

  1. Stick to conventions. 遵守约定。 (You should also rename your class to a more appropriate name, but that is your choice.) Add the @Override annotation to your paintComponent() method since you wish to override this method from the original JPanel . (您也应该将类重命名为一个更合适的名称,但这是您的选择。)将@Override注释添加到paintComponent()方法中,因为您希望从原始JPanel覆盖此方法。 If it crashes because of the annotation, it means you are not overriding correctly. 如果由于注释而崩溃,则意味着您没有正确覆盖。
  2. Change the paintComponent() parameter from Graphics2D to Graphics . paintComponent()参数从Graphics2D更改为Graphics
  3. Add the JPanel to the JFrame . JPanel添加到JFrame
  4. Make sure you call setPreferredSize() on your JPanel and specify a size. 确保在JPanel上调用setPreferredSize()并指定大小。
  5. Call pack() on the JFrame right before making it visible so the layout manager can place everything accordingly. 在使其可见之前,立即在JFrame上调用pack() ,以便布局管理器可以相应地放置所有内容。

Now I'm sure by the end of all this you are still not going to be happy with what you see because the code still needs some work, but at least this should give you a boost into the right direction. 现在,我确信所有这些操作的最后,您仍然不会对看到的内容感到满意,因为代码仍然需要一些工作,但是至少这应该可以使您朝正确的方向发展。 Also, you may want to call setColor() before drawing the rectangle. 另外,您可能需要在绘制矩形之前调用setColor() ;) ;)

Hope this helped. 希望这会有所帮助。


Code

public class frame extends JPanel {

    JFrame f1;
    JPanel p1, p2;
    JLabel lbl1, lbl2, lbl3;
    JTextField txtfld1, txtfld2, txtfld3;
    JScrollBar sbar1, sbar2, sbar3;       


    public frame() {

        f1 = new JFrame("Applet ScrollBars");
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setSize(380, 350);
        f1.setLayout(new FlowLayout());  
        p1 = new JPanel(new GridLayout(3,3,10,10));
        lbl1 = new JLabel("Scroll Bar 1");
        lbl2 = new JLabel("Scroll Bar 2");
        lbl3 = new JLabel("Scroll Bar 3");
        sbar1 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 10, 0, 255);
        sbar2 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 10, 0, 255);
        sbar3 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 10, 0, 255);
        txtfld1 = new JTextField(3);
        txtfld1.setText(String.valueOf(sbar1.getValue()));
        txtfld1.setEditable(false);
        txtfld2 = new JTextField(3);
        txtfld2.setText(String.valueOf(sbar2.getValue()));
        txtfld2.setEditable(false);
        txtfld3 = new JTextField(3);
        txtfld3.setText(String.valueOf(sbar3.getValue()));
        txtfld3.setEditable(false);
        p1.add(lbl1);
        p1.add(lbl2);
        p1.add(lbl3);
        p1.add(sbar1);
        p1.add(sbar2);
        p1.add(sbar3);
        p1.add(txtfld1);
        p1.add(txtfld2);
        p1.add(txtfld3);
        f1.add(p1);
        setPreferredSize(new Dimension(512, 512));
        f1.add(this);
        f1.pack();
        f1.setVisible(true);


    }

    @Override
    public void paintComponent(Graphics g) {

        g.drawRect(50,50,70,100);
        g.setColor(Color.red);        

    }

    public static void main(String[] args) {

        new frame();


    }

}

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

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