动态地向JScrollPane添加组件

时间:2012-06-18 13:35:35

标签: java swing awt

我是Java Swing的新手我想开发一个类似于这个图像的POS应用程序: enter image description here

为了开发这个应用程序,我正在使用Eclipse,我已经创建了JPanel,它由数字显示,例如图像中的(1,2)。在3号我已经添加了包含JPanel的JscrollPan,现在我想在这里做点击当我从2号面板上点击一个按钮时应该动态添加3号面板中的新按钮,那时我想在每行只显示三个按钮并且只在需要时才能垂直激活滚动。但是我无法做到这一点,因为当我编写scrollPanel.setPreferredSize(new Dimension(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;
}

所以,请告诉我我做错了什么。提前谢谢。

2 个答案:

答案 0 :(得分:2)

关注第3小组,请考虑以下因素:

  1. 为面板指定GridLayout(0, 3),指定三列中的任意数字行。
  2. 实施Scrollable界面,让getPreferredScrollableViewportSize()返回一个Dimension,它是按钮高度的倍数。
  3. 添加每个按钮后,请为其指定合适的Action
  4. 添加按钮时,revalidate()面板和repaint()它。

答案 1 :(得分:1)

我得到了答案,实际问题是,我已经为面板设置了最大尺寸,并且所有动态生成的按钮都放置在单行,添加到最大尺寸没有遇到,但是功能无法与其他人通信,因此我们需要更改通过修改FlowLayout interface来动态调整大小。

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