jtabel换行符

omvjsjqw  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(293)

我脑子里的线断了 JTable .
我试过很多解决办法,但都不管用,所以我问这个问题。
有人能帮我修一下这个,而不改变我的习惯吗 JTable 建造师。
有关我的代码的一些详细信息:
我的程序在两个jtabbedpane中显示jtables,在表0中还有4个jtabbedpane包含相同的表。
我的程序只能全屏运行。
这个程序只是对jtable和jtabbedpanes函数的测试。
我的问题集中在jtable单元格中的换行符上。
代码:

import java.awt.*;
import java.awt.event.*;

import java.util.*;

import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.event.*;
import javax.swing.table.*;

public class Versuch01 extends JFrame {
  // Anfang Attribute
  JTabbedPane[] jTabbedPane;
  JPanel[] jPanelH;
  JPanel[] jPanelL;
  JScrollPane[] jScrollPane;
  JTable[] jTable;
  final int wordWrapColumnIndex = 5;
  // Ende Attribute
  public Versuch01() {
    // Frame-Initialisierung
    super();
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int frameWidth = 400;
    int frameHeight = 400;
    setSize(frameWidth, frameHeight);

    int x1 = (d.width - getSize().width) / 2;
    int y1 = (d.height - getSize().height) / 2;
    setLocation(x1, y1);

    int x = (int) d.getWidth();
    int y = (int) d.getHeight();
    setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);

    setLayout(new BorderLayout());
    setTitle("Versuch01");
    setResizable(true);

    String[] columnNames = {
                             "First Name", "Last Name", "Sport", "# of Years",
                             "Vegetarian"
                           };
    Object[][] data = {
                        {
                          "Kathy",
                          "Smithaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa aaaaaaaaaaaaaaa",
                          "Snowboarding", new Integer(5), new Boolean(true)
                        },
                        {
                          "John", "Doe", "Rowing", new Integer(3),
                          new Boolean(true)
                        },
                        {
                          "Sue", "Black", "Knitting", new Integer(2),
                          new Boolean(false)
                        },
                        {
                          "Jane", "White", "Speed reading", new Integer(20),
                          new Boolean(false)
                        },
                        {
                          "Joe", "Brown", "Pool", new Integer(10),
                          new Boolean(false)
                        }
                      };

    // Anfang Komponenten
    jTabbedPane = new JTabbedPane[2];

    for (int i = 0; i < jTabbedPane.length; i++) {
      jTabbedPane[i] = new JTabbedPane(JTabbedPane.TOP,
                                       JTabbedPane.WRAP_TAB_LAYOUT);
    } // end of for

    jPanelH = new JPanel[2];

    for (int i = 0; i < jPanelH.length; i++) {
      jPanelH[i] = new JPanel();
      jTabbedPane[0].addTab("Tab " + i, jPanelH[i]);
    } // end of for

    jPanelL = new JPanel[4];
    jTable = new JTable[4];
    jScrollPane = new JScrollPane[4];

    for (int i = 0; i < jPanelL.length; i++) {
      jPanelL[i] = new JPanel();
      MultilineTableCell wordWrapRenderer = new  MultilineTableCell();
      jTable[i] = new JTable(data, columnNames) {
          public TableCellRenderer getCellRenderer(int row, int column) {
            if (column == wordWrapColumnIndex) {
              return wordWrapRenderer;
            } else {
              return super.getCellRenderer(row, column);
            }
          }
        };
      jTable[i].setDefaultRenderer(String.class, new LineWrapCellRenderer());
      jTable[i].setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      jTable[i].getColumnModel().getColumn(0)
               .setPreferredWidth((int) ((double) x * (0.05))); //Gibt die relative Größe in dezimalen Zahlen zu Tabbele an.
      jTable[i].getColumnModel().getColumn(1)
               .setPreferredWidth((int) ((double) x * (0.65)));
      jTable[i].getColumnModel().getColumn(2)
               .setPreferredWidth((int) ((double) x * (0.1)));
      jTable[i].getColumnModel().getColumn(3)
               .setPreferredWidth((int) ((double) x * (0.1)));
      jTable[i].getColumnModel().getColumn(4)
               .setPreferredWidth((int) ((double) x * (0.1)));
      jScrollPane[i] = new JScrollPane(jTable[i]);
      jScrollPane[i].setPreferredSize(new Dimension(x, y));
      jPanelL[i].add(jScrollPane[i]);
      jTabbedPane[1].addTab("Tab " + i, jPanelL[i]);
      jTabbedPane[1].setTabPlacement(JTabbedPane.TOP);
      jTabbedPane[1].setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
      jTabbedPane[1].setPreferredSize(new Dimension(x, y));
      jPanelH[0].add(jTabbedPane[1]);
    } // end of for

    add(jTabbedPane[0]);

    // Ende Komponenten
    setVisible(true);
  } // end of public Versuch01

  // Anfang Methoden
  public static void main(String[] args) {
    new Versuch01();
  } // end of main

  // Ende Methoden
} // end of class Versuch01

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题