单击按钮时,将另一个类的JPanel添加到JFrame中的JPanel

时间:2014-10-04 18:57:02

标签: java eclipse swing jpanel

嘿,我是java(和stackoverflow)的新手,首先请原谅我的英语,如果我问的问题已经在另一个帖子中回答,请对不起。

我按照本教程将jpanel从另一个类添加到jframe中的jpanel:

Adding JPanel from another class to JPanel in JFrame (begginer java programmer)

它有效,但当我尝试使用按钮从其他类添加jpanel时,它没有工作。 我使用swing>在eclipse中创建了项目。应用程序窗口而不是jframe。

这是我的代码

主要

import java.awt.EventQueue;


public class main {

    private JFrame frame;
    //call from other class
    private axe_skills_panel axs;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    main window = new main();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public main() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */



    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 705, 427);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));

        JLabel lblNewLabel = new JLabel("SELECT YOUR HERO");
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setFont(new Font("Comic Sans MS", Font.BOLD, 14));
        frame.getContentPane().add(lblNewLabel, BorderLayout.NORTH);

        JPanel skills_panel = new JPanel();
        frame.getContentPane().add(skills_panel, BorderLayout.CENTER);
        //axs = new axe_skills_panel();
        //skills_panel.add(axs);

        JPanel heroselection_panel = new JPanel();
        heroselection_panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
        frame.getContentPane().add(heroselection_panel, BorderLayout.WEST);
        heroselection_panel.setLayout(new BoxLayout(heroselection_panel, BoxLayout.Y_AXIS));

        JScrollPane scrollPane_1 = new JScrollPane();
        heroselection_panel.add(scrollPane_1);

        JPanel panel = new JPanel();
        scrollPane_1.setViewportView(panel);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        //AXE BUTTON
        JButton axe_logo = new JButton("");
        axe_logo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            //v THESE LINES GIVING ME AN ERROR
            skills_panel.add(axs);
            skills_panel.validate();
            skills_panel.repaint();
            //^ THESE LINES GIVING ME AN ERROR
            }
        });

        axe_logo.setIcon(new ImageIcon(main.class.getResource("/images/axe_herologo.png")));
        axe_logo.setBorder(null);
        panel.add(axe_logo);

        JPanel RNG_panel = new JPanel();
        FlowLayout flowLayout = (FlowLayout) RNG_panel.getLayout();
        frame.getContentPane().add(RNG_panel, BorderLayout.EAST);

    }
}

其他班级

import javax.swing.JPanel;


public class axe_skills_panel extends JPanel {

    /**
     * Create the panel.
     */
    public axe_skills_panel() {
        setBorder(null);
        setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        JButton axe_skills_helix_btn = new JButton("");
        axe_skills_helix_btn.setIcon(new ImageIcon(axe_skills_panel.class.getResource("/images/axe_skill_counterhelix.png")));
        axe_skills_helix_btn.setBorder(null);
        add(axe_skills_helix_btn);

    }

}

这是问题的图片:http://i5.minus.com/i02NwV2YdTZOR.jpg

请原谅我的新手。 谢谢:))

修改

Code-Apprentice回答,TY MAN:)

0 个答案:

没有答案