JScrollPane不显示

用户名

我正在尝试使用此代码显示JscrollPane。但它显示的是空白框,仅显示“关闭”按钮。无法弄清楚为什么它不显示。任何帮助将不胜感激!:)

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;

import edu.pitt.bank.Account;
import edu.pitt.bank.Transaction;
import edu.pitt.utilities.DbUtilities;
import edu.pitt.utilities.MySqlUtilities;

public class TransactionUI {
    private JFrame  frame;
    private JScrollPane transactionPane;
    private JTable tblTransactions;

    public TransactionUI(Account userAccount) {
        frame = new JFrame();
        frame.setTitle("Account Transactions");
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        transactionPane = new JScrollPane();
        frame.getContentPane().add(transactionPane);
        DbUtilities db = new MySqlUtilities();
        String [] cols = {"Type", "Amount", "Date"};
        String sql = "SELECT type, amount, transactionDate FROM srp63_bank1017.transaction;";

        try {
            System.out.println("use getDataTable()");
            DefaultTableModel transactionList = db.getDataTable(sql, cols);
            System.out.println("getDataTable() used");
            tblTransactions = new JTable(transactionList);
            tblTransactions.setFillsViewportHeight(true);
            tblTransactions.setShowGrid(true);
            tblTransactions.setGridColor(Color.BLACK);
            transactionPane.getViewport().add(tblTransactions);
        } catch (SQLException e) {
        e.printStackTrace();
    }       
        JButton btnClose = new JButton("Close");
        btnClose.setBounds(323, 212, 89, 23);           
        btnClose.setBounds(284, 214, 73, 23);
        frame.getContentPane().add(btnClose);
    }

    public JFrame getFrame() {
        return frame;
    } 
}

我用它从另一个类中调用上述框架:

public void actionPerformed(ActionEvent arg0) { 
                if(userAccount.getAccountID() != null){
                    TransactionUI tUI = new TransactionUI(userAccount);
                    tUI.getFrame().setVisible(true);
                } else {
                    System.out.println("Account object must not be null");
                }
            }       
        });

这是getDataTable方法...

public DefaultTableModel getDataTable(String sqlQuery, String[] customColumnNames) throws SQLException{
        ResultSet rs = getResultSet(sqlQuery);
        /* Metadata object contains additional information about a ResulSet, 
         * such as database column names, data types, etc...
         */
        ResultSetMetaData metaData = rs.getMetaData();

        // Get column names from the metadata object and store them in a Vector variable
        Vector<String> columnNames = new Vector<String>();
        for(int column = 0; column < customColumnNames.length; column++){
            columnNames.add(customColumnNames[column]);
        }

        // Create a nested Vector containing an entire table from the ResultSet
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while(rs.next()){
            Vector<Object> vector = new Vector<Object>();
            for(int columnIndex = 1; columnIndex <= metaData.getColumnCount(); columnIndex++){
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }
        return new DefaultTableModel(data, columnNames);
    }

我没有收到任何错误

疯狂程序员

问题1

frame.getContentPane().setLayout(null);

避免使用null布局,像素完美布局是现代ui设计中的一种幻觉。有太多因素会影响组件的单个大小,您无法控制。Swing旨在与布局经理为核心一起工作,舍弃这些问题不会导致问题和问题的终结,您将花费越来越多的时间来进行纠正

有关更多详细信息,请参见在容器中布置组件

问题二

transactionPane.getViewport().add(tblTransactions);

请勿adJScrollPane或一起JViewport使用

transactionPane.getViewport().setView(tblTransactions);   

或者

transactionPane.setViewportView(tblTransactions);   

反而

有关更多详细信息,请参见如何使用滚动窗格

滚动窗格

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JTable table = new JTable(new DefaultTableModel(100, 100));
                table.setGridColor(Color.LIGHT_GRAY);
                JScrollPane scrollPane = new JScrollPane();
                scrollPane.setViewportView(table);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(scrollPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

问题三

使用多个窗口,请参见使用多个JFrame,良好/不良做法?进行更深入的讨论

我认为您真正想要的是某种模式对话框。有关更多详细信息,请参见如何制作对话框

您的代码无需修改

(除了删除数据库代码)

在此处输入图片说明

这就是您的代码在我的PC上的显示方式,请仔细看一下按钮...

您的代码已修改为使用布局管理器...

布局经理

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                TransactionUI ui = new TransactionUI();
                JFrame frame = ui.getFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TransactionUI {

        private JFrame frame;
        private JScrollPane transactionPane;
        private JTable tblTransactions;

        public TransactionUI() {
            frame = new JFrame();
            frame.setTitle("Account Transactions");
            frame.setBounds(100, 100, 450, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(new BorderLayout());

            transactionPane = new JScrollPane();
            frame.getContentPane().add(transactionPane);
            String[] cols = {"Type", "Amount", "Date"};
            String sql = "SELECT type, amount, transactionDate FROM srp63_bank1017.transaction;";

            System.out.println("use getDataTable()");
            DefaultTableModel transactionList = new DefaultTableModel(100, 100);
            System.out.println("getDataTable() used");
            tblTransactions = new JTable(transactionList);
            tblTransactions.setFillsViewportHeight(true);
            tblTransactions.setShowGrid(true);
            tblTransactions.setGridColor(Color.BLACK);
            transactionPane.setViewportView(tblTransactions);

            JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            JButton btnClose = new JButton("Close");
            buttons.add(btnClose);
            frame.getContentPane().add(buttons, BorderLayout.SOUTH);
        }

        public JFrame getFrame() {
            return frame;
        }
    }

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章