我不确定在这样声明时如何引用JPanel。

这是整个程序的编码:一切正常,但是布局不是我想要的。向其添加BorderLayouts似乎不起作用。

class FrameDemo
{
    public static void main(String[] args)
    {
        final JFrame frame = new JFrame("CIT Test Program");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(350, 250));
        frame.add(new JPanel()
        {{
            String[] tests = {"A+ Certification", "Network+ Certification", "Security+ Certification", "CIT Full Test Package"};
            JComboBox comboBox = new JComboBox(tests);
            TextArea text = new TextArea(5,10);
            add(new JLabel("Welcome to the CIT Test Program "));
            add(new JLabel("Please select which Test Package from the list below."));

            JMenuBar menuBar = new JMenuBar();
            JMenu fileMenu = new JMenu("File");
            JMenu editMenu = new JMenu("Edit");
            JMenu helpMenu = new JMenu("Help");
            menuBar.add(fileMenu);
            menuBar.add(editMenu);
            menuBar.add(helpMenu);
            JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
            JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
            JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
            JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
            JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
            JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
            JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
            JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
            fileMenu.add(newMenu);
            fileMenu.add(openMenu);
            fileMenu.add(saveMenu);
            fileMenu.add(exitMenu);
            editMenu.add(cutMenu);
            editMenu.add(copyMenu);
            editMenu.add(pasteMenu);
            helpMenu.add(infoMenu);
            this.add(comboBox, BorderLayout.NORTH);
            this.add(text, BorderLayout.SOUTH);
            frame.setJMenuBar(menuBar);
            add(new JButton("Select")
            {{
                addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e )
                    {
                        frame.dispose();
                        JOptionPane.showMessageDialog(frame,"IT WORKS!");

                    }
                });
            }});
        }});
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }
}


不确定如何引用JPanel以使用BorderLayout。
我将如何去做呢?

最佳答案

如果将面板添加到JFrame(如此处使用add()所示),则可以假定它是作为contentPanel添加的。但是,最好明确一些。代替这个:

frame.add(new JPanel()
{}


用这个:

JPanel panel = new JPanel(new BorderLayout());
// add your stuff to the panel;
frame.add(panel);


编辑:

查看您的编辑后,很明显您正在初始化一个匿名类。通常这不是一个坏习惯,但是您在这里放置了很多初始化代码。您要放入的代码位于双括号块中,从本质上讲,它会将其放入静态初始化程序中。看起来像一个普通的匿名类,但实际上不是。有了那么多的代码,它就应该拥有自己的类。如果您愿意编写新类,那么应该没问题。我建议您然后将其定义为JPanel的扩展,并在自己的构造函数中将new BorderLayout()传递给super

编辑2:

如果您创建一个名为Bar的全新文件/类,并按如下方式进行编码:

public class Bar extends JPanel {
    public Bar(final JFrame frame) {
        String[] tests = { "A+ Certification", "Network+ Certification",
                "Security+ Certification", "CIT Full Test Package" };
        JComboBox comboBox = new JComboBox(tests);
        TextArea text = new TextArea(5, 10);
        add(new JLabel("Welcome to the CIT Test Program "));
        add(new JLabel("Please select which Test Package from the list below."));

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        JMenu helpMenu = new JMenu("Help");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        JMenuItem newMenu = new JMenuItem("New  (Ctrl+N)");
        JMenuItem openMenu = new JMenuItem("Open  (Ctrl+O)");
        JMenuItem saveMenu = new JMenuItem("Save  (Ctrl+S)");
        JMenuItem exitMenu = new JMenuItem("Exit  (Ctrl+W)");
        JMenuItem cutMenu = new JMenuItem("Cut  (Ctrl+X)");
        JMenuItem copyMenu = new JMenuItem("Copy  (Ctrl+C)");
        JMenuItem pasteMenu = new JMenuItem("Paste  (Ctrl+V)");
        JMenuItem infoMenu = new JMenuItem("Help  (Ctrl+H)");
        fileMenu.add(newMenu);
        fileMenu.add(openMenu);
        fileMenu.add(saveMenu);
        fileMenu.add(exitMenu);
        editMenu.add(cutMenu);
        editMenu.add(copyMenu);
        editMenu.add(pasteMenu);
        helpMenu.add(infoMenu);
        this.add(comboBox, BorderLayout.NORTH);
        this.add(text, BorderLayout.SOUTH);
        frame.setJMenuBar(menuBar);
        add(new JButton("Select") {
            {
                addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        frame.dispose();
                        JOptionPane.showMessageDialog(frame, "IT WORKS!");

                    }
                });
            }
        });

    }


您需要做的就是打电话

  JPanel panel = new Bar(frame);


但是,这里的目标是使用BorderLayout,所以我建议您将这个调用放入开始:

 public Bar(final JFrame frame) {
        super(new BorderLayout());
        .... everything else
 }

关于java - 如何使用通用JPanel()管理BorderLayout,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3059050/

10-10 06:42