【问题标题】:Java swing not drawing paintComponentJava swing不绘制paintComponent
【发布时间】:2016-08-06 12:23:14
【问题描述】:

我试图在带有一些滚动条和文本字段的框架上绘制一个简单的矩形(只是测试),但它没有显示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();


    }

}

【问题讨论】:

  • 1) 您永远不会创建添加到某处的frame 实例,因此永远不会调用paintComponent。 2)应该是public void paintComponent(Graphics g),下次在方法上面打上@Override注解,避免出现此类错误。 3)不要忘记在paintComponent方法的开头调用super.paintComponent(g)
  • 嘿,谢谢你的帮助,我不太了解框架部分的实例,我应该创建另一个 JFrame 并添加(框架)到它吗?
  • frame,奇怪的是(也许考虑另一个名字以避免混淆),是一个JPanel。为了能够看到JPanel,以及通过paintComponent(g) 在其上绘制的内容,必须将其添加到另一个组件(不一定是JFrame)。由于我真的不知道面板的预期效果,我只能猜测p1 实际上应该是一个frame 实例。如果这没有帮助,请说明你想在哪里画什么。

标签: java swing paintcomponent


【解决方案1】:

问题

你没有遵守约定。这是造成问题的小错误。您的frame 类实际上是JPanel,而不是JFrame

有两个主要问题:您从未将面板添加到框架中,并且paintComponent() 方法有一个 Graphics 对象,而不是 Graphics2D 对象作为参数。

对代码的更改位于此答案的底部。


解决方案

  1. 遵守约定。 (您还应该将您的类重命名为更合适的名称,但这是您的选择。)将@Override 注释添加到您的paintComponent() 方法,因为您希望从原始JPanel 覆盖此方法。如果它因为注释而崩溃,则意味着您没有正确覆盖。
  2. paintComponent() 参数从Graphics2D 更改为Graphics
  3. JPanel 添加到JFrame
  4. 确保在您的JPanel 上调用setPreferredSize() 并指定尺寸。
  5. 在显示之前在JFrame 上调用pack(),以便布局管理器可以相应地放置所有内容。

现在我敢肯定,到这一切结束时,您仍然不会对所看到的内容感到满意,因为代码仍然需要一些工作,但至少这应该可以推动您朝着正确的方向前进。此外,您可能想在绘制矩形之前调用setColor()。 ;)

希望这会有所帮助。


代码

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


    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    相关资源
    最近更新 更多