图片或图片覆盖Java

时间:2014-09-11 13:31:15

标签: java swing layout-manager

我正在尝试制作一个计算器,到目前为止这是我的代码。所以我的问题是,一旦我点击下一个按钮,结果就会重叠。我必须从给定的答案输入答案,如果答案是正确的,那么它将给我一个绿色图像,如果它是错误的,它将给我一个红色图像。但是,然后,当我点击下一个...图像重叠时,数字有时不会移动。请帮忙。

    public Calculatorx() {
        con = getContentPane();
        con.setLayout(null);
        JLabel subTitle = new JLabel(" Elementary Math Tutor ");
        con.add(subTitle);
        subTitle.setBounds(150, 1, 200, 70);
        rand1 = 1 + (int) (Math.random() * 9);
        rand2 = 1 + (int) (Math.random() * rand1);
        while (rand1 % rand2 != 0) {
            rand2 = 1 + (int) (Math.random() * rand1);
        }
        jbtadd1 = new JButton("" + rand1);
        jbtsub1 = new JButton("" + rand1);
        jbtmul1 = new JButton("" + rand1);
        jbtdiv1 = new JButton("" + rand1);
        con.add(jbtadd1);
        con.add(jbtsub1);
        con.add(jbtmul1);
        con.add(jbtdiv1);
        jbtadd1.setBounds(50, 50, 50, 50);
        jbtsub1.setBounds(50, 120, 50, 50);
        jbtmul1.setBounds(50, 190, 50, 50);
        jbtdiv1.setBounds(50, 260, 50, 50);
        JLabel plus = new JLabel(plusIcon);
        JLabel minus = new JLabel(minusIcon);
        JLabel mul = new JLabel(mulIcon);
        JLabel div = new JLabel(divIcon);
        con.add(plus);
        con.add(minus);
        con.add(mul);
        con.add(div);
        plus.setBounds(100, 50, 150, 50);
        minus.setBounds(80, 120, 200, 50);
        mul.setBounds(60, 190, 250, 50);
        div.setBounds(40, 260, 300, 50);
        jbtadd2 = new JButton("" + rand2);
        jbtsub2 = new JButton("" + rand2);
        jbtmul2 = new JButton("" + rand2);
        jbtdiv2 = new JButton("" + rand2);
        con.add(jbtadd2);
        con.add(jbtsub2);
        con.add(jbtmul2);
        con.add(jbtdiv2);
        jbtadd2.setBounds(250, 50, 50, 50);
        jbtsub2.setBounds(250, 120, 50, 50);
        jbtmul2.setBounds(250, 190, 50, 50);
        jbtdiv2.setBounds(250, 260, 50, 50);
        JLabel equal1 = new JLabel(equalIcon);
        JLabel equal2 = new JLabel(equalIcon);
        JLabel equal3 = new JLabel(equalIcon);
        JLabel equal4 = new JLabel(equalIcon);
        con.add(equal1);
        con.add(equal2);
        con.add(equal3);
        con.add(equal4);
        equal1.setBounds(330, 40, 50, 50);
        equal2.setBounds(330, 110, 50, 50);
        equal3.setBounds(330, 180, 50, 50);
        equal4.setBounds(330, 250, 50, 50);
        addAnswer = new JTextField();
        subAnswer = new JTextField();
        mulAnswer = new JTextField();
        divAnswer = new JTextField();
        con.add(addAnswer);
        con.add(subAnswer);
        con.add(mulAnswer);
        con.add(divAnswer);

        addAnswer.setBounds(410, 50, 50, 50);
        subAnswer.setBounds(410, 120, 50, 50);
        mulAnswer.setBounds(410, 190, 50, 50);
        divAnswer.setBounds(410, 260, 50, 50);
        JButton Check = new JButton("CHECK");
        JButton Next = new JButton("NEXT");
        JButton Exit = new JButton("EXIT");
        con.add(Check);
        con.add(Next);
        con.add(Exit);
        Check.setBounds(235, 330, 75, 30);
        Next.setBounds(320, 330, 65, 30);
        Exit.setBounds(395, 330, 65, 30);



}

1 个答案:

答案 0 :(得分:0)

首先,您的Next动作侦听器抛出NullPointerException,因为未启动4个操作数变量。请在您的Calculatorx构造函数的开头添加以下代码:

 operand1 = new JLabel();
 operand2 = new JLabel();
 operand3 = new JLabel();
 operand4 = new JLabel();

要解决重叠图标的问题,请在actionPerformed方法的Check Action Listener的开头插入以下行:

 Check.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            con.repaint();              
            ....

也可能不需要以下两行:

 con.add(operand4);
 operand4.setBounds(520, 260, 50, 50);

作为一般建议,您应始终使用小写字母作为对象名称。只有类名以大写字母(以及常量)开头