【发布时间】:2017-08-18 13:40:42
【问题描述】:
Java 的表单有问题。我试图调整 JTable 的大小以占用 JPanel 上的整个空间,但它不会。你对此有什么建议吗?我向您展示了问题和我的代码,谢谢大家。
我的代码在这里
package DisplayingLibrary;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.sdz.connection.SdzConnection;
import LibraryDAO.DAO;
import LibraryTable.Ouvrage;
import LibraryTableDAO.OuvrageDAO;
public class DisplayLibrary extends JFrame{
public DisplayLibrary() {
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTable");
this.setSize(1000, 600);
this.setBounds(100, 100, 941, 495);
this.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 712, 434);
this.getContentPane().add(panel);
panel.setBackground(Color.ORANGE);
JTable table = new JTable();
JScrollPane tableSP = new JScrollPane(table);
panel.add(tableSP);
DefaultTableModel model = new DefaultTableModel();
Object[] columnsName = new Object[4];
columnsName[0] = "Id";
columnsName[1] = "Code Ouvrage";
columnsName[2] = "Nom ouvrage";
columnsName[3] = "Nom Auteur";
model.setColumnIdentifiers(columnsName);
Object[] rowData = new Object[4];
DAO<Ouvrage> ouvrageDAO = new OuvrageDAO(SdzConnection.getInstance());
ArrayList<Ouvrage> ouvrages = ouvrageDAO.getList();
for(int i = 0; i < ouvrageDAO.getList().size(); i++) {
rowData[0] = ouvrages.get(i).getId_ouvrage();
rowData[1] = ouvrages.get(i).getCode_ouvrage();
rowData[2] = ouvrages.get(i).getNom_ouvrage();
rowData[3] = ouvrages.get(i).getNom_auteur();
model.addRow(rowData);
}
table.setModel(model);
//this.getContentPane().add(new JScrollPane(table));
}
public static void main(String[] args){
DisplayLibrary dl = new DisplayLibrary();
dl.setVisible(true);
}
}
我用橙色为我的面板上色,以显示问题出在哪里,我必须隐藏所有这些颜色。我已经添加了一个 BorderLayout,或者当我想要一个 SetLayout(null) 到我的 JPanel 来调整我的 JTable 的大小时,JTable 消失了。再次感谢您的帮助。
在 Camickr 的帮助下编辑
使用以下代码
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTable");
this.setSize(1000, 600);
this.setBounds(100, 100, 941, 495);
/*JPanel panel = new JPanel();
panel.setBounds(10, 11, 712, 434);
this.getContentPane().add(panel);
panel.setBackground(Color.ORANGE);*/
JTable table = new JTable();
table.setSize(700, 400);
JScrollPane tableSP = new JScrollPane(table);
BorderLayout borlay = new BorderLayout();
//panel.add(tableSP);
this.getContentPane().add(tableSP, borlay.CENTER);
JButton button = new JButton();
button.setSize(40, 40);
this.getContentPane().add(button, borlay.LINE_END);
JButton button_2 = new JButton();
this.getContentPane().add(button_2, borlay.LINE_END);
【问题讨论】:
-
将您的表格直接添加到 contentPane。在这种情况下,
BorderLayout,它是 contentPane 的默认布局管理器,将为您的表格提供窗口的完整位置。 -
正如this documentation page 所说,“注意,在使用BorderLayout 时,必须使用
add(Component, Object)方法添加组件,以确保所有组件都正确添加。人们常犯的错误是使用add(Component),这可能会导致某些组件不可见”。 -
另外,试试
CardLayout,听起来它对你有用。 -
@M.Prokhorov 类似的东西? table.setLayout(new CardLayout());或 panel.add(tableSP, new CardLayout()); ?
-
这与 CardLayout 无关。只需使用已建议的默认 BorderLayout 即可。当然,为了让 BorderLayout 工作,您需要摆脱所有
setLayout( null )语句。不要使用空布局!!! Swing 旨在与布局管理器一起使用。