【问题标题】:repaint() method not calling paintCompnentrepaint() 方法不调用paintCompnent
【发布时间】:2018-10-26 18:37:33
【问题描述】:

我正在尝试制作一个JFrame 程序,该程序将 binary-decimal-hexadecimal 转换器与按钮接口。其中的一部分,我想画圆圈来代表一排圆圈中的二进制数,实心圆圈代表一,空心圆圈代表零。但是,当我尝试调用 repaint 来绘制任何东西时,它不会执行 paintComponent() 方法,我知道这是因为它不执行其中的 print 语句。完整代码如下:

package e2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.FlowLayout;

public class DecBin extends JPanel {

    String theText = "IT WORKS";
    int style = Font.BOLD; // bold only
    Font font = new Font("Arial", style, 40);

    DecBin() {
        JFrame f = new JFrame("Decimal to binary and binary to decimal converter");
        f.setSize(1000, 1000);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new FlowLayout());

        Font font = new Font("Arial", style, 40);
        JLabel textLabel = new JLabel(theText);
        textLabel.setFont(font);

        JButton b = new JButton("DectoBin");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = JOptionPane.showInputDialog("input a decimal number");
                int num = Integer.parseInt(msg);
                theText = num + " is " + decToBin(num) + " in binary";
                textLabel.setText(theText);
            }
        });
        f.add(b);

        JButton d = new JButton("BintoDec");
        d.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = JOptionPane.showInputDialog("input a binary number");
                System.out.print("The decimal form of " + msg + " is " + binToDec(msg));
                theText = msg + " is " + binToDec(msg) + " in decimal";
                textLabel.setText(theText);
            }
        });
        f.add(d);

        JButton hd = new JButton("hexToDec");
        hd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = JOptionPane.showInputDialog("input a hexadecimal number");
                theText = msg + " is " + hexToDec(msg) + " in decimal";
                textLabel.setText(theText);
            }
        });
        f.add(hd);

        JButton dh = new JButton("DectoHex");
        dh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = JOptionPane.showInputDialog("input a decimal number");
                int num = Integer.parseInt(msg);
                theText = num + " is " + decToHex(num) + " in hexadecimal";
                textLabel.setText(theText);
            }
        });
        f.add(dh);

        JButton bh = new JButton("BinToHex");
        bh.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = JOptionPane.showInputDialog("input a binary number");
                theText = msg + " is " + decToHex(binToDec(msg)) + " in hexadecimal";
                textLabel.setText(theText);
            }
        });

        f.add(bh);

        JButton hb = new JButton("HexToBin");
        hb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String msg = JOptionPane.showInputDialog("input a Hexadecimal number");
                theText = msg + " is " + decToBin(hexToDec(msg)) + " in hexadecimal";
                textLabel.setText(theText);
            }
        });
        f.add(hb);

        JButton c = new JButton("Draw");
        c.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.print("test1");
                validate();
                repaint();
            }
        });
        f.add(c);
        f.add(textLabel, BorderLayout.SOUTH);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new DecBin();
    }

    static String decToBin(int d) {
        StringBuilder binary = new StringBuilder("");
        while (d != 0) {
            binary.insert(0, d % 2);
            d = d / 2;
        }
        return binary.toString();
    }

    static int binToDec(String b) {
        int total = 0;
        int x;
        for (int i = b.length(); i > 0; i--) {
            x = Integer.parseInt(Character.toString(b.charAt(i - 1)));
            if (x == 1) {
                total += Math.pow(2, b.length() - i);
            }
        }
        return total;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.red);
        g2.fillOval(100, 100, 20, 20);
        System.out.print("painted components");
    }

    static String decToHex(int d) {
        StringBuilder Hex = new StringBuilder("");
        while (d != 0) {
            int remainder = d % 16;
            if (remainder < 10) {
                Hex.insert(0, d % 16);
            } else if (remainder == 10) {
                Hex.insert(0, 'A');
            } else if (remainder == 11) {
                Hex.insert(0, 'B');
            } else if (remainder == 12) {
                Hex.insert(0, 'C');
            } else if (remainder == 13) {
                Hex.insert(0, 'D');
            } else if (remainder == 14) {
                Hex.insert(0, 'E');
            } else if (remainder == 15) {
                Hex.insert(0, 'F');
            }
            d = d / 16;
        }
        return Hex.toString();
    }

    static int hexToDec(String b) {
        int total = 0;
        int len = b.length();
        for (int i = len - 1; i >= 0; i--) {
            if (b.charAt(i) == 'A') {
                total += 10 * Math.pow(16, len - i - 1);
            } else if (b.charAt(i) == 'B') {
                total += 11 * Math.pow(16, len - i - 1);
            } else if (b.charAt(i) == 'C') {
                total += 12 * Math.pow(16, len - i - 1);
            } else if (b.charAt(i) == 'D') {
                total += 13 * Math.pow(16, len - i - 1);
            } else if (b.charAt(i) == 'E') {
                total += 14 * Math.pow(16, len - i - 1);

            } else if (b.charAt(i) == 'F') {
                total += 15 * Math.pow(16, len - i - 1);
            } else {
                total += Character.getNumericValue(b.charAt(i)) * Math.pow(16, len - i - 1);
            }
        }
        return total;
    }
}

我认为相关的代码是:

DecBin() {
    JFrame f=new JFrame("Decimal to binary and binary to decimal converter");
    f.setSize(1000,1000);    
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new FlowLayout());
    JButton c =new JButton("Draw");    
    c.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) {
            System.out.print("test1");
            validate();
            repaint();
        }
    });
    f.add(c);
    f.setVisible(true);    
}

public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.red);
        g2.fillOval(100, 100, 20, 20);
        System.out.print("painted components");
}
public static void main(String[] args) {
    new DecBin();
}

我已经花了几个小时研究这个问题,但还没有找到一个可以打印“绘制组件”的解决方案。

【问题讨论】:

  • JPanel 子类构造函数中初始化JFrame 是一种非常不标准的做事方式,并且可能是导致您的问题的一个因素。您应该在 main 方法中初始化 JFrame,然后在其中添加您的 DecBin

标签: java swing jframe paintcomponent repaint


【解决方案1】:

您似乎没有将您的DecBin 面板adding 到任何其他UI 元素。因此,它没有理由被画出来。

您应该避免在 JPanel 子类的构造函数中初始化 JFrames - 它实际上应该是相反的。这将使事情更容易理解(和调试)。

【讨论】:

  • 阅读 Custom Painting 上的 Swing 教程部分。下载工作示例并根据您的要求进行定制。演示代码结构合理。
  • 其实,反之亦然。您应该避免不必要的子类化,因此,初始化代码根本不需要在构造函数中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2013-12-25
  • 1970-01-01
  • 2011-02-22
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多