下面代码是一个模拟左右选择器的swing,其中左边面板中加入了很多个复选框的测试数据,不幸的是他们超出了规定的面板范围,求修正,谢谢import java.awt.Checkbox;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;import javax.swing.JPanel;
import javax.swing.ScrollPaneConstants;public class ChooseCSSFrame extends javax.swing.JDialog { private static final long serialVersionUID = 1L; private Font currentFont = new java.awt.Font("新宋体", 0, 14);
private Font currentFont4PanelTitle = new java.awt.Font("新宋体", 0, 12); private javax.swing.JButton addNoteButton;
private javax.swing.JButton addAllButton;
private javax.swing.JButton delNoteButton;
private javax.swing.JButton delAllButton;
private javax.swing.JButton sureButton;
private javax.swing.JButton cancelButton;
private javax.swing.JScrollPane showNoteJSPanel;
private javax.swing.JPanel showNotePanel;
private javax.swing.JPanel donnotShowNotePanel;
private javax.swing.JPanel buttonPanel; private List<String> cssList; public ChooseCSSFrame(List<String> cssList) {
this.cssList = cssList;
initComponents();
} private void initComponents() { showNoteJSPanel = new javax.swing.JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
showNotePanel = new JPanel();
donnotShowNotePanel = new javax.swing.JPanel();
buttonPanel = new javax.swing.JPanel();
addNoteButton = new javax.swing.JButton();
addAllButton = new javax.swing.JButton();
delNoteButton = new javax.swing.JButton();
delAllButton = new javax.swing.JButton();
sureButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton(); showNoteJSPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "显示的节点", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, currentFont4PanelTitle)); // NOI18N
showNotePanel.setLayout(new java.awt.GridLayout(cssList.size(), 1));
showNoteJSPanel.setViewportView(showNotePanel); for (String note : cssList) {
showNotePanel.add(new Checkbox(note));
} donnotShowNotePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "不显示的节点",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, currentFont4PanelTitle)); // NOI18N
donnotShowNotePanel.setLayout(new java.awt.GridLayout()); addNoteButton.setFont(currentFont); // NOI18N
addNoteButton.setText("添加");
addNoteButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); addAllButton.setFont(currentFont); // NOI18N
addAllButton.setText("添加全部");
addAllButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); delNoteButton.setFont(currentFont); // NOI18N
delNoteButton.setText("删除");
delNoteButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); delAllButton.setFont(currentFont); // NOI18N
delAllButton.setText("删除全部");
delAllButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(buttonPanel);
buttonPanel.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel3Layout.createSequentialGroup().addGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(delNoteButton,
javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE).addComponent(
addAllButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
.addComponent(addNoteButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 106,
Short.MAX_VALUE).addComponent(delAllButton, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
.addContainerGap()));
jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(addNoteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(
addAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(delNoteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31,
javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(
delAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); sureButton.setFont(currentFont); // NOI18N
sureButton.setText("确定");
sureButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
sureButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
}); cancelButton.setFont(currentFont); // NOI18N
cancelButton.setText("取消");
cancelButton.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
}); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addGroup(
layout.createSequentialGroup().addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(
sureButton, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(
javax.swing.GroupLayout.Alignment.LEADING,
layout.createSequentialGroup().addGap(22, 22, 22).addComponent(showNoteJSPanel,
javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(buttonPanel,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(cancelButton,
javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(
donnotShowNotePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(22, 22, 22)));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup().addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup().addContainerGap().addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(136, 136, 136))
.addGroup(
layout.createSequentialGroup().addGap(32, 32, 32).addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false).addComponent(
donnotShowNotePanel, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(showNoteJSPanel, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)).addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE))).addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(sureButton,
javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(cancelButton,
javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap())); pack();
setLocationRelativeTo(null);
} private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
} private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
} public static void main(String args[]) {
List<String> cssList = new ArrayList<String>();
cssList.add("content");
cssList.add("title");
cssList.add("line");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
cssList.add("name");
new ChooseCSSFrame(cssList).setVisible(true);
}}
over thanks
JavaSwing布局