我正在開發一個嘗試在 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;
}
當前視圖如下所示:

As of Now 按鈕顯示在頂部。我想要做的是將按鈕放在螢屏底部。我嘗試了一些布局(BoxLayout 和 BorderLayout),但視圖符合預期。我可以理解這將是一個很小的變化,但我在 Swing 編程方面沒有太多經驗。
有人可以建議我如何實作這一目標。
編輯:
嘗試了建議的更改:
JScrollPane jScrollPane = new JScrollPane(jEditorPane);
panel.add(jScrollPane);
frmOpt.add(panel,BorderLayout.CENTER);
frmOpt.add(createButton(),BorderLayout.PAGE_END);
但它并沒有像預期的那樣來。JscrollPane 中的按鈕和 html 頁面完全分開顯示,并且頁面也沒有超過整個螢屏。

謝謝,
uj5u.com熱心網友回復:
[內容窗格] 的默認布局管理器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());
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/441220.html
上一篇:如何在JPanel上繪制組件?
