JScrollPane没有显示

时间:2015-02-15 20:47:10

标签: java swing 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 个答案:

答案 0 :(得分:2)

问题#1

frame.getContentPane().setLayout(null);

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

有关详细信息,请参阅Laying Out Components Within a Container

问题#2

transactionPane.getViewport().add(tblTransactions);

请勿adJScrollPane使用JViewport,请使用

transactionPane.getViewport().setView(tblTransactions);   

transactionPane.setViewportView(tblTransactions);   

代替

请参阅 How to Use Scroll Panes了解更多详情

ScrollPane

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);
            }
        });
    }

}

问题#3

使用多个窗口,请参阅The Use of Multiple JFrames, Good/Bad Practice?进行更深入的讨论

我认为你真正想要的是某种模态对话。有关详细信息,请参阅How to Make Dialogs

您的代码未经修改

(除了删除数据库代码)

enter image description here

这就是你的代码在我的电脑上的样子,仔细看看按钮......

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

Layout managers

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;
        }
    }

}