滚动条未创建/表格未显示

问题描述 投票:0回答:1

我正在编写一个程序,其中需要创建一个 JTable(我能够)。但我想给它添加一个滚动条。下面是我的代码。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class TagReplaceUI extends JFrame {

    private JPanel contentPane;
    private JTextField srcTextField;
    private Executor executor = Executors.newCachedThreadPool();
    static DefaultTableModel model = new DefaultTableModel();
    static JTable table = new JTable(model);

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final TagReplaceUI frame = new TagReplaceUI();
                    frame.add(new JScrollPane(table));
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    // FileDownloadTest downloadTest = new
    // FileDownloadTest(srcTextField.getText(), textArea);

    public TagReplaceUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 552, 358);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        srcTextField = new JTextField();
        srcTextField.setBounds(10, 26, 399, 20);
        contentPane.add(srcTextField);
        srcTextField.setColumns(10);

        JButton srcBtn = new JButton("Source Excel");
        srcBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fChoose = new JFileChooser();
                fChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (fChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    srcTextField.setText(fChoose.getSelectedFile().getAbsolutePath());
                } else {
                    System.out.println("No Selection");
                }
            }
        });

        table.setBounds(10, 131, 516, 178);
        contentPane.add(table);
        model.addColumn("Col1");
        model.addColumn("Col2");

        srcBtn.setBounds(419, 25, 107, 23);
        contentPane.add(srcBtn);

        JButton dNcButton = new JButton("Process");
        dNcButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                executor.execute(new Test(srcTextField.getText(), model));
            }
        });
        dNcButton.setBounds(212, 70, 89, 23);
        contentPane.add(dNcButton);

    }

}

当我运行上面的程序时,表格框架也没有显示。但是当我删除

frame.add(new JScrollPane(table));
时,它在
JTable
区域中显示表格,但其中没有滚动条。

如何在表格中获得滚动条并正确显示数据?

java swing jtable jscrollpane
1个回答
2
投票

首先不要使用空布局,请阅读这里

frame.add(new JScrollPane(table));
此滚动窗格未显示,因为在空布局中每个组件都需要有边界。尝试下面的代码。我刚刚将静态变量更改为实例变量。并将滚动窗格添加到构造函数中。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class TagReplaceUI extends JFrame {

    private JPanel contentPane;
    private JTextField srcTextField;
    private Executor executor = Executors.newCachedThreadPool();
    private DefaultTableModel model = new DefaultTableModel();
    private JTable table = new JTable(model);

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    final TagReplaceUI frame = new TagReplaceUI();

                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    // FileDownloadTest downloadTest = new
    // FileDownloadTest(srcTextField.getText(), textArea);

    public TagReplaceUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 552, 358);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        srcTextField = new JTextField();
        srcTextField.setBounds(10, 26, 399, 20);
        contentPane.add(srcTextField);
        srcTextField.setColumns(10);

        JButton srcBtn = new JButton("Source Excel");
        srcBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fChoose = new JFileChooser();
                fChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (fChoose.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                    srcTextField.setText(fChoose.getSelectedFile().getAbsolutePath());
                } else {
                    System.out.println("No Selection");
                }
            }
        });

        JScrollPane scroll = new JScrollPane(table);
        scroll.setBounds(10, 131, 516, 178);
        contentPane.add(scroll);
        model.addColumn("Col1");
        model.addColumn("Col2");

        srcBtn.setBounds(419, 25, 107, 23);
        contentPane.add(srcBtn);

        JButton dNcButton = new JButton("Process");
        dNcButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                executor.execute(new Test(srcTextField.getText(), model));
            }
        });
        dNcButton.setBounds(212, 70, 89, 23);
        contentPane.add(dNcButton);

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.