问题描述
我无法让Swing布局做我想做的事。我希望包含两个JEditorPanes的Center面板在包含相等(固定)高度的'n'窗格时滚动。
我一直在Netbean的UI设计师中玩,试图让它工作
jPanel3是中心面板
jEditorPane4和5是一些示例编辑窗格(这些将保留评论)
Im having trouble getting Swing layouts to do what I want. I want the Center panel which contains two JEditorPanes to scroll when it contains 'n' Panes of equal (fixed) height.
I've been playing around in Netbean's UI designer to try to get it to work
jPanel3 is the center panel
jEditorPane4 and 5 are some example editor panes (these will hold comments)
public class GBugForm1 extends javax.swing.JFrame {
public static void main(String[] args)
{
GBugForm1 form;
form = new GBugForm1();
form.setDefaultCloseOperation(javax.swing.JDialog.DISPOSE_ON_CLOSE);
form.setSize(500,500);
form.setVisible(true);
}
/**
* Creates new form GBugForm
*/
public GBugForm1() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jScrollPane6 = new javax.swing.JScrollPane();
jEditorPane4 = new javax.swing.JEditorPane();
jScrollPane4 = new javax.swing.JScrollPane();
jEditorPane5 = new javax.swing.JEditorPane();
jPanel4 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
setPreferredSize(new java.awt.Dimension(500, 460));
setLayout(new java.awt.BorderLayout());
jPanel1.setLayout(new java.awt.BorderLayout());
jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.PAGE_AXIS));
jEditorPane4.setPreferredSize(new java.awt.Dimension(106, 200));
jScrollPane6.setViewportView(jEditorPane4);
jPanel3.add(jScrollPane6);
jScrollPane4.setViewportView(jEditorPane5);
jPanel3.add(jScrollPane4);
jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);
jPanel4.setPreferredSize(new java.awt.Dimension(492, 105));
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 500, Short.MAX_VALUE)
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 105, Short.MAX_VALUE)
);
jPanel1.add(jPanel4, java.awt.BorderLayout.PAGE_START);
add(jPanel1, java.awt.BorderLayout.CENTER);
jPanel2.setPreferredSize(new java.awt.Dimension(400, 40));
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 500, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 40, Short.MAX_VALUE)
);
add(jPanel2, java.awt.BorderLayout.SOUTH);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JEditorPane jEditorPane4;
private javax.swing.JEditorPane jEditorPane5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane4;
private javax.swing.JScrollPane jScrollPane6;
// End of variables declaration
}
推荐答案
这是一个,可以指导您的进一步努力。指定每个面板的首选大小以强制显示滚动条;类似地,设置框架的整体大小(在 pack()
之后)以强制显示外部滚动条。有关更多信息,请参阅此。另请注意使用符合的URL。
Here's an sscce that may guide your further efforts. Each panel's preferred size is specified to force the scroll bar to appear; similarly, the frame's overall size is set (after pack()
) to force the outer scroll bar to appear. See this Q&A for more. Note also the use of an RFC 2606 compliant URL.
顺便说一下,你应该学习之前依赖GUI编辑器。
As an aside, you should probably study layouts before relying too much on a GUI editor.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
/**
* @see https://stackoverflow.com/a/12827643/230513
* @see https://stackoverflow.com/questions/4755524
*/
public class HtmlView extends JPanel {
private static final String EXAMPLE = "http://www.example.com";
private final JEditorPane jep;
public HtmlView(String url) {
super(new GridLayout(1, 1));
jep = new JEditorPane();
try {
jep.setPage(EXAMPLE);
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
jep.setEditable(false);
this.add(new JScrollPane(jep));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 200);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(new HtmlView(EXAMPLE));
panel.add(new HtmlView(EXAMPLE));
panel.add(new HtmlView(EXAMPLE));
f.add(new JScrollPane(panel));
f.pack();
f.setSize(640, 480);
f.setVisible(true);
}
});
}
}
这篇关于摆动布局 - 使用多个JEditorPanes滚动的中心面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!