java - 将 JScrollPane 添加到 JFrame

标签 java swing

我想通过这个函数 createDatabaseViewGUI 将 JScrollPane 添加到 JFrame 中,任何人都可以指导我如何执行此操作,以便我可以将此 jtable 和 jscrollpane 添加到 jframe 中并使其正常工作

package schoolclient;

import java.awt.Dimension;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import schoolserver.SchoolInterface;

public class RMIClient extends JFrame {
    SchoolInterface sii = null;
    Object data = null;
    public static void main(String[] args) {
        RMIClient rmic = new RMIClient();
        rmic.getSchoolInstacne();
        //rmic.createDatabaseViewGUI();
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jFrame.setVisible(true);
        jFrame.setSize(270, 320);
        jFrame.setContentPane(rmic.createDatabaseViewGUI());
        //Object[] o = rmic.getDatabaseToList();
        //for(Object o : rmic.getDatabaseToList())
        //System.out.println(o);
    }

    public void getSchoolInstacne() {
        try {
            sii = (SchoolInterface) Naming.lookup("SchoolServer"); 
        }
        catch (RemoteException | MalformedURLException | NotBoundException e) {
            e.printStackTrace();
        }
    }

    public ArrayList getDatabaseToList() {

        ArrayList<Object> data = null;
        try {
            data = sii.getDatabsesName();

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    public void createDatabaseViewGUI() {
        Object[] x = new Object[10];
        int i = 0;
        for(Object o : getDatabaseToList())
            x[i++] = o;
        DefaultTableModel defaultTable = new DefaultTableModel();
        defaultTable.addColumn("Databses Names", x);
        JTable jTable = new JTable(defaultTable);
        jTable.setPreferredScrollableViewportSize(new Dimension(250, 350));
        jTable.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(jTable);
        jTable.getColumnModel().getColumn(0).setPreferredWidth(150);
        int[] selection = jTable.getSelectedRows();
           for (int j = 0; j < selection.length; j++) {
             selection[j] = jTable.convertRowIndexToModel(selection[j]);
        super.add(scrollPane);

        jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ListSelectionModel lsm = jTable.getSelectionModel();
        lsm.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                int row = jTable.getSelectedRow();
                data = (Object)jTable.getValueAt(row, 1);
            }
        });
          }
    }
}

最佳答案

我可以猜到,你的问题在这里:rmic.createDatabaseViewGUI()。此方法不能为void。它必须返回值。

jFrame.getContentPane(rmic.createDatabaseViewGUI());

来自documentation :

setContentPane(Container contentPane)
Sets the contentPane property.

关于java - 将 JScrollPane 添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44463610/

相关文章:

java - 使用循环和字符串数组创建 JMenuBar 错误

java - JFrame中如何访问多个JPanel?

java - JTextField 出现故障

java - Android 查询 - 检测位置

Java 数组索引越界异常

java - 为什么 Android 虚拟设备无法运行我的项目?

java - jcombobox 弹出菜单

java - 将默认区域设置分隔符与 NumberFormat 结合使用,然后对数字进行舍入

java - 如何使用 Apollo graphql 动态调整查询形状(字段)?

java - 在 JPanel 内绘画