我试图在JFrame上将JPanel上的行设置为标准

时间:2013-11-06 23:47:59

标签: java swing jframe jpanel

我有一些线条(音乐写作的指法),我正在设置它们,以便线条显示为JPanel的标准。我创建了这个类,并尝试在运行时创建一个新实例,但绘制空白。我已经尝试了很多东西,想要创建我自己的方法,但我可能最终使用paint(),如果这更容易?你觉得怎么样?

public class Lines extends JPanel {

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.black);

    g.drawLine(20, 50, 1200, 50);
    g.drawLine(20, 60, 1200, 60);
    g.drawLine(20, 70, 1200, 70);
    g.drawLine(20, 80, 1200, 80);
    g.drawLine(20, 90, 1200, 90);

    g.drawLine(20, 140, 1200, 140);
    g.drawLine(20, 150, 1200, 150);
    g.drawLine(20, 160, 1200, 160);
    g.drawLine(20, 170, 1200, 170);
    g.drawLine(20, 180, 1200, 180);

    g.drawLine(20, 230, 1200, 230);
    g.drawLine(20, 240, 1200, 240);
    g.drawLine(20, 250, 1200, 250);
    g.drawLine(20, 260, 1200, 260);
    g.drawLine(20, 270, 1200, 270);
}
}

我想在这里创建一个新实例;

class panelControls extends JPanel {

    private GUIPanel panel;
    private Lines lines;


    public panelControls(GUIPanel panel) {


        this.panel = panel;
        this.panel.add(lines);
    }   

然后这就是新面板;

public class GUIPanel extends JPanel {

    BufferedImage image;

    public GUIPanel(int width, int height) {

        this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = this.image.getGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        this.setFocusable(true);

    }

    @Override
    public Dimension getSize() {

        return (new Dimension(this.image.getWidth(), this.image.getHeight()));
    }

    @Override
    public void paintComponent(Graphics graphics) {
        Graphics g = graphics.create();
        g.drawImage(this.image, 0, 0, null);
    }
}

好的,这是tabGUI;

    private GUIPanel panel;
    private panelControls controls;

    public tabGUI(String title, int width, int height) {

        super(String.format("Title", title));

        this.panel = new GUIPanel(width, height);
        this.controls = new panelControls(this.panel);
        this.getContentPane().add(this.controls, BorderLayout.CENTER);
    }

    public GUIPanel getGUIPanel() {
        return (this.panel);
    }
}

这是主要的;

public class mainGUI implements Runnable {

    private tabGUI tabGui;
    private String title;
    public mainGUI(String title) {

        this.title = title;
        this.tabGui = new tabGUI(this.title, 1110, 700);

    }

    public void setTitle(String title){
        this.tabGui.setTitle(this.title);
    }

    public void run() {

        this.tabGui = new tabGUI(this.title, 1110, 700);
        this.tabGui.setTitle(this.title);
        this.tabGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.tabGui.setPreferredSize(new Dimension(1240, 800));
        this.tabGui.pack();
        this.tabGui.setVisible(true);
    }

    public static void main(String[] args) throws Exception {

//        JFrame.setDefaultLookAndFeelDecorated(true);
        String title = "piece1";
        //put the tab source file to run here
        mainGUI tabGui = new mainGUI(title);
        javax.swing.SwingUtilities.invokeLater(tabGui);


    }
}

得到这个;

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1090)
    at java.awt.Container.add(Container.java:410)
    at readtabcode.panelControls.<init>(panelControls.java:15)
    at readtabcode.tabGUI.<init>(tabGUI.java:17)
    at readtabcode.mainGUI.<init>(mainGUI.java:14)
    at readtabcode.mainGUI.main(mainGUI.java:37)
Java Result: 1

0 个答案:

没有答案