【问题标题】:JTable nicely sized in JPanelJTable 在 JPanel 中大小合适
【发布时间】: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 旨在与布局管理器一起使用。

标签: java swing jtable jpanel


【解决方案1】:

问题是 Jtable 在 contentPane 中占据了所有位置,这就是为什么我让 JPanel 有一侧(大约 75% 的窗口)和另一个 Panel 有按钮和组合框。

因此,您将表格添加到CENTER,并将面板添加到BorderLayoutLINE_END 区域。

阅读 How to Use BorderLayout 上的 Swing 教程部分,了解更多信息和工作示例。

【讨论】:

  • 我试试你的方法,没关系,我成功添加按钮,但我无法调整按钮大小,当我尝试添加另一个按钮时,它不起作用。我需要添加其他 contentPane 并使用我的按钮和组合框执行相同的方法吗?无论如何,感谢您提供有用的建议。
  • PS:我在第一篇文章中添加了新窗口和新代码。
  • 如果你想要两个按钮在右侧,那么你需要 1) 创建一个面板,2) 将按钮添加到这个面板,3) 将面板添加到框架中。
  • 抱歉回复晚了,我成功了!在您的帮助下,也因为我使用了 setSize 而不是 setPreferredSize
  • @UnagiV,很高兴它有帮助。不要忘记通过单击复选标记“接受”答案,以便人们知道问题已解决。
猜你喜欢
  • 2013-12-17
  • 1970-01-01
  • 2016-11-05
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多