1
public class FaceMain extends JFrame {
CreateFace p1 = new CreateFace();
private ControlPanel panel;

public FaceMain(ControlPanel value) {
panel = value;
    JFrame main = new JFrame();
    main.setTitle("Face Frame");
    main.setSize(new Dimension(600, 600));
    main.setLocationRelativeTo(null);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setVisible(true);

    Container c = main.getContentPane();
    main.add(p1);
    panel.eyesSetE(true);
    JFrame control = new JFrame();
    control.setTitle("Control Panel");
    control.setSize(new Dimension(300, 300));
    control.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    control.setLocationRelativeTo(main);
    control.setVisible(true);

    ControlPanel p2 = new ControlPanel(p1);
    control.add(p2);

}
}

public class ControlPanel extends JPanel {
boolean eyesSetEdit = false, faceSetEdit = false, mouthSetEdit = false,
        editEyes;
private Color purple = new Color(133, 22, 145);
private CreateFace face;
private CreateFace p1;

public ControlPanel(CreateFace value) {
    face = value;
    p1 = value;
    setLayout(new GridLayout(4, 0));
    JButton change = new JButton("Click");
    add(change);
    JLabel info = new JLabel("Click Above To Change Features",
            JLabel.CENTER);
    add(info);
    JLabel info1 = new JLabel("Slide Below To Change Size", JLabel.CENTER);
    add(info1);
    JScrollBar slider = new JScrollBar(Scrollbar.HORIZONTAL, 0, 100, 0, 300);
    add(slider);
public void eyesSetE(boolean x) {
    eyesSetEdit = x;
}

public boolean getEyesSet() {
    return eyesSetEdit;
}

我已经扩展了我的类以尝试更改一个布尔值,该值将在 ControlPanel 类中广泛使用以做出决定,但是每次我启动程序时,我都会在“panel.eyesSetE(true);”行得到一个 nullpointerexception 即使我尝试调用 getEyesSet() 我仍然收到一个空指针

4

1 回答 1

3

circle你永远不会改变类中的实例CreateCircle,所以它永远不会改变大小。

不要使用static应该是实例变量的东西,而是使用您创建的类的实例并提供一个 setter 方法来更改变量......

基本上,此示例将 的实例传递p1ControlPanel窗格,以便它具有一些上下文来操作您之前创建的内容。

import datetest.CircleShort.ControlPanel;
import datetest.CircleShort.CreateCircle;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Scrollbar;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;

public class CircleShort extends JFrame {

    CreateCircle p1 = new CreateCircle();

    public CircleShort() {
        CreateCircle p1 = new CreateCircle();
        JFrame main = new JFrame();
        main.setSize(new Dimension(600, 600));
        main.setLocationRelativeTo(null);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setVisible(true);

        Container c = main.getContentPane();
        main.add(p1);
        JFrame control = new JFrame();
        control.setTitle("Control Panel");
        control.setSize(new Dimension(300, 300));
        control.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        control.setLocationRelativeTo(main);
        control.setVisible(true);

        ControlPanel p2 = new ControlPanel(p1);
        control.add(p2);

    }

    static class CreateCircle extends JComponent {

        int circleX = 100;
        Ellipse2D.Double circle;

        public CreateCircle() {
            circle = new Ellipse2D.Double(circleX, 50, 400, 350);
        }

        public void setCircleX(int x) {
            circleX = x;
            circle = new Ellipse2D.Double(circleX, 50, 400, 350);
            repaint();
        }

        public int getCircleX() {
            return circleX;
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(Color.red);
            g2.fill(circle);
        }
    }

    class ControlPanel extends JComponent {

        private CreateCircle circle;

        public ControlPanel(CreateCircle value) {
            circle = value;
            setLayout(new GridLayout(4, 0));
            JButton change = new JButton("Click");
            add(change);
            JLabel info = new JLabel("Click Above To Change Color",
                    JLabel.CENTER);
            add(info);
            JLabel info1 = new JLabel("Slide Below To Change Size",
                    JLabel.CENTER);
            add(info1);
            JScrollBar slider = new JScrollBar(Scrollbar.HORIZONTAL, 0, 100, 0,
                    300);
            add(slider);
            slider.addAdjustmentListener(new AdjustmentListener() {

                public void adjustmentValueChanged(AdjustmentEvent e) {
                    System.out.println(e.getValue());
                    circle.setCircleX((circle.getCircleX() + (e.getValue() % 5)));
                }

            });

        }
    }

    static class CircleRun {

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

static在您真正了解如何使用它之前,它是邪恶的。如果您发现自己static只是因为您似乎无法弄清楚如何访问它而创建了某个字段或方法,那么您可能做错了什么,小心它......

这实际上提出的下一个问题是,“为什么?”

AJSlider将是更适合使用的控件,并且在概念上更易于用户理解

于 2013-11-12T05:29:06.390 回答