【问题标题】:returning the values to my the method calling it将值返回给我调用它的方法
【发布时间】:2016-01-07 20:31:52
【问题描述】:

我有以下方法

private void gettablewidths() {
   JTableHeader th = tableR.getTableHeader();
   TableColumnModel tcm = th.getColumnModel();
   for(int x = 0, y = tcm.getColumnCount(); x < y; x++)
       {
        TableColumn tc = tcm.getColumn(x);
        double header = (double) tc.getHeaderValue();
        System.out.println("Column name = "+tc.getHeaderValue()+", width = "+tc.getWidth());
        return  ;
    }
 }

它被调用

gettablewidths();

来自另一个方法(其他方法)

如何返回表格的所有列宽,以便在其他方法中使用它们?

总点是获取列宽的值 然后刷新表模型。 获取值宽度应用于表格,因此用户不必再次调整列大小。

【问题讨论】:

    标签: java return


    【解决方案1】:
      tc.getWidth()  //returns an int
    

    在循环外创建一个整数数组列表,并将 tc.getWidth()) 的结果添加到循环内的数组列表中:

    List<Integer> list = new ArrayList<>();
    
    for(int x = 0, y = tcm.getColumnCount(); x < y; x++)
        {
            tc = tcm.getColumn(x);
            list.add(tc.getWidth());
        }
    

    第 2 部分 - 用另一种方法返回你的数组列表

    更改您的方法返回类型,以便返回您的数组列表:

    public ArrayList<Integer> gettablewidths(){
    
    // logic.....
    return ArrayList;
    }
    

    【讨论】:

      【解决方案2】:

      我假设列的宽度是int 值。在您调用getTableWidths(); 的类中,您应该设置一个等于它的整数,例如int width = getTableWidths(); 你的方法应该是这样的。但是,我很困惑你为什么在循环中调用return ;,所以我把它注释掉了。

      getTableWidths()

      private int getTableWidths() 
      {
          int totalWidth = 0; // This is what you'll return as the width
          JTableHeader th = tableR.getTableHeader();
          TableColumnModel tcm = th.getColumnModel();
          for(int x = 0, y = tcm.getColumnCount(); x < y; x++)
          {
              TableColumn tc = tcm.getColumn(x);
              double header = (double) tc.getHeaderValue();
              System.out.println("Column name = " + tc.getHeaderValue() + ", width = " + tc.getWidth());
              // return  ;
              totalWidth += tc.getWidth();
          }
          return totalWidth;
      }
      

      要将其更改为返回 double,只需将其中的所有 int 定义(除了在 for 循环中)设置为 double

      这只是针对总宽度值,我没有意识到您想要表格的每个宽度。由于您已经知道该表有多少列,因此您可以根据需要使用数组。

      getTableWidths() // return integer array

      private int[] getTableWidths() 
          {
              int[] widths; // This is what you'll return as the width
              JTableHeader th = tableR.getTableHeader();
              TableColumnModel tcm = th.getColumnModel();
              widths = new int[tcm.getColumnCount()];
              for(int x = 0, y = tcm.getColumnCount(); x < y; x++)
              {
                  TableColumn tc = tcm.getColumn(x);
                  double header = (double) tc.getHeaderValue();
                  System.out.println("Column name = " + tc.getHeaderValue() + ", width = " + tc.getWidth());
                  // return  ;
                  widths[x] = tc.getWidth();
              }
              return widths;
          }
      

      【讨论】:

      • 哦,如果他的意思是所有表格的宽度都比 arggggg 我不好
      • @Riley Carney 会这样做,因为我认为你结合了宽度并将其返回到另一种方法..
      • 第一个,我标记为// return integer array的第二个返回一个整数数组,或者表格的所有宽度。
      【解决方案3】:

      将返回类型从 void 更改为您想要返回权重的类型。

      在新方法中,使其参数为gettablewidths()返回的数据类型;

      【讨论】:

        猜你喜欢
        • 2019-11-19
        • 2013-10-17
        • 2018-05-28
        • 2015-07-10
        • 2011-01-05
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多