如何在 JFrame 内的 JScrollPane 底部显示按钮
我正在开发一个应用程序,试图在 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 中的按钮和 html 页面完全分开显示,页面也没有覆盖整个屏幕。
谢谢,
I am working on an application in which I am trying to show a banner in a JFrame. Inside the JFrame, I using a JScrollbar which is using JEditorPane to display content of a HTML page.
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;
}
Currently the view is looking like this:
As of Now button is displayed on the Top. What I want to do is to put the button in the bottom of the screen. I have tried some layout (BoxLayout and BorderLayout) but view was as per the expection. I can understand that it will be a small change but I am not having much exp in Swing Programming.
Can someone please suggest how can I achieve that.
Edit:
Tried suggested changes:
JScrollPane jScrollPane = new JScrollPane(jEditorPane);
panel.add(jScrollPane);
frmOpt.add(panel,BorderLayout.CENTER);
frmOpt.add(createButton(),BorderLayout.PAGE_END);
but it's not coming as expected. Button and html page in JscrollPane are displaying completely separated and Page is also not coming over complete screen.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JFrame
[内容窗格] 的默认布局管理器是BorderLayout
。中心组件占据了所有剩余空间。因此,BorderLayout 会忽略中心组件的首选大小。另一方面,FlowLayout
尊重其所包含组件的首选大小。由于您已经最大化了JFrame
,它的中心组件将几乎占据整个屏幕,因此您只需将JScrollPane
添加到BorderLayout
中心即可。The default layout manager for [the content pane of]
JFrame
isBorderLayout
. The center component takes up all the remaining space. HenceBorderLayout
ignores the preferred size of the center component.FlowLayout
, on the other hand, respects the preferred size of its contained components. Since you have maximized theJFrame
its center component will take up almost the entire screen so you just need to add theJScrollPane
to theBorderLayout
center.