JScrollPane水平添加项目,但我希望它们垂直添加

时间:2014-11-08 11:32:26

标签: java user-interface jpanel jscrollpane

我正在创建一个我想要向上和向下滚动的定制表,所以我使用的是JScrollPane。

我的问题是,每次我在表格中添加另一行时,它会将其添加到最后一行的一侧,而不是在最后一行的下方,这是我想要的那一行。例如,它是向表而不是行添加列。

需要添加的行数不确定,具体取决于表中的条目数。

我知道我可以使用表格但是出于本练习的目的,我想这样做。

以下是我的代码的MCVE版本,用于演示此问题。我把颜色改变了,所以字段更明显:

public class Customers {


     public static JFrame frame = new JFrame();

     public static void frameGui(JPanel panel, String name){

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setContentPane(panel);

            frame.setSize(1200,500);

            frame.setVisible(true);
     }



     public static void ScrollCustomersGui(){           

            JPanel Table = new JPanel();

            Table.add(customersTableHeadings(Table));
            Table.add(customersTableHeadings(Table));  
            Table.add(customersTableHeadings(Table));  

            JScrollPane scroll = new JScrollPane(Table);

            JPanel All = new JPanel(new BorderLayout());
            All.add(scroll);

            frameGui(All, "Customers");
        }

    public static JPanel customersTableHeadings(JPanel panel){

         FlowLayout FlowCustTable = (FlowLayout)panel.getLayout();
         FlowCustTable.setHgap(0);

         JPanel customersTable = new JPanel(FlowCustTable);

         JTextField custid = new JTextField("ONE");
         custid.setPreferredSize(new Dimension(500, 50));
         custid.setBackground(Color.CYAN);

         customersTable.add(custid);

         return customersTable;

    }





}

2 个答案:

答案 0 :(得分:2)

使用垂直BoxLayout这里是修改后的代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class Customers
{

    public static JFrame frame = new JFrame();

    public static void frameGui(JPanel panel, String name)
    {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.setSize(1200, 500);
        frame.setVisible(true);
    }

    public static void ScrollCustomersGui()
    {
        Box box = Box.createVerticalBox();
        //JPanel Table = new JPanel();
        //Table.setLayout(new BoxLayout(Table, BoxLayout.Y_AXIS));

        box.add(customersTableHeadings());
        box.add(customersTableHeadings());
        box.add(customersTableHeadings());

        JScrollPane scroll = new JScrollPane(box);

        JPanel All = new JPanel(new BorderLayout());
        All.add(scroll);

        frameGui(All, "Customers");
    }

    public static JPanel customersTableHeadings()
    {
        // FlowLayout FlowCustTable = (FlowLayout) panel.getLayout();
        // FlowCustTable.setHgap(0);

        JPanel customersTable = new JPanel();
        customersTable.setMaximumSize(new Dimension(550, 60));

        JTextField custid = new JTextField("ONE");
        custid.setPreferredSize(new Dimension(500, 50));
        custid.setBackground(Color.CYAN);

        customersTable.add(custid);

        return customersTable;

    }

    public static void main(String[] args)
    {
        ScrollCustomersGui();
    }
}

答案 1 :(得分:1)

你使用的FlowLayout不能用于你的情况你可以使用BoxLayout检查它here,你也可以使用GridBagLayout但它很复杂:)