java中布局问题
我在JPanel中添加了4个button,使用BoxLayout布局管理器垂直排列,但是这4个按钮在垂直方向上紧紧地挨在一起,我想在每个按钮之间设置空隙,这应该怎么弄啊?...
我在JPanel中添加了4个button,使用BoxLayout布局管理器垂直排列,但是这4个按钮在垂直方向上紧紧地挨在一起,我想在每个按钮之间设置空隙,这应该怎么弄啊?
展开
4个回答
展开全部
使用BOX类的createRigidArea方法
JButton button;
Container pane = getContentPane();
BoxLayout boxlayout = new BoxLayout(pane,BoxLayout.Y_AXIS);
pane.setLayout(boxlayout);
pane.add(Box.createRigidArea(new Dimension(100,10)));//设置上下左右的间距
JButton button;
Container pane = getContentPane();
BoxLayout boxlayout = new BoxLayout(pane,BoxLayout.Y_AXIS);
pane.setLayout(boxlayout);
pane.add(Box.createRigidArea(new Dimension(100,10)));//设置上下左右的间距
展开全部
public class NewJFrame1 extends javax.swing.JFrame {
/** Creates new form NewJFrame1 */
public NewJFrame1() {
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() {
java.awt.GridBagConstraints gridBagConstraints;
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form"); // NOI18N
getContentPane().setLayout(new java.awt.GridBagLayout());
jScrollPane1.setName("jScrollPane1"); // NOI18N
jTree1.setName("jTree1"); // NOI18N
jScrollPane1.setViewportView(jTree1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane1, gridBagConstraints);
jLabel1.setText(org.openide.util.NbBundle.getMessage(NewJFrame1.class, "NewJFrame1.jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipady = 15;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
getContentPane().add(jLabel1, gridBagConstraints);
jLabel2.setText(org.openide.util.NbBundle.getMessage(NewJFrame1.class, "NewJFrame1.jLabel2.text")); // NOI18N
jLabel2.setName("jLabel2"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipady = 15;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
getContentPane().add(jLabel2, gridBagConstraints);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration
}
/** Creates new form NewJFrame1 */
public NewJFrame1() {
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() {
java.awt.GridBagConstraints gridBagConstraints;
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setName("Form"); // NOI18N
getContentPane().setLayout(new java.awt.GridBagLayout());
jScrollPane1.setName("jScrollPane1"); // NOI18N
jTree1.setName("jTree1"); // NOI18N
jScrollPane1.setViewportView(jTree1);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jScrollPane1, gridBagConstraints);
jLabel1.setText(org.openide.util.NbBundle.getMessage(NewJFrame1.class, "NewJFrame1.jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipady = 15;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
getContentPane().add(jLabel1, gridBagConstraints);
jLabel2.setText(org.openide.util.NbBundle.getMessage(NewJFrame1.class, "NewJFrame1.jLabel2.text")); // NOI18N
jLabel2.setName("jLabel2"); // NOI18N
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.ipady = 15;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
getContentPane().add(jLabel2, gridBagConstraints);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTree jTree1;
// End of variables declaration
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你在后面加上它的宽度和高度呀,你去查查文档他的构造方法有这个的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改为手动布局吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询