2

在此处输入图像描述

我正在制作一个 GUI,我试图在其中缩进列名。我也在粘贴我的源代码。我正在使用 GridBagConstraints。这是我创建 GridBagConstraints 的代码,但似乎什么也没发生

 private final static Dimension VOLUME_FIELD_DIMENSION = new Dimension(200, 25);
    private final static Dimension NAME_FIELD_DIMENSION = new Dimension(200, 25);
    private final static Dimension IMAGE_FIELD_DIMENSION = new Dimension(200, 25);
    private final static Dimension DATE_FIELD_DIMENSION = new Dimension(150, 25);
    private final static Dimension STATUS_FIELD_DIMENSION = new Dimension(100, 25);
    private final static Dimension IMAGE_PANEL_DIMENSION = new Dimension(1000,250);            
     private static void ImagePanel(ArrayList<ResourceListObject> arr, JPanel imagelist)
    {


        for(int i=0 ; i < arr.size();i++)
        {
            try {
                // column 1: name
                JTextField t1 = createTextField(arr.get(i).getName(), NAME_FIELD_DIMENSION);

                // column 2: Created At
                JTextField t2 = createTextField(arr.get(i).getCreatedDate(), DATE_FIELD_DIMENSION);


                // column 3: Status 
                JTextField t3 = createTextField(arr.get(i).getStatus(), STATUS_FIELD_DIMENSION);

                // column 4: Image Id
                JTextField t4 = createTextField(arr.get(i).getID(), IMAGE_FIELD_DIMENSION);

                //column 5:Volume Id
                JTextField t5 = createTextField(arr.get(i).getVolume_id(), VOLUME_FIELD_DIMENSION);
                // construct the row (panel), and then add it to the list panel
                JPanel rowPanel = new JPanel();
                rowPanel.setLayout(new BoxLayout(rowPanel, BoxLayout.X_AXIS));
                rowPanel.add(t1);
                rowPanel.add(t2);
                rowPanel.add(t3);
                rowPanel.add(t4);
                rowPanel.add(t5);
                imagelist.add(rowPanel);
            } catch (Exception e) {
                 }

        }


    }

    private static JTextField createTextField(String text, Dimension d) {
        JTextField tf = new JTextField(text);
        tf.setPreferredSize(d);
        tf.setBackground(Color.WHITE);
        tf.setEditable(false);
        return tf;
    }

    private static JPanel getColumnHeadings() {

        GridBagConstraints regularFieldConstraints = new GridBagConstraints();
        regularFieldConstraints.fill = GridBagConstraints.BOTH;
        String nameColumn = "Image Name";
        String secondColumn = "Created At";
        String thirdColumn = "Status";
        String fourthColumn = "Image Id";
        String fifthColumn = "Volume Id";

        JLabel name = new JLabel(nameColumn);
        JLabel created_at = new JLabel(secondColumn);
        JLabel status = new JLabel(thirdColumn);
        JLabel id = new JLabel(fourthColumn);
        JLabel volume_id = new JLabel(fifthColumn);

        name.setPreferredSize(NAME_FIELD_DIMENSION);
        created_at.setPreferredSize(DATE_FIELD_DIMENSION);
        status.setPreferredSize(STATUS_FIELD_DIMENSION);
        id.setPreferredSize(IMAGE_FIELD_DIMENSION);
        volume_id.setPreferredSize(VOLUME_FIELD_DIMENSION);

        JPanel headingsPanel = new JPanel(new GridBagLayout());
        headingsPanel.add(name, regularFieldConstraints);
        headingsPanel.add(created_at, regularFieldConstraints);
        headingsPanel.add(status, regularFieldConstraints);
        headingsPanel.add(id, regularFieldConstraints);
        headingsPanel.add(volume_id, regularFieldConstraints);

        return headingsPanel;
    }
4

2 回答 2

2

该类sun.swing.table.DefaultTableCellHeaderRenderer不是已发布 API 的一部分。如果您不愿意依赖它,请考虑以下替代选项:

图片

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class Demo {

    /** @see https://stackoverflow.com/a/6644956/230513 */
    private static class HeaderRenderer implements TableCellRenderer {

        TableCellRenderer renderer;

        public HeaderRenderer(JTable table) {
            renderer = table.getTableHeader().getDefaultRenderer();
        }

        @Override
        public Component getTableCellRendererComponent(
            JTable table, Object value, boolean isSelected,
            boolean hasFocus, int row, int col) {
            // assumes JLabel
            JLabel label = (JLabel) renderer.getTableCellRendererComponent(
                       table, value, isSelected, hasFocus, row, col);
            label.setHorizontalAlignment(JLabel.LEADING);
            return label;
        }
    }

    private void initGUI() {
        Object[] columnNames = new Object[]{
            "Image Name", "Created At", "Status", "Image Id", "Volume Id"};
        DefaultTableModel dtm = new DefaultTableModel(columnNames, 0);
        dtm.addRow(new Object[]{"Some picture's name", "2013-10-05",
            "Status unknown", "0123456789", "9876543210"});

        JTable table = new JTable(dtm);
        table.getTableHeader().setDefaultRenderer(new HeaderRenderer(table));

        JScrollPane scrollPane = new JScrollPane(table);

        JPanel content = new JPanel(new BorderLayout());
        content.setPreferredSize(new Dimension(600, 250));
        content.add(scrollPane);

        JFrame frame = new JFrame("Demo");
        frame.setContentPane(content);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }
}
于 2013-10-05T16:01:04.670 回答
2

我希望所有列名都是缩进,如 Image Id

如果我理解正确,您希望列标题标签左对齐,就像Image ID. 如果是这样,您可以使用TableCellRenderer来使用JTable.getHeader()方法来实现。(如果不是这样,对不起误解你的问题)

例如,您可以使用从DefaultTableCellRenderer扩展而来的sun.swing.table.DefaultTableCellHeaderRenderer

在此处输入图像描述

示例代码

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import sun.swing.table.DefaultTableCellHeaderRenderer;

public class Demo{

   private void initGUI(){
       Object[] columnNames = new Object[]{"Image Name", "Created At", "Status", "Image Id", "Volume Id"};
       DefaultTableModel dtm = new DefaultTableModel(columnNames, 0);
       dtm.addRow(new Object[]{"Some picture's name", "2013-10-05", "Status unknown", "0123456789", "9876543210"});

       /*
        * Create your own TableCellRender
        */
       TableCellRenderer headerRenderer = new DefaultTableCellHeaderRenderer(){
           @Override
           public Component getTableCellRendererComponent(JTable jtable, Object o, boolean bln, boolean bln1, int i, int i1) {
               super.getTableCellRendererComponent(jtable, o, bln, bln1, i, i1);
               setHorizontalAlignment(SwingConstants.LEADING);
               return this;
           }
       };

       JTable table = new JTable(dtm);
       table.getTableHeader().setDefaultRenderer(headerRenderer);

       JScrollPane scrollPane = new JScrollPane(table);

       JPanel content = new JPanel(new BorderLayout());
       content.setPreferredSize(new Dimension(600,250));
       content.add(scrollPane);

       JFrame frame = new JFrame("Demo");
       frame.setContentPane(content);
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       frame.pack();
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);

       try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(frame);            
        }
        catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex){
            ex.printStackTrace();
        }

   }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Demo().initGUI();
            }
        });
    }    

}
于 2013-10-05T13:35:40.510 回答