repaint()方法未调用paintCompnent

时间:2018-10-26 18:37:34

标签: java swing jframe paintcomponent repaint

我正在尝试制作一个{<1}}程序,该程序将 binary-decimal-hexadecimal 转换器与按钮连接。部分原因是,我想在一行圆中绘制表示二进制数的圆,其中实心圆表示一个,空圆表示零。但是,当我尝试调用repaint绘制任何内容时,它不会执行JFrame方法,我知道这是因为它不会在其中执行print语句。完整代码如下:

paintComponent()

我认为相关代码是:

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;
    }
}

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

1 个答案:

答案 0 :(得分:1)

您似乎没有addDecBin面板访问任何其他UI元素。因此,没有理由要涂漆。

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