JPanel Paint不会显示在我的JFrame上

时间:2015-08-18 21:41:00

标签: java jframe jbutton paint paintcomponent

我无法弄明白。我尝试过交换我的createWindow()和createButton()方法,并尝试调用repaint()。我也试过分层,以便首先制作JPanel,然后是JFrame,然后将JPanel附加到JFrame。 我确保在将所有元素添加到JFrame后添加setVisible(true),现在JPanel根本不会显示。这是我的Window类和绘画类:

public class Window扩展了JFrame {

private JPanel design;
private Container win;
private JFrame window,popUp;
private JButton button,button2,saveButton,loadButton,quitButton;
private JOptionPane j;
private String name;


Window(){ //Constructor

    initial();

    createWindow();


    createButton();


    actionAddButton();
    combinePanel();

    setVisible(true);

}

private void initial(){

     //window = new JFrame("Family Tree");
     button = new JButton("Add Person");
     button2 = new JButton("Delete Person");
     saveButton = new JButton("Save");
     loadButton = new JButton("Load");
     quitButton = new JButton("Quit");
     j = new JOptionPane();
     name = null;
     win = getContentPane();

}

public JButton getButton2(){
    return button2;
}

private void createWindow(){

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Family Tree");
    setSize(650, 650);
    setLocationRelativeTo(null);
    //setVisible(true);

   // win.setLayout(new GridLayout(1,1));

     //Adds paint to container
}

private void combinePanel(){

    Painting panel = new Painting();
    add(panel);

}
private void createButton(){

   // Container panel = getContentPane(); // content panel area where child components placed
    //panel.add(button,BorderLayout.LINE_START); 
    /*getContentPane returns contentPane object*/
    GroupLayout layout = new GroupLayout(win);
   // GroupLayout layout = new GroupLayout(window.getContentPane());
    layout.setAutoCreateContainerGaps(true); //Dont have to specify gaps
    layout.setAutoCreateGaps(true);
    win.setLayout(layout); //NEED THIS

    layout.setVerticalGroup(  //setHorizontalGroup(Group group)

            layout.createSequentialGroup().addComponent(button)
                .addComponent(button2)
                .addComponent(saveButton) .addComponent(quitButton)
                .addComponent(loadButton)


    );

    layout.setHorizontalGroup(
            /*createParallelGroup() returns new parallel group*/
        layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                .addComponent(button).addComponent(button2)
                .addComponent(saveButton).addComponent(loadButton)
                .addComponent(quitButton))



    );
    repaint();
}

private void actionAddButton(){

    button.addActionListener(new ActionListener() { //Action listener for button
         @Override //
         public void actionPerformed(ActionEvent event){ //Want own action preformed

             name = JOptionPane.showInputDialog(new PopUp(),"Enter Name");

         }   

    });
}

这是我的绘画班:

公共类绘画扩展了JPanel {

Painting(){
    setBackground(Color.GREEN);

}

@Override
public void paintComponent(Graphics g){

    super.paintComponent(g);

    Graphics2D divider = (Graphics2D) g; //casting object

    divider.draw(new Line2D.Double(180, 0, 180, 650));


}

感谢所有帮助。感谢

0 个答案:

没有答案