最近写Swing 遇到一点头疼的问题
package testvalue;import java.awt.GridBagLayout;import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.JTable;public class testtable extends JPanel { private static final long serialVersionUID = 1L; private JScrollPane jScrollPane = null; private JPanel jPanel = null; private JLabel jLabel = null; private JPanel jPanel1 = null; private JPanel jPanel2 = null; private JTable jTable = null; private JPanel jPanel3 = null; private JLabel jLabel1 = null; private JPanel jPanel4 = null; private JTable jTable1 = null; /**
* This is the default constructor
*/
public testtable() {
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.weightx = 1.0;
this.setSize(300, 200);
this.setLayout(new GridBagLayout());
this.add(getJScrollPane(), gridBagConstraints);
} /**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJPanel());
}
return jScrollPane;
} /**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel1 = new JLabel(new ImageIcon(".\\image\\5.jpg"));
jLabel1.setText("比率参数");
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(4); jLabel = new JLabel(new ImageIcon(".\\image\\5.jpg"));
jLabel.setLayout(new FlowLayout()); jLabel.setText("类型设置"); jPanel = new JPanel();
jPanel.setLayout(gridLayout);
jPanel.add(getJPanel1(), null);
jPanel.add(getJPanel2(), null);
jPanel.add(getJPanel3(), null);
jPanel.add(getJPanel4(), null); }
return jPanel;
} /**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel1.add(jLabel, null); }
return jPanel1;
} /**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jPanel2 = new JPanel();
jPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel2.add(getJTable(), null);
}
return jPanel2;
} /**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable(2, 2); }
return jTable;
} /**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
jPanel3 = new JPanel();
jPanel3.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel3.add(jLabel1, new GridBagConstraints());
}
return jPanel3;
} /**
* This method initializes jPanel4
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel4() {
if (jPanel4 == null) {
jPanel4 = new JPanel();
jPanel4.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel4.add(getJTable1(), null);
}
return jPanel4;
} /**
* This method initializes jTable1
*
* @return javax.swing.JTable
*/
private JTable getJTable1() {
if (jTable1 == null) {
jTable1 = new JTable(5, 2);
}
return jTable1;
}
public static void main(String args []){
JFrame frame = new JFrame();
testtable table = new testtable();
frame.add(table);
frame.pack();
frame.setVisible(true);
}}我用的是graidLayout 4行1列 但是构造出来以后他们每个JPanel之间的距离太大了 应该怎么办?
package testvalue;import java.awt.GridBagLayout;import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.JTable;public class testtable extends JPanel { private static final long serialVersionUID = 1L; private JScrollPane jScrollPane = null; private JPanel jPanel = null; private JLabel jLabel = null; private JPanel jPanel1 = null; private JPanel jPanel2 = null; private JTable jTable = null; private JPanel jPanel3 = null; private JLabel jLabel1 = null; private JPanel jPanel4 = null; private JTable jTable1 = null; /**
* This is the default constructor
*/
public testtable() {
super();
initialize();
} /**
* This method initializes this
*
* @return void
*/
private void initialize() {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.weightx = 1.0;
this.setSize(300, 200);
this.setLayout(new GridBagLayout());
this.add(getJScrollPane(), gridBagConstraints);
} /**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJPanel());
}
return jScrollPane;
} /**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel() {
if (jPanel == null) {
jLabel1 = new JLabel(new ImageIcon(".\\image\\5.jpg"));
jLabel1.setText("比率参数");
GridLayout gridLayout = new GridLayout();
gridLayout.setRows(4); jLabel = new JLabel(new ImageIcon(".\\image\\5.jpg"));
jLabel.setLayout(new FlowLayout()); jLabel.setText("类型设置"); jPanel = new JPanel();
jPanel.setLayout(gridLayout);
jPanel.add(getJPanel1(), null);
jPanel.add(getJPanel2(), null);
jPanel.add(getJPanel3(), null);
jPanel.add(getJPanel4(), null); }
return jPanel;
} /**
* This method initializes jPanel1
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel1() {
if (jPanel1 == null) {
jPanel1 = new JPanel();
jPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel1.add(jLabel, null); }
return jPanel1;
} /**
* This method initializes jPanel2
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel2() {
if (jPanel2 == null) {
jPanel2 = new JPanel();
jPanel2.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel2.add(getJTable(), null);
}
return jPanel2;
} /**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable(2, 2); }
return jTable;
} /**
* This method initializes jPanel3
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel3() {
if (jPanel3 == null) {
jPanel3 = new JPanel();
jPanel3.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel3.add(jLabel1, new GridBagConstraints());
}
return jPanel3;
} /**
* This method initializes jPanel4
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel4() {
if (jPanel4 == null) {
jPanel4 = new JPanel();
jPanel4.setLayout(new FlowLayout(FlowLayout.LEFT));
jPanel4.add(getJTable1(), null);
}
return jPanel4;
} /**
* This method initializes jTable1
*
* @return javax.swing.JTable
*/
private JTable getJTable1() {
if (jTable1 == null) {
jTable1 = new JTable(5, 2);
}
return jTable1;
}
public static void main(String args []){
JFrame frame = new JFrame();
testtable table = new testtable();
frame.add(table);
frame.pack();
frame.setVisible(true);
}}我用的是graidLayout 4行1列 但是构造出来以后他们每个JPanel之间的距离太大了 应该怎么办?
方法二:使用null布局,然后给你的4个panel分别setBounds(),这样你想放哪就放哪,最自由,最美观