【问题标题】:JTable Won't Show On JPanelJTable 不会显示在 JPanel 上
【发布时间】:2010-12-19 16:38:30
【问题描述】:

您好,我创建了一个 Jtable,可以让它显示在我的框架上,但不能显示在我的 JFrame 顶部的 JPanel 上。我似乎无法改变

导入 java.awt.;
导入 java.awt.event.ActionEvent;
导入 java.awt.event.ActionListener;
导入 java.io.;
导入 java.util.ArrayList;
导入 java.util.Comparator;
导入 javax.swing.;
导入 javax.swing.table.;

公共类主要 { DefaultTableModel table_model; 字符串[][] 地址数据 = 新字符串[10][5]; JPanel面板; JFrame框架; JButton 加载数据; 整数计数,索引,行 = 0; 把这条线串起来; ArrayList People = new ArrayList();

public Main() { //Creating JFrame and setting properties frame = new JFrame(); frame.setResizable(false); frame.setTitle("Address Book"); frame.setSize(800,600); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); //Creating JPanel and setting properties panel = new JPanel(); panel.setLayout(null); panel.setBackground(new Color(77,81,84)); //Setting the table and Scroll Bars this.table_model = new DefaultTableModel(addressData, new String[]{"First Name", "Surname", "Home Number", "Mobile Number", "Address", "Postcode"}); JTable table = new JTable(this.table_model); table.setBounds(130, 40, 200, 200); panel.add(new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)); //Load Data button, reading file in and adding data to ArrayList then Array of Arrays loadData = new JButton("Load File"); loadData.setBounds(10, 10, 100, 20); loadData.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { BufferedReader reader = new BufferedReader(new FileReader("file/address.buab")); while ((thisLine = reader.readLine()) !=null) { if (row >= 4) { index++; row = 0; People.add(thisLine); addressData[index][row] = People.get(count); count++; row++; } else { People.add(thisLine); addressData[index][row] = People.get(count); count++; row++; } } reader.close(); } catch (IOException ex) { System.err.println("Input Exception, Check address.buab File"); } } }); panel.add(loadData); //Auto sort on table fields TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(this.table_model); sorter.setComparator(1, new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } public boolean equals(Object obj) { return obj.equals(this); } }); table.setRowSorter(sorter); frame.getContentPane().add(panel); frame.setVisible(true); } public static void main(String[] args) { new Main(); }

}

关于如何将表格设置在我的 JPanel 上可见的任何想法

【问题讨论】:

    标签: java swing jtable jpanel


    【解决方案1】:

    您需要使用特定的布局管理器创建JPanel,然后添加具有适当约束的组件(JTableJButton),以使它们出现在面板的正确区域。目前您将面板的布局管理器设置为null,这几乎肯定是不正确的。您可能想查看Using Layout Managers 教程。

    要考虑的一个简单布局管理器是BorderLayout。例如:

    // Create JPanel with BorderLayout layout manager.
    JPanel panel = new JPanel(new BorderLayout());
    
    // Add JTable to panel center.  This component will expand
    // to take up available space.
    panel.add(myTableScrollPane, BorderLayout.CENTER);
    
    // Add button to the bottom of the panel.
    panel.add(myButton, BorderLayout.SOUTH);
    

    【讨论】:

      【解决方案2】:

      一个疯狂的猜测:

       panel.setLayout(null);
      

      试试这个:

       panel.setLayout(new BorderLayout());
      

      后来:

       panel.add(loadData, BorderLayout.SOUTH) // e.g. for the button
      
       // e.g. for the table
       panel.add(new JScrollPane(table, .....)), BorderLayout.CENTER) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-24
        • 2014-01-16
        • 1970-01-01
        • 2014-09-29
        • 2016-08-29
        相关资源
        最近更新 更多