Button不会创建JPanel

时间:2014-02-13 21:38:30

标签: java swing

我是Java新手,正在为我的大学做一些窗口应用程序。 我试图在开始时用三个按钮做某种菜单,当点击其中一个按钮时,它应该创建一个带有两个按钮的JPanel,但我的代码不起作用。

以下是代码:

import java.awt.*;

public class mainScreen extends JFrame {

    private JPanel contentPane;

    public mainScreen() {
        super("Aplikacija za atletska natjecanja");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0, 0, screenSize.width, screenSize.height);
        contentPane = new JPanel();
        contentPane.setBackground(SystemColor.info);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        setContentPane(contentPane);

        JPanel top = new JPanel();
        top.setBounds(200, 11, screenSize.width - 400, screenSize.height - (screenSize.height - 100));
        contentPane.add(top);

        JPanel mainMenu = new JPanel();
        mainMenu.setBounds(200, 110, screenSize.width - 400, screenSize.height - (screenSize.height - 30));
        contentPane.add(mainMenu);
        mainMenu.setLayout(new GridLayout(1, 0, 0, 0));

        JButton btnTrkacke = new JButton("Trka\u010Dke");
        btnTrkacke.setBackground(SystemColor.text);
        mainMenu.add(btnTrkacke);
        btnTrkacke.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                JPanel panTrk = new JPanel();
                panTrk.setBounds(201, 140, (screenSize.width - 400) / 3, 30);
                contentPane.add(panTrk);
                panTrk.setLayout(new GridLayout(1, 0, 0, 0));

                    JButton btnTrkAtl = new JButton("Atleti\u010Dari");
                    btnTrkAtl.setBackground(SystemColor.text);
                    panTrk.add(btnTrkAtl);

                    JButton btnTrkDisc = new JButton("Discipline");
                    btnTrkDisc.setBackground(SystemColor.text);
                    panTrk.add(btnTrkDisc);
            }
        });

        JButton btnSkakacke = new JButton("Skaka\u010Dke");
        btnSkakacke.setBackground(SystemColor.text);
        mainMenu.add(btnSkakacke);

        JButton btnBacacke = new JButton("Baca\u010Dke");
        btnBacacke.setBackground(SystemColor.text);
        mainMenu.add(btnBacacke);
    }
}

当我点击其他两个按钮时,也应该创建相同的面板,但是在其他位置上......是否更好的为该窗格创建类,然后在单击按钮时调用它?

2 个答案:

答案 0 :(得分:2)

在更改其组件后,您忘记在contentPane上调用revalidate并重新绘制:

contentPane.revalidate();
contentPane.repaint();

如,

     @Override
     public void actionPerformed(ActionEvent e) {

        JPanel panTrk = new JPanel();
        panTrk.setBounds(201, 140, (screenSize.width - 400) / 3, 30);
        contentPane.add(panTrk);
        panTrk.setLayout(new GridLayout(1, 0, 0, 0));

        JButton btnTrkAtl = new JButton("Atleti\u010Dari");
        btnTrkAtl.setBackground(SystemColor.text);
        panTrk.add(btnTrkAtl);

        JButton btnTrkDisc = new JButton("Discipline");
        btnTrkDisc.setBackground(SystemColor.text);
        panTrk.add(btnTrkDisc);

        contentPane.revalidate();  // ***** added *****
        contentPane.repaint();     // ***** added *****
     }
  • revalidate告诉容器重新布局其组件。
  • 重新向绘制管理器重新请求应该重新绘制组件和任何子项。

顺便说一下:你使用null布局管理器和setBounds(...)的绝对定位,你真的不想这样做。对于新手而言,这似乎是创建复杂GUI的最佳方式,您处理Swing GUI创建的越多,您会发现越多这样做会将您的GUI设置为直接夹克,将其绘制在一个非常紧凑的角落并制作它很难扩展或增强。只是不要这样做。

答案 1 :(得分:0)

将问这里,因为它同样的项目

所以它就像这样工作.... 3个按钮,每个按钮下都是带有两个按钮的hiden面板,所以在他下面点击了一些按钮shuld show panel,在他下面点击了一些ather按钮shuld show panel ,但是另外两个面板shuld隐藏....这很好用,直到连续点击一次按钮,之后我点击那个按钮下面的一些按钮面板不要隐藏..

这是代码

JPanel mainMenu = new JPanel();
    mainMenu.setBounds(200, 110, screenSize.width - 400, screenSize.height - (screenSize.height - 30));
    contentPane.add(mainMenu);
    mainMenu.setLayout(new GridLayout(1, 0, 0, 0));

        JButton btnTrkacke = new JButton("Trka\u010Dke");
        btnTrkacke.setBackground(SystemColor.text);
        mainMenu.add(btnTrkacke);
        btnTrkacke.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {


                panTrk = new JPanel();
                panTrk.setBounds(201, 140, (screenSize.width - 400) / 3, 30);
                contentPane.add(panTrk);
                panTrk.setLayout(new GridLayout(1, 0, 0, 0));   

                    JButton btnTrkAtl = new JButton("Atleti\u010Dari");
                    btnTrkAtl.setBackground(SystemColor.text);
                    panTrk.add(btnTrkAtl);

                    JButton btnTrkDisc = new JButton("Discipline");
                    btnTrkDisc.setBackground(SystemColor.text);
                    panTrk.add(btnTrkDisc);


                    contentPane.revalidate();
                    contentPane.repaint();
                    panSka.setVisible(false);   
                    panBac.setVisible(false);

            }
        });

        JButton btnSkakacke = new JButton("Skaka\u010Dke");
        btnSkakacke.setBackground(SystemColor.text);
        mainMenu.add(btnSkakacke);
        btnSkakacke.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {


                panSka = new JPanel();
                panSka.setBounds(201 + (screenSize.width - 400) / 3, 140, (screenSize.width - 400) / 3, 30);
                contentPane.add(panSka);
                panSka.setLayout(new GridLayout(1, 0, 0, 0));

                    JButton btnSkaAtl = new JButton("Atleti\u010Dari");
                    btnSkaAtl.setBackground(SystemColor.text);
                    panSka.add(btnSkaAtl);

                    JButton btnSkaDisc = new JButton("Discipline");
                    btnSkaDisc.setBackground(SystemColor.text);
                    panSka.add(btnSkaDisc);


                    contentPane.revalidate();
                    contentPane.repaint();
                    panTrk.setVisible(false);   
                    panBac.setVisible(false);
            }
        });

        JButton btnBacacke = new JButton("Baca\u010Dke");
        btnBacacke.setBackground(SystemColor.text);
        mainMenu.add(btnBacacke);
        btnBacacke.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {


                panBac = new JPanel();
                panBac.setBounds(201 + (screenSize.width - 400) / 3 * 2, 140, (screenSize.width - 400) / 3, 30);
                contentPane.add(panBac);
                panBac.setLayout(new GridLayout(1, 0, 0, 0));

                    JButton btnBacAtl = new JButton("Atleti\u010Dari");
                    btnBacAtl.setBackground(SystemColor.text);
                    panBac.add(btnBacAtl);

                    JButton btnBacDisc = new JButton("Discipline");
                    btnBacDisc.setBackground(SystemColor.text);
                    panBac.add(btnBacDisc);



                    contentPane.revalidate();
                    contentPane.repaint();
                    panSka.setVisible(false);   
                    panTrk.setVisible(false);


            }
        });