调整框架大小时 JEditor 窗格出现问题

发布于 2024-08-11 00:33:52 字数 3115 浏览 5 评论 0原文

我是 java GUI 编程新手。我在 JScrollPane 中添加 JEditor 窗格(带有 HTML 文本),并将滚动窗格添加到 JFrame。问题是当我调整框架大小时,JEditor 窗格消失。

有人帮帮我吗?

大家好,

以下是代码:

这里的另一个问题是我为窗格设置了weightx和weighty。但是当框架最大化/最小化时,JEditor窗格仍然没有调整大小

public class GridBagWithJEditorPane extends javax.swing.JFrame {

public GridBagWithJEditorPane() {
    initComponents();
}

private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    Panel1 = new javax.swing.JPanel();
    Button1 = new javax.swing.JButton();
    Label = new javax.swing.JLabel();
    Panel2 = new javax.swing.JPanel();
    ScrollPane = new javax.swing.JScrollPane();
    EditorPane1 = new javax.swing.JEditorPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new java.awt.GridBagLayout());

    Panel1.setLayout(new java.awt.GridBagLayout());

    Button1.setText("Button");
    Panel1.add(Button1, new java.awt.GridBagConstraints());

    Label.setText("Label");
    Panel1.add(Label, new java.awt.GridBagConstraints());

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    getContentPane().add(Panel1, gridBagConstraints);

    Panel2.setLayout(new java.awt.GridBagLayout());

    ScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    EditorPane1.setContentType("text/html");
    EditorPane1.setText("<html>\r\n  <head>\r\n\r\n  </head>\r\n  <body>\r\n    <p style=\"margin-top: 0\">\r\n      \rHere some html text sakdjslakdjsa dksa dsakjdklsajdklsad klsajd lksad<br>\n      asdka;slkd;laskd;sa dlksa dksa dksald;lsakd;lsakd;l ska;dl sal;dk;salkd<br>\n     asas;alks;laKSL;Kalk ALSKLAks;laSAsLAKS;Lk;slk<br>\t\n    alsdasldk;alskd;laskd;l sadksa;dlksa;ldk;saldk;alsd<br>\n    </p>\r\n  </body>\r\n</html>\r\n");
    EditorPane1.setMinimumSize(new java.awt.Dimension(15, 15));
    EditorPane1.setPreferredSize(new java.awt.Dimension(340, 220));
    ScrollPane.setViewportView(EditorPane1);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    Panel2.add(ScrollPane, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    getContentPane().add(Panel2, gridBagConstraints);

    pack();
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GridBagWithJEditorPane().setVisible(true);
        }
    });
}


private javax.swing.JButton Button1;
private javax.swing.JEditorPane EditorPane1;
private javax.swing.JLabel Label;
private javax.swing.JPanel Panel1;
private javax.swing.JPanel Panel2;
private javax.swing.JScrollPane ScrollPane;

}

i am new to java GUI programming. i am adding a JEditor pane (with HTML text ) inside a JScrollPane and added the scroll pane to a JFrame.The problem is when i re size the frame the JEditor pane vanishes.

Any one help me ?

Hi All ,

Following is the code :

here another problem is i set weightx and weighty for the pane .But still the JEditor pane is not resizing when the frame is maximized/mimized

public class GridBagWithJEditorPane extends javax.swing.JFrame {

public GridBagWithJEditorPane() {
    initComponents();
}

private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;

    Panel1 = new javax.swing.JPanel();
    Button1 = new javax.swing.JButton();
    Label = new javax.swing.JLabel();
    Panel2 = new javax.swing.JPanel();
    ScrollPane = new javax.swing.JScrollPane();
    EditorPane1 = new javax.swing.JEditorPane();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setLayout(new java.awt.GridBagLayout());

    Panel1.setLayout(new java.awt.GridBagLayout());

    Button1.setText("Button");
    Panel1.add(Button1, new java.awt.GridBagConstraints());

    Label.setText("Label");
    Panel1.add(Label, new java.awt.GridBagConstraints());

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    getContentPane().add(Panel1, gridBagConstraints);

    Panel2.setLayout(new java.awt.GridBagLayout());

    ScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

    EditorPane1.setContentType("text/html");
    EditorPane1.setText("<html>\r\n  <head>\r\n\r\n  </head>\r\n  <body>\r\n    <p style=\"margin-top: 0\">\r\n      \rHere some html text sakdjslakdjsa dksa dsakjdklsajdklsad klsajd lksad<br>\n      asdka;slkd;laskd;sa dlksa dksa dksald;lsakd;lsakd;l ska;dl sal;dk;salkd<br>\n     asas;alks;laKSL;Kalk ALSKLAks;laSAsLAKS;Lk;slk<br>\t\n    alsdasldk;alskd;laskd;l sadksa;dlksa;ldk;saldk;alsd<br>\n    </p>\r\n  </body>\r\n</html>\r\n");
    EditorPane1.setMinimumSize(new java.awt.Dimension(15, 15));
    EditorPane1.setPreferredSize(new java.awt.Dimension(340, 220));
    ScrollPane.setViewportView(EditorPane1);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    Panel2.add(ScrollPane, gridBagConstraints);

    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    getContentPane().add(Panel2, gridBagConstraints);

    pack();
}

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new GridBagWithJEditorPane().setVisible(true);
        }
    });
}


private javax.swing.JButton Button1;
private javax.swing.JEditorPane EditorPane1;
private javax.swing.JLabel Label;
private javax.swing.JPanel Panel1;
private javax.swing.JPanel Panel2;
private javax.swing.JScrollPane ScrollPane;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-08-18 00:33:52

小工作示例。向我们展示您的代码,否则我们将无法判断您的问题出在哪里

import javax.swing.*;
public class Test {
  public static void main (String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new JFrame("JScrollPane+JEditorPane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(new JEditorPane()));
        frame.pack();
        frame.setVisible(true);
      }
    });
  }
}

Small working example. Show us your code else we won't be able to tell where your problem lies

import javax.swing.*;
public class Test {
  public static void main (String args[]) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame frame = new JFrame("JScrollPane+JEditorPane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(new JEditorPane()));
        frame.pack();
        frame.setVisible(true);
      }
    });
  }
}
メ斷腸人バ 2024-08-18 00:33:52

问题是您为滚动窗格设置了weightX/Y 值,而不是为面板设置了weightX/Y 值。

为什么要将滚动窗格添加到面板,然后将面板添加到框架?面板是不必要的。

The problem is you set weightX/Y values for the scroll pane, but not the panel.

Why are you adding the scroll pane to a panel and then adding the panel to the frame? The panel is unnecessary.

绝情姑娘 2024-08-18 00:33:52

我建议不要使用 GridBagLayout。有很多更现代的布局管理器(MiGLayoutFormLayoutSpringLayout...),就这一点而言,您几乎总是可以只需将 JPanelsBorderLayouts 嵌套即可获得您想要的内容。 这也将使您的布局问题更容易诊断。

说实话,我从 1995 年开始就开始使用 Java 编程,从 1998 年开始就使用 Swing,但我从来没有费心去正确地学习 GridBagLayout。这根本不值得付出努力。

I suggest not using GridBagLayout. There are plenty of more modern layout managers out there (MiGLayout, FormLayout, SpringLayout...), and for that matter you can almost always get what you want just by nesting JPanels with BorderLayouts. This will also make your layout problems much easier to diagnose.

Seriously, I've been programming in Java since 1995 and with Swing since 1998, and I've never bothered to learn GridBagLayout properly. It's just not worth the effort.

瀟灑尐姊 2024-08-18 00:33:52

我自己解决了这个问题。
中的解决方案相同

解决方案与 使用 JList 调整大小问题?

solved the problem myself .
The solution is same as in

resize problem with JList?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文