JTable 不会显示在 JPanel 上

发布于 2024-08-12 08:50:00 字数 3261 浏览 2 评论 0原文

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

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

公开课主课 { 默认表模型 table_model; String[][] 地址数据 = new String[10][5]; JPanel面板; JFrame框架; JButton加载数据; int 计数,索引,行=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 上可见的表格的任何想法

Hi I have created a Jtable and can get it to show on my frame yet not on the JPanel I have ontop of my JFrame. I can't seem to change

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

public class Main { DefaultTableModel table_model; String[][] addressData = new String[10][5]; JPanel panel; JFrame frame; JButton loadData; int count,index,row =0; String thisLine; 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(); }

}

Any ideas on how I might go about setting the table visible ontop of my JPanel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九厘米的零° 2024-08-19 08:50:00

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

一个值得考虑的简单布局管理器是 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);

You need to create your JPanel with a specific layout manager and then add components (the JTable and JButton) with appropriate constraints to cause them to appear in the correct area of the panel. Currently you are setting the panel's layout manager to null, which is almost certainly incorrect. You may want to check out the Using Layout Managers tutorial.

One simple layout manager to consider is BorderLayout. For example:

// 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);
幸福%小乖 2024-08-19 08:50:00

一个大胆的猜测:

 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) 

A wild guess:

 panel.setLayout(null);

Try this instead:

 panel.setLayout(new BorderLayout());

And later:

 panel.add(loadData, BorderLayout.SOUTH) // e.g. for the button

 // e.g. for the table
 panel.add(new JScrollPane(table, .....)), BorderLayout.CENTER) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文