gpt4 book ai didi

Java Swing : Layout Manager like "North" in BorderLayout needed

转载 作者:行者123 更新时间:2023-12-01 12:10:22 26 4
gpt4 key购买 nike

我想要一个像边框布局管理器一样的布局管理器,除了只有一个区域:北。

边框布局管理器的北部区域对我来说工作正常(即占用正确的空间),但是中心占用了所有可用空间(请参见下面的蓝色区域)。理想情况下,我希望中心占据零空间。

这可能吗?

谢谢。

示例 1(共 2 个)(图片)

example

示例 2(共 2)(代码)

import java.awt.*;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;

public class test
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
JFrame j = new JFrame("Test");
WorkBench right = new WorkBench();

JScrollPane sp
= new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

sp.setAutoscrolls(false);
sp.setViewportView(right);

j.getContentPane().add(sp, BorderLayout.CENTER);

right.addPortal(getNewPortalPane());
right.addPortal(getNewPortalPane());

SwingUtilities.invokeLater(new Runnable()
{
public void run() {
sp.getViewport().setViewPosition(new java.awt.Point(0, 0));
}
});


// center the jframe, then make it visible
j.setSize(800, 600);
j.setLocationRelativeTo(null);
j.setVisible(true);
}
});
}


public static JPanel getNewPortalPane()
{
JEditorPane html_box = new JEditorPane();
html_box.setEditorKit(new HTMLEditorKit());
html_box.setOpaque(true);
html_box.setBackground(Color.WHITE);

html_box.setContentType("text/html");

String htmlString = "<html>\n"
+ "<body>\n"
+ "<h1>Welcomfve! sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n"
+ "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n"
+ "<p>This is some sample text sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n"
+ "<p><a href=\"http://dsfsdfdf.com/blog/\">sdfsdfsdf blog</a></p>\n"
+ "</body>\n";

html_box.setText(htmlString);


JPanel p = new JPanel(new BorderLayout());
p.setBackground(Color.blue);
p.add(html_box, BorderLayout.NORTH);

return p;
}

public static class WorkBench extends JPanel implements Scrollable
{
private Box vertical_box = null;

public WorkBench()
{
setLayout(new BorderLayout());

this.vertical_box = Box.createVerticalBox();
add(this.vertical_box , BorderLayout.CENTER);
}

public void addPortal(JPanel portal)
{
this.vertical_box.add(portal);

validate();
}

@Override
public Dimension getPreferredScrollableViewportSize()
{
return getPreferredSize();
}


@Override
public int getScrollableUnitIncrement(
Rectangle visibleRect,
int orientation,
int direction)
{
return 20;
}


@Override
public int getScrollableBlockIncrement(
Rectangle visibleRect,
int orientation,
int direction)
{
return 60;
}


@Override
public boolean getScrollableTracksViewportWidth()
{
return true;
}


@Override
public boolean getScrollableTracksViewportHeight()
{
if (getParent() instanceof JViewport)
{
return (((JViewport)getParent()).getHeight() > getPreferredSize().height);
}

return false;
}
}

}

最佳答案

可能有一个更优雅的解决方案,但我发现有效的是使用 BorderLayout.NORTH 添加内部面板,将内部面板的布局设置为 BoxLayout > 使用PAGE_AXIS,然后将组件添加到内部面板。因此,在您的示例中,它将是(编辑以修复滚动条):

public class test {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
JFrame j = new JFrame("Test");
WorkBench right = new WorkBench();

final JScrollPane sp = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

sp.setAutoscrolls(false);
sp.setViewportView(right);

j.getContentPane().add(sp, BorderLayout.CENTER);

right.addPortal(getNewPortalPane());
right.addPortal(getNewPortalPane());

SwingUtilities.invokeLater(new Runnable() {

public void run() {
sp.getViewport().setViewPosition(new java.awt.Point(0, 0));
}
});

// center the jframe, then make it visible
j.setSize(800, 600);
j.setLocationRelativeTo(null);
j.setVisible(true);
}
});
}

public static JPanel getNewPortalPane() {
JEditorPane html_box = new JEditorPane();
html_box.setEditorKit(new HTMLEditorKit());
html_box.setOpaque(true);
html_box.setBackground(Color.WHITE);

html_box.setContentType("text/html");

String htmlString = "<html>\n"
+ "<body>\n"
+ "<h1>Welcomfve! sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h1>\n"
+ "<h2>This is an H2 header. sdsdsdsdsd sdsdsd sdsd sdsd sdsd </h2>\n"
+ "<p>This is some sample text sdsdsdsdsd sdsdsd sdsd sdsd sdsd </p>\n"
+ "<p><a href=\"http://dsfsdfdf.com/blog/\">sdfsdfsdf blog</a></p>\n"
+ "</body>\n";

html_box.setText(htmlString);

JPanel p = new JPanel(new BorderLayout());
p.setBackground(Color.blue);
p.add(html_box, BorderLayout.NORTH);

return p;
}

public static class WorkBench extends JPanel implements Scrollable {

private final JPanel inner;

public WorkBench() {
setLayout(new BorderLayout());
inner = new JPanel();
inner.setLayout(new BoxLayout(inner, BoxLayout.PAGE_AXIS));
add(inner, BorderLayout.NORTH);
}

public void addPortal(JPanel portal) {
inner.add(portal);
validate();
}

@Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}

@Override
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 20;
}

@Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 60;
}

@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}

@Override
public boolean getScrollableTracksViewportHeight() {
if (getParent() instanceof JViewport) {
return (((JViewport) getParent()).getHeight() > getPreferredSize().height);
}

return false;
}
}
}

关于Java Swing : Layout Manager like "North" in BorderLayout needed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27313545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com