Add Object to JPanel after button click

2019-03-01 08:10发布

I have created a JScrollPane with a JPanel inside it and I want to add JPanel/JLabel/Other objects after pressing the button. For example after three button presses I want to get something like this: desired result

I tried myJPane.add(testLabel) with testlabel.setBounds()but no result, I don't want to use GridLayout because of the unchangeable sizes. I would like it if the added objects had different sizes - adjusted to the text content.

What should I use for it and how?

Thanks in advance. Best regards, Tom.

3条回答
等我变得足够好
2楼-- · 2019-03-01 08:34

I will use a BoxLayout, creating a vertical box, and after each button action, it will add a new JPanel to this box.

Example:

public class YourChat extends JPanel{
    private JScrollPane sc;
    private Box bv;

    public YourChat(){
        bv = Box.createVerticalBox();
        sc = new JScrollPane(bv);
        //your functions (panel creation, addition of listeners, etc)

        add(sc);
    }

    //panel customized to have red backgroud
    private class MyPanel extends JPanel(){
        private JLabel label=new JLabel();
        public MyPanel(String text){
            setBackgroundColor(Color.red);
            add(label);
        }
    }


    //inside the action listener
    public void actionPerformed(ActionEvent e) {
            sc.add(new MyPanel(textField.getText()));
            textField.setText("");
    }
}

For extra information check on: [https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html]

See also the example [http://www.java2s.com/Code/Java/Swing-JFC/VerticalandhorizontalBoxLayouts.htm]

查看更多
混吃等死
3楼-- · 2019-03-01 08:48

Here is a JPanel inside a JScrollPane that adds JLabels to it when pressing the button:

public class Example extends JFrame {

    public Example() {

        JPanel boxPanel = new JPanel();
        boxPanel.setLayout(new BoxLayout(boxPanel, BoxLayout.PAGE_AXIS));

        JTextField textField = new JTextField(20);      
        JButton sendButton = new JButton("Send");
        sendButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                JLabel label = new JLabel(textField.getText());
                label.setOpaque(true);
                label.setBackground(Color.RED);
                boxPanel.add(label);
                boxPanel.add(Box.createRigidArea(new Dimension(0,5)));
                textField.setText("");
                boxPanel.revalidate();
//              pack();
            }
        });


        JPanel southPanel = new JPanel();
        southPanel.add(textField);
        southPanel.add(sendButton);

        add(new JScrollPane(boxPanel));
        add(southPanel, BorderLayout.PAGE_END);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {

        new Example();
    }
}

The BoxLayout will stack the labels on top of each other.

Notes:

  • setOpaque(true) must be called on label for it to honor the background color.
  • Box.createRigidArea is used for creating gaps. Use it as you wish.
  • The call to revalidate() is imperative in order to display the new components immediately.
  • Calling pack() (on the JFrame) will resize it each time to fit all the new components. I just put it there for demonstration since the initial frame size is too small to display the initial components added.
查看更多
女痞
4楼-- · 2019-03-01 08:51

Use BoxLayout if you want only add vertically, otherwise you can use FlowLayout for both directions.

查看更多
登录 后发表回答