【问题标题】:paintComponent always update rectanglepaintComponent 总是更新矩形
【发布时间】:2015-03-07 09:16:07
【问题描述】:

我在使用矩形时遇到了一些问题

这里是我的代码:

import java.awt.*;
import javax.swing.*;
import java.util.*;
public class square
{
    JFrame fr=new JFrame("square");
    Random acak=new Random();
    JScrollPane sc;
    int tinggi,lebar;
    JTabbedPane tab=new JTabbedPane();
    square()
    {   
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setLocation(0,0);
        fr.setExtendedState(JFrame.MAXIMIZED_BOTH);
        fr.setLayout(new GridLayout(1,1));
        tab.addTab("Panel1",new panel1());
        tab.addTab("Panel2",new panel2());
        fr.add(tab);    
        fr.show();
    }
    public static void main(String[] args)
    {
        new square();
    }
}
class panel1 extends JPanel
{
    panel1()
    {
    }
    public void paintComponent(Graphics g3)
    {
        int x=(int)(Math.random()*500);
        int y=(int)(Math.random()*500);
        g3.setColor(Color.red);
        g3.fillRect(x,y,100,100);
    }
}
class panel2 extends JPanel
{
    panel2()
    {
    }
    public void paintComponent(Graphics g2)
    {
        int x=(int)(Math.random()*500);
        int y=(int)(Math.random()*500);
        g2.setColor(Color.blue);
        g2.fillRect(x,y,100,100);
    }
}

我随机使用坐标和大小

如果我切换标签,矩形的坐标和大小总是会改变。

第一次怎么制作和协调???

【问题讨论】:

  • 不要在 paintComponent 方法中创建/更改绘制状态 (x, y)。像在构造函数中一样设置它,然后在paintComponent 方法中使用它。
  • 你能举个例子吗???
  • 只需制作面板类的int x, y字段,然后在构造函数public panel() { x = .., y = .. }中初始化它们,然后在paint方法中使用x和y。如果您不了解字段和构造函数,我强烈建议您在使用 GUI 之前先了解 Java 基础知识
  • 抱歉,我的 IDE 没有打开。

标签: java swing paintcomponent rectangles


【解决方案1】:

首先查看Painting in AWT and Swing 以了解绘画的工作原理。

基本上,当系统认为您的组件需要更新时,您的 paintComponent 方法将被(间接)调用,并预计会呈现组件的当前状态。

绘画的发生可能有多种原因,其中大部分是您无法控制的。

与其在paintComponent 方法中计算矩形的状态,不如使用一种您可以控制自己的方法来执行此操作,并让paintComponent 方法简单地进行绘画。

【讨论】:

    【解决方案2】:

    这里:

    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    public class square
    {
        JFrame fr=new JFrame("square");
        Random acak=new Random();
        JScrollPane sc;
        int tinggi,lebar;
        JTabbedPane tab=new JTabbedPane();
        gambar()
        {   
            fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fr.setLocation(0,0);
            fr.setExtendedState(JFrame.MAXIMIZED_BOTH);
            fr.setLayout(new GridLayout(1,1));
            tab.addTab("Panel1",new panel1());
            tab.addTab("Gambar",new panel2());
            fr.add(tab);    
            fr.show();
        }
        public static void main(String[] args)
        {
            new square();
        }
    }
    class panel1 extends JPanel
    {
        int x,y;
        panel1()
        {
            x=(int)(Math.random()*500);
            y=(int)(Math.random()*500);
        }
        public void paintComponent(Graphics g3)
        {
    
            g3.setColor(Color.red);
            g3.fillRect(x,y,x,y);
        }
    }
    class panel2 extends JPanel
    {
        int x,y;
        panel2()
        {
            x=(int)(Math.random()*500);
            y=(int)(Math.random()*500);
        }
        public void paintComponent(Graphics g2)
        {
    
            g2.setColor(Color.blue);
            g2.fillRect(x,y,x,y);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-04
      • 1970-01-01
      相关资源
      最近更新 更多