当前位置: 首页 > 知识库问答 >
问题:

动态地将JPanel添加到JScrollPane

竺展
2023-03-14

因此,我的问题是面板被插入到JScrollPane中,这些面板的大小正在减小,但没有滚动条。

这是我的代码,我已经播种很远:

    public class ContractForm extends JFrame {

    private JPanel contentPane;
    private JTextField txtContractID;
    private JScrollPane scrollPane; 
    private JButton btnAddComponent;
    private JButton btnOk;
    private JComboBox<Customer> comboBoxCustomer;
    private JButton btnCancel;
    private ArrayList<JPanel> rows;
    private JPanel panel;
    private Dimension size;


    /**
     * Create the frame.
     */
    public ContractForm() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(250, 250, 654, 476);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

                rows = new ArrayList<JPanel>();

        JLabel lblContractid = new JLabel("ContractID");
        lblContractid.setBounds(50, 11, 78, 14);
        contentPane.add(lblContractid);

        txtContractID = new JTextField();
        txtContractID.setBounds(138, 8, 86, 20);
        contentPane.add(txtContractID);
        txtContractID.setColumns(10);

        JLabel lblNewLabel = new JLabel("Customer");
        lblNewLabel.setBounds(276, 11, 69, 14);
        contentPane.add(lblNewLabel);

        comboBoxCustomer = new JComboBox<Customer>();
        comboBoxCustomer.setBounds(355, 8, 235, 20);
        contentPane.add(comboBoxCustomer);

        JLabel lblYear = new JLabel("Year:");
        lblYear.setBounds(50, 37, 46, 14);
        contentPane.add(lblYear);

        JSpinner spinner = new JSpinner();
        spinner.setBounds(138, 31, 86, 20);
        contentPane.add(spinner);

        scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setBounds(0, 59, 628, 312);
        contentPane.add(scrollPane);

        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        size = panel.getSize();
        scrollPane.setViewportView(panel);


        btnAddComponent = new JButton("addComponent");
        btnAddComponent.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                ArrayList<JobDescription> jobs = Database.getInstance().getJobDescription();
                JobDescriptionContractRow row = new JobDescriptionContractRow(jobs);
                rows.add(row);
                panel.add(row);
                scrollPane.repaint();
                scrollPane.revalidate();

            }
        });
        btnAddComponent.setBounds(355, 30, 114, 23);
        contentPane.add(btnAddComponent);

        btnOk = new JButton("Ok");
        btnOk.setBounds(185, 382, 89, 23);
        contentPane.add(btnOk);

        btnCancel = new JButton("Cancel");
        btnCancel.setBounds(298, 382, 89, 23);
        contentPane.add(btnCancel);




    }
}

public class JobDescriptionContractRow extends JPanel {
    private JTextField textField;
    private JButton btnRemove;

    /**
     * Create the panel.
     */
    public JobDescriptionContractRow(ArrayList<JobDescription> jobs) {
        setLayout(null);

        JLabel lblJobdescription = new JLabel("Job description");
        lblJobdescription.setBounds(10, 14, 94, 14);
        add(lblJobdescription);

        JComboBox<JobDescription> comboBox = new JComboBox<JobDescription>();
        comboBox.setBounds(102, 11, 149, 20);
        for(JobDescription job : jobs){
            comboBox.addItem(job);
        }
        comboBox.setSelectedItem(null);
        add(comboBox);

        textField = new JTextField();
        textField.setBounds(365, 11, 86, 20);
        add(textField);
        textField.setColumns(10);

        JLabel lblNewLabel = new JLabel("Contract price");
        lblNewLabel.setBounds(277, 14, 78, 14);
        add(lblNewLabel);

        JButton btnRemove = new JButton("remove");
        btnRemove.setBounds(469, 10, 89, 23);
        add(btnRemove);


    }
}

共有2个答案

冷英博
2023-03-14

您可以强制JScrollPane中的一个控件调整大小。

JViewport v = new JViewport();
v.setSetViewSize(new Dimention(width,height));
scroll.setViewport(v); 
田玉韵
2023-03-14

JPanel(您的JScrollPane视图组件)默认使用FlowLayout,它尊重组件的首选大小。您可以重写所添加的JPanels的getPreferredsize以正确调整它们的大小。如果不这样做,JScrollPane将计算所有大小以适应ViewPortView

旁注:避免使用绝对定位(nulllayout)并使用布局管理器。

 类似资料:
  • 问题内容: 我对此有疑问。我有一个JPanel,通常我会像这样创建一个JLabel: 但是我希望每次单击一个按钮时,在该面板中创建一个新的JLabel,它的大小相同,但高度不同。我试过了: 但是这样一来,我就无法设定界限。我从JTextField获得的stringName。 问题答案: 首先,使用layout。正确完成布局后,组件将按照需要放置。其次,在向布局动态添加组件时,您需要告诉布局更新。这

  • 问题内容: 在NetBeans中,我已经使用GUI编辑器制作了一个JFrame,并且在框架中放置了一个JPanel。目前,我正在尝试在类构造时在面板中创建一个新按钮。这是我的代码,但似乎无法正常工作。(第一行显示该按钮,其他行尝试显示该按钮。) 我整夜都在搜寻Google,但似乎无法正常运作。 问题答案: 有时候,您看不到按钮是布局管理器问题(因为您没有为布局管理器设置正确的属性)。您可以通过禁用

  • 问题内容: 我有一个gui,它的Panel包含一系列标签和TextField,并使用spring布局(这是mainPanel),而另一个Panel仅包含button(buttonPanel)。我正在尝试使我的mainPanel也具有垂直滚动条。我想实现我的GUI,以便在JFrame中有2个面板。mainPanel出现在框架的顶部,而buttonPanel出现在mainPanel的下方。 我的问题是

  • 我目前有一个主JFrame包含几个JPanels,每个面板中都有一些文本。创建JPanels的代码在一个单独的类中(它“实现”JPanels)。如何仅向单个面板添加JScrollPane? 我已经花了一些时间尝试这样做,但什么也没有发生。顺便说一下,我的JPanel使用GridLayout 上面的类是另一个面板类的实例变量,它是主面板“MenuHold”的实例变量。但使用“MenuHolder”的

  • 我正在尝试将此 JPanel 添加到 JFrame 中,但我看不到他。当我创建新的 java 类并自己制作这个框架时,一切都很好。 来自JFrame表单的代码。 来自 JPanel 的代码: 来自Java类的代码。 我想不出区别在哪里。有人能帮我吗?

  • 我正在创建一个swing应用程序,并希望将JfreeChart添加到JPanel中。我创建了一个方法,在该方法中我创建了如下所示的图表: 创建了一个全局变量: 由于某种原因,当我点击按钮时,图表没有显示出来。