繁体   English   中英

如何在 JFrame 内的 JScrollPane 底部显示按钮

[英]How to show the Button in the bottom of the JScrollPane Inside a JFrame

我正在开发一个应用程序,我试图在其中显示 JFrame 中的横幅。在 JFrame 中,我使用了一个 JScrollbar,它使用 JEditorPane 来显示 HTML 页面的内容。

    public static JFrame ShowBanner() throws IOException {
        int width = 0;
        int height = 0;
        String urlText = null;
        String ScrnResol = getScreenResolution();
        String[] ScreenResolution = ScrnResol.split(",");
        try {
            width = Integer.parseInt(ScreenResolution[0]);
            height = Integer.parseInt(ScreenResolution[1]);
        } catch (NumberFormatException nfe) {
            logger.error("NumberFormatException: " + nfe.getMessage());
        }
        //Creating Frame
        frmOpt = new JFrame("Banner");
        frmOpt.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frmOpt.setPreferredSize(new Dimension(width, height));
        frmOpt.setUndecorated(true);
        frmOpt.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        frmOpt.setVisible(true);
        frmOpt.setAlwaysOnTop(true);
        frmOpt.setLocationRelativeTo(null);
        //bringToForeground(getHWND(frmOpt));
        JPanel panel = new JPanel();
        LayoutManager layout = new FlowLayout();
        panel.setLayout(layout);
        JEditorPane jEditorPane = new JEditorPane();
        jEditorPane.setEditable(false);
        try {
            
            String urlText=IOUtils.toString(BringUpFrame.class.getClassLoader().getResourceAsStream("banner.htm"));
            jEditorPane.setContentType("text/html");
            jEditorPane.setText(urlText);
        } catch (Exception e) {
            logger.error("Exception while executing showBanner: {}", e);
            jEditorPane.setContentType("text/html");
            jEditorPane.setText("<html>Page not found.</html>");
        }
        JScrollPane jScrollPane = new JScrollPane(jEditorPane);
        scrollPane.setColumnHeaderView(createButton());
        jScrollPane.setPreferredSize(new Dimension(width, height));
        panel.add(jScrollPane);
        frmOpt.add(panel);
        frmOpt.pack();
        frmOpt.setVisible(true);
        return frmOpt;

   }

   private static JPanel createButton() {
        JPanel panel = new JPanel();
        panel.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        JButton button = new JButton("Close");
        panel.add(button);
        return panel;
    }

当前视图看起来像这样:

在此处输入图像描述

从现在开始按钮显示在顶部。 我想要做的是将按钮放在屏幕底部。 我尝试了一些布局(BoxLayout 和 BorderLayout),但视图符合预期。 我可以理解这将是一个小的变化,但我在 Swing 编程方面没有太多经验。

有人可以建议我如何实现这一目标。

编辑

尝试建议的更改:

JScrollPane jScrollPane = new JScrollPane(jEditorPane);
panel.add(jScrollPane);
frmOpt.add(panel,BorderLayout.CENTER);
frmOpt.add(createButton(),BorderLayout.PAGE_END);

但它并没有像预期的那样出现。 JscrollPane 中的 Button 和 html 页面完全分离显示,Page 也没有完全显示出来。 在此处输入图像描述

谢谢,

JFrame [的内容窗格] 的默认布局管理器是BorderLayout 中心组件占据了所有剩余空间。 因此BorderLayout忽略中心组件的首选大小。 另一方面, FlowLayout遵循其包含组件的首选大小。 由于您已经最大化了JFrame它的中心组件将几乎占据整个屏幕,因此您只需将JScrollPane添加到BorderLayout中心即可。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ForManish {
    private void createAndDisplayGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.add(createScrollPane(), BorderLayout.CENTER);
        frame.add(createButtonPanel(), BorderLayout.PAGE_END);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
    }

    private JScrollPane createScrollPane() {
        JEditorPane editorPane = new JEditorPane("text/html", "<h1 align=\"center\">Checkout unavailable");
        editorPane.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        JScrollPane scrollPane = new JScrollPane(editorPane);
        scrollPane.setBorder(BorderFactory.createLineBorder(new Color(1.0f, 1.0f, 1.0f, 0.1f)));
        return scrollPane;
    }

    private JPanel createButtonPanel() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        JButton closeButton = new JButton("Close");
        buttonPanel.add(closeButton);
        return buttonPanel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new ForManish().createAndDisplayGui());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM