Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 动态向JScrollPane添加组件_Java_Swing_Awt - Fatal编程技术网

Java 动态向JScrollPane添加组件

Java 动态向JScrollPane添加组件,java,swing,awt,Java,Swing,Awt,我是Java Swing新手,我想开发一个POS应用程序,如下图所示: 为了开发这个应用程序,我正在使用Eclipse,我创建了JPanel,它在图中以数字显示,例如(1,2)。在3号面板上,我添加了包含JPanel的JscrollPan,现在我想在这里单击2号面板上的一个按钮,该按钮应该在3号面板中动态添加新按钮,当时我只想在每一行显示三个按钮,并且滚动应该在需要时垂直激活。但我不能这样做,因为当我编写scrollPanel.setPreferredSize(新维度(2,3))时;垂直滚动无

我是Java Swing新手,我想开发一个POS应用程序,如下图所示:

为了开发这个应用程序,我正在使用Eclipse,我创建了JPanel,它在图中以数字显示,例如(1,2)。在3号面板上,我添加了包含JPanel的JscrollPan,现在我想在这里单击2号面板上的一个按钮,该按钮应该在3号面板中动态添加新按钮,当时我只想在每一行显示三个按钮,并且滚动应该在需要时垂直激活。但我不能这样做,因为当我编写scrollPanel.setPreferredSize(新维度(2,3))时;垂直滚动无法工作。我的代码在这里:

public class WelcomeUI extends JFrame {
    private JPanel contentPane;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WelcomeUI frame = new WelcomeUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public WelcomeUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(Toolkit.getDefaultToolkit().getScreenSize());
        setUndecorated(true);       
        contentPane = new JPanel();
        contentPane.setBackground(Color.LIGHT_GRAY);
        contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);        
        header = new JPanel();
        header.setForeground(Color.BLUE);
        FlowLayout fl_header = (FlowLayout) header.getLayout();
        fl_header.setAlignment(FlowLayout.RIGHT);
        fl_header.setVgap(40);
        fl_header.setHgap(0);
        header.setBackground(Color.BLUE);
        contentPane.add(header, BorderLayout.NORTH);

        footer = new JPanel();
        FlowLayout flowLayout = (FlowLayout) footer.getLayout();
        flowLayout.setVgap(10);
        footer.setBackground(Color.BLUE);
        contentPane.add(footer, BorderLayout.SOUTH);

        panel_2 = new JPanel();
        panel_2.setBackground(Color.BLUE);
        contentPane.add(panel_2, BorderLayout.WEST);

        JButton btnSelectRestaurant = new JButton("Select Restaurant");
        btnSelectRestaurant.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createDynamicButton(e);
            }
        });     
        JButton btnNewButton = new JButton("Select Steward");       
        JButton btnNewButton_1 = new JButton("Select Table");       
        JButton btnNewButton_2 = new JButton("Misc keys");
        GroupLayout gl_panel_2 = new GroupLayout(panel_2);
        gl_panel_2.setHorizontalGroup(
            gl_panel_2.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel_2.createSequentialGroup()
                    .addGap(19)
                    .addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
                        .addComponent(btnNewButton, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                        .addComponent(btnSelectRestaurant, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnNewButton_1, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                        .addComponent(btnNewButton_2, Alignment.TRAILING, 0, 0, Short.MAX_VALUE))
                    .addContainerGap())
        );
        gl_panel_2.setVerticalGroup(
            gl_panel_2.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_panel_2.createSequentialGroup()
                    .addGap(26)
                    .addComponent(btnSelectRestaurant, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(ComponentPlacement.UNRELATED)
                    .addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(438, Short.MAX_VALUE))
        );
        gl_panel_2.setAutoCreateContainerGaps(true);
        gl_panel_2.setAutoCreateGaps(true);
        panel_2.setLayout(gl_panel_2);

        panel_3 = new JPanel();
        FlowLayout flowLayout_2 = (FlowLayout) panel_3.getLayout();
        flowLayout_2.setHgap(250);
        contentPane.add(panel_3, BorderLayout.EAST);

        scrollPane = new JScrollPane();     
        contentPane.add(scrollPane, BorderLayout.CENTER);       
        scrollPanel = new JPanel();     
        scrollPanel.setPreferredSize(new Dimension(2, 3));
        scrollPane.setViewportView(scrollPanel);        
    }
    protected void createDynamicButton(ActionEvent e) {     
        JButton dynButton = new JButton("My Button");
        dynButton.setPreferredSize(new Dimension(300, 200));        
        scrollPanel.add(dynButton);         
        scrollPanel.validate();
        scrollPanel.revalidate();       
    }   
    private JPanel header;
    private JPanel footer;
    private JPanel panel_2;
    private JPanel panel_3;
    private JPanel scrollPanel;
    private JScrollPane scrollPane;
}

所以,请告诉我哪里做错了。提前感谢。

我得到了答案,实际上问题是,我已经为面板和所有动态生成的按钮设置了最大尺寸,这些按钮放置在一行中,添加到没有遇到最大尺寸,但函数无法与其他按钮通信,因此我们需要动态更改尺寸,这将通过修改来实现


可以使用修改的布局而不是FlowLayout。

聚焦于面板3,考虑以下内容:

  • 给面板一个
    GridLayout(0,3)
    ,它在三列中指定任意数量的行
  • 实现可滚动的
    界面,并让
    getPreferredScrollableViewportSize()
    返回一个
    维度,该维度是按钮高度的倍数
  • 添加每个按钮时,请为其指定一个合适的按钮
  • 添加按钮时,
    revalidate()
    打开面板并
    repaint()
    重新绘制它

  • 请尽量使你的问题更清楚concise@mKorbel:哎呀,我应该说
    revalidate()
    ,如图所示;感谢您的审阅。这个invalidate是一些没有明确原因的历史方法,啊,
    revalidate()
    调用
    invalidate()
    来支持自动布局。