Java GUI - 菜单栏和拆分窗格

时间:2016-02-14 11:21:41

标签: java split jframe jpanel pane

我知道如何编写Java代码,但我遇到了很多麻烦。我做了一个菜单栏,但我想在它下面放一个分割窗格。菜单栏很好,但拆分窗格给了我很多错误,我不知道如何解决它。

非常感谢任何帮助。

    package getcodinggui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;


public class GetCodingGUI {
    JTextArea output;
    JScrollPane scrollPane;

    public JMenuBar createMenuBar() {
        JMenuBar menuBar;
        JMenu menu;

        menuBar = new JMenuBar();

        menu = new JMenu("Home");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
                "File Menu Items");
        menuBar.add(menu);

        menu = new JMenu("About");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("Contact Us");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("FAQ");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu Items");
        menuBar.add(menu);

        menu = new JMenu("Log In");
        menu.setMnemonic(KeyEvent.VK_N);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu Items");
        menuBar.add(menu);

        return menuBar;
    }


    public Container createContentPane() {
        //Create the content-pane-to-be.
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.setOpaque(true);


        //Create a scrolled text area.
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);

        //Add the text area to the content pane.
        contentPane.add(scrollPane, BorderLayout.CENTER);

        return contentPane;
    }

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        public static class MyJFrameWin extends JFrame{ 

        JSplitPane jSplitPane, jSplitPane2;
        JPanel jPanel1, jPanel2a, jPanel2b;

        jPanel1 = new JPanel();
            jPanel2a = new JPanel();
            jPanel2b = new JPanel();

            jSplitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
                    jPanel2a, jPanel2b);
            jSplitPane2.setOneTouchExpandable(true);
            jSplitPane2.setDividerLocation(100);

            jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 
                    jPanel1, jSplitPane2);
            jSplitPane.setOneTouchExpandable(true);
            jSplitPane.setDividerLocation(150);

            getContentPane().add(jSplitPane);
        }
}


        //Create and set up the content pane.
        GetCodingGUI demo = new GetCodingGUI();
        frame.setJMenuBar(demo.createMenuBar());
        frame.setContentPane(demo.createContentPane());

        //Display the window.
        frame.setSize(1280, 720);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(runJSplitPaneLater);
}

        javax.swing.SwingUtilities.invokeLater(new Runnable, runJSplitPaneLater() 

{
        static Runnable runJSplitPaneLater = new Runnable(){
            @Override
        public void run() {
            MyJFrameWin myJFrameWin = new MyJFrameWin();
            myJFrameWin.setVisible(true);
                createAndShowGUI();
            }  
        });


    }
}

1 个答案:

答案 0 :(得分:1)

我不得不清理20个编译错误。

这是我创建的GUI。

Split Pane Example

以下是我所做的更改。

  1. 我重新安排了所有代码。从上到下阅读时,代码更容易理解。

  2. 由于SwingUtilities invokeLater方法需要Runnable,因此我使您的GUI视图类实现了Runnable。

  3. 我将您的内容窗格放在其中一个JSplitPanes中。我猜对了哪个窗格。

  4. 我将外部JSplitPane放入JFrame。

  5. 我修改了菜单alt键。

  6. 我从你的createContentPane方法返回了一个JPanel。

  7. 我格式化了您的代码。

  8. 我缩小了JFrame的大小,使其适合我的屏幕。

  9. 以下是代码:

    package com.ggl.testing;
    
    import java.awt.BorderLayout;
    import java.awt.event.KeyEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class GetCodingGUI implements Runnable {
        private JTextArea output;
        private JScrollPane scrollPane;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new GetCodingGUI());
        }
    
        @Override
        public void run() {
            // Create and set up the window.
            JFrame frame = new JFrame("Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JSplitPane jSplitPane, jSplitPane2;
            JPanel jPanel1, jPanel2a, jPanel2b;
    
            jPanel1 = new JPanel();
            jPanel2a = new JPanel();
            jPanel2b = createContentPane();
    
            jSplitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jPanel2a,
                    jPanel2b);
            jSplitPane2.setOneTouchExpandable(true);
            jSplitPane2.setDividerLocation(100);
    
            jSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, jPanel1,
                    jSplitPane2);
            jSplitPane.setOneTouchExpandable(true);
            jSplitPane.setDividerLocation(150);
    
            frame.add(jSplitPane);
            frame.setJMenuBar(createMenuBar());
    
            // Display the window.
            frame.setSize(800, 600);
            frame.setVisible(true);
        }
    
        public JMenuBar createMenuBar() {
            JMenuBar menuBar;
            JMenu menu;
    
            menuBar = new JMenuBar();
    
            menu = new JMenu("Home");
            menu.setMnemonic(KeyEvent.VK_H);
            menu.getAccessibleContext().setAccessibleDescription("File Menu Items");
            menuBar.add(menu);
    
            menu = new JMenu("About");
            menu.setMnemonic(KeyEvent.VK_A);
            menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
            menuBar.add(menu);
    
            menu = new JMenu("Contact Us");
            menu.setMnemonic(KeyEvent.VK_C);
            menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
            menuBar.add(menu);
    
            menu = new JMenu("FAQ");
            menu.setMnemonic(KeyEvent.VK_F);
            menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
            menuBar.add(menu);
    
            menu = new JMenu("Log In");
            menu.setMnemonic(KeyEvent.VK_L);
            menu.getAccessibleContext().setAccessibleDescription("Edit Menu Items");
            menuBar.add(menu);
    
            return menuBar;
        }
    
        public JPanel createContentPane() {
            // Create the content-pane-to-be.
            JPanel contentPane = new JPanel(new BorderLayout());
            contentPane.setOpaque(true);
    
            // Create a scrolled text area.
            output = new JTextArea(5, 30);
            output.setEditable(false);
            scrollPane = new JScrollPane(output);
    
            // Add the text area to the content pane.
            contentPane.add(scrollPane, BorderLayout.CENTER);
    
            return contentPane;
        }
    
    }