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

时间:2009-11-11 12:11:26

标签: java swing jscrollpane jeditorpane

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

有人帮我吗?

大家好,

以下是代码:

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

public class GridBagWithJEditorPane扩展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;

}

4 个答案:

答案 0 :(得分:0)

小工作示例。向我们展示您的代码,否则我们将无法分辨您的问题所在

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);
      }
    });
  }
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

我建议不要使用GridBagLayout。那里有许多更现代化的布局管理器(MiGLayoutFormLayoutSpringLayout ...),就此而言,你几乎总能通过嵌套{{{{{{ 1}} JPanels这也会使您的布局问题更容易诊断。

说真的,自1995年以来我一直在用Java编程,自1998年开始使用Swing编程,我从来没有费心去学习BorderLayouts.。这不值得努力。

答案 3 :(得分:0)

自己解决了这个问题。 解决方案与

相同

resize problem with JList?

相关问题