java - 如何在具有另一个组件的拆分 Pane 上添加按钮

标签 java swing user-interface button pane

我想向拆分 Pane 的左侧组件添加一个按钮。我已经在那里放置了一个文件树组件,但我想在其底部添加一个按钮。

这是一张图片,用于阐明我想要按钮的位置 Here

请参阅下面代码的主要部分

public class Viewer_Navigator {
    /*
    *  This is the root of the objects created or fetched.
    */
    // SB_Session sess = null;
    // SB_Context ctx = null;

    /*
    * We are performing an administration service (-looks like a Navigator, but rooted in 
    * the Schema and object store for administration objects).)
    */
    // SB_Service adminSvc = null;

    private JFrame frame;

    /**
    * Launch the application Duh.
    */
    public static void main(String[] args) {

        /*
        * ??? Load startup parameters from the args[] or from a file. (Load the file
        * first and override with any command line args.
        */
        String defStartParams = "AdminStart.xml";

        /*
        * ??? Need to create an XML parser to load attributes for constructors but we're cheating for now
        */
        String adminParams = parseXML(defStartParams);

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Viewer_Navigator window = new Viewer_Navigator();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private static String parseXML(String defStartParams) {
        // We're cheating for now
        String params = "sb_ContName='com.staorbase.service.SB_AdminSvc'";
        params += ";";

        return params;
    }

    /**
    * Create the application.
    */
    public Viewer_Navigator() {

        /*
        * HashMap<String, Object> attrHM = new HashMap<String, Object>();
        * 
        * // let's start with the creation of a test Context
        * 
        * attrHM.clear(); attrHM.put("sb_CtxInfoSvc.sb_SvcName", "STAORbase-Homebase");
        * attrHM.put("sb_CtxIsCreds.sb_CrduAccnt","TestUser1");
        * attrHM.put("sb_CtxIsCreds.sb_CrduPswd","Testing1234");
        * 
        * try { ctx = new SB_Context(attrHM, null); } catch (Exception e) { // //
        * Complain...
        * System.out.println("??? EXCEPTIONS encountered creating the Context!");
        * e.printStackTrace(); }
        * 
        * // If we successfully created the Context, there should be a SB_Session sess
        * sess = SB_Session.SB_Sess_GetSession();
        * 
        */  // Now we can use the Session as the root to our Tree!

        initialize();
    }

    /**
    * Initialize the contents of the frame.
    */
    private void initialize() {
        frame = new JFrame();
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
            }
        });
        frame.setBounds(100, 100, 840, 545);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);  //put the jframe on center of the screen -added

        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmConnect = new JMenuItem("Connect ...");
        mntmConnect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    Viewer_Login sb_Login_dlg = new Viewer_Login();
                    sb_Login_dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    sb_Login_dlg.setVisible(true);
                } catch (Exception elogin) {
                    elogin.printStackTrace();
                }
            }
        });

        mnFile.add(mntmConnect);

        JMenuItem mntmDisconnect = new JMenuItem("Disconnect");
        mntmDisconnect.setEnabled(false);
        mnFile.add(mntmDisconnect);

        JMenu mnEdit = new JMenu("Edit");
        menuBar.add(mnEdit);

        JMenu mnView = new JMenu("View");
        menuBar.add(mnView);

        JMenu mnUpateSolr = new JMenu("Update Apache Solr"); //added
        menuBar.add(mnUpateSolr); //added

        JMenu mnQuerySearch = new JMenu("Query Search"); //added
        menuBar.add(mnQuerySearch); //added

        JToolBar toolBar = new JToolBar();
        frame.getContentPane().add(toolBar, BorderLayout.NORTH);

        JSplitPane splitPane = new JSplitPane();
        frame.getContentPane().add(splitPane, BorderLayout.CENTER);

        JButton ReFresh=new JButton("Test");







        /*JTree tree = new JTree();
        tree.setModel(new DefaultTreeModel(
        new DefaultMutableTreeNode("Session Object") {
            // new DefaultMutableTreeNode(sess.sb_getSessId()) {
            {
                DefaultMutableTreeNode node_1;
                DefaultMutableTreeNode node_2;
                DefaultMutableTreeNode node_3;
                DefaultMutableTreeNode node_4;
                node_1 = new DefaultMutableTreeNode("Context:: Bob/SOFDL");
                node_1.add(new DefaultMutableTreeNode("sb_CtxId"));
                node_1.add(new DefaultMutableTreeNode("sb_CtxInfoSvc"));
                node_2 = new DefaultMutableTreeNode("sb_CtxSchema");
                node_3 = new DefaultMutableTreeNode("SB_Object");
                node_3.add(new DefaultMutableTreeNode("sb_ObjParent"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjRefCnt"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjSISID"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjSOID"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjClass"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjAvail"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjState"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjCtx"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjIcon"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjHelp"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjUserInt"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjUserObj"));
                node_3.add(new DefaultMutableTreeNode("sb_ObjRndrLst"));
                node_4 = new DefaultMutableTreeNode("SB_Secure");
                node_4.add(new DefaultMutableTreeNode("sb_SecOwnerId"));
                node_4.add(new DefaultMutableTreeNode("sb_SecGroupId"));
                node_4.add(new DefaultMutableTreeNode("sb_SecAccCode"));
                node_4.add(new DefaultMutableTreeNode("sb_SecACL"));
                node_4.add(new DefaultMutableTreeNode("SB_Registered"));
                node_3.add(node_4);
                node_2.add(node_3);
                node_1.add(node_2);
                node_1.add(new DefaultMutableTreeNode("sb_CtxIsCreds"));
                node_1.add(new DefaultMutableTreeNode("sb_CtxUserProfile"));
                node_1.add(new DefaultMutableTreeNode("sb_CtxISId"));
                node_1.add(new DefaultMutableTreeNode("sb_CtxServices"));
                node_1.add(new DefaultMutableTreeNode("sb_CtxFlags"));
                add(node_1);
                node_1 = new DefaultMutableTreeNode("Context:: Mary");
                node_1.add(new DefaultMutableTreeNode("SchemaDb"));
                node_1.add(new DefaultMutableTreeNode("Credentials"));
                node_1.add(new DefaultMutableTreeNode("UserProfile"));
                node_1.add(new DefaultMutableTreeNode("Services"));
                add(node_1);
                node_1 = new DefaultMutableTreeNode("Context::Admin");
                node_1.add(new DefaultMutableTreeNode("SchemaDb"));
                node_1.add(new DefaultMutableTreeNode("Credentials"));
                node_1.add(new DefaultMutableTreeNode("UserProfile"));
                node_1.add(new DefaultMutableTreeNode("Services"));
                add(node_1);
            }
        }
        )); */

        // Get the root node of the tree
        // DefaultMutableTreeNode rootSess = (DefaultMutableTreeNode) tree.getModel().getRoot();
        // rootSess.setUserObject(sess); // remember the Session in it

        //Viewer_FileTree fileTree = new Viewer_FileTree(new File("./"));

        NewFileTree test=new NewFileTree("C://");

        splitPane.setLeftComponent(test);






        JEditorPane editorpane= new JEditorPane();
           JScrollPane editorScrollPane = new JScrollPane(editorpane);
           editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
           File file = new File("C:\\Users\\Hamzah\\Downloads\\Solr-OutsideIn-Project\\Solr-OutsideIn-Project\\yababy.html");
           try {
            editorpane.setPage(file.toURI().toURL());
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


           editorpane.setEditable(false);



        splitPane.setRightComponent(editorpane);
    }



}

非常感谢您的帮助

谢谢

最佳答案

  • 在左侧分割 Pane 部分放置一个 JPanel
  • 给它一个BorderLayout
  • JTree 添加到此 JPanelBorderLayout.CENTER 位置
  • JButton 添加到此 JPanelBorderLayout.PAGE_END 位置
  • 如果您不希望按钮填满底部区域,请先将其放入 JPanel 中,然后将该 JPanel 添加到左侧分割 Pane JPanel 的 BorderLayout.PAGE_END 位置
  • 关键是嵌套 JPanel,每个 JPanel 使用自己的布局,从而通过使用简单的布局管理器实现复杂的 GUI 布局。
  • 最重要:阅读 Layout Manager Tutorial 。这些信息都可以在那里找到

完成

关于java - 如何在具有另一个组件的拆分 Pane 上添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60142607/

相关文章:

java - 设置 BorderLayout 并调用 PaintComponent()

java - 在 JTextPane 中禁用超链接

java - 在按键时启动计时器,java

java - Java 中具有抽象方法签名的可见性修饰符

java - 单击 X 隐藏/简单地关闭 Jdialog

Java 图形用户界面组件

c++ - 需要 Qt 架构建议

java - 制作可滚动表格

java - 使用回收器在 android Studio 中获取 NullPointerException

java - 如何在Suse上安装java?