public class Practice extends JFrame { JPanel labelsPanel; public Practice (){ JButton appendLabelButton = new JButton ("ラベルを追加"); appendLabelButton.addActionListener (new ActionListener(){ public void actionPerformed(ActionEvent event){appendLabel();}}); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(appendLabelButton); labelsPanel = new JPanel(); labelsPanel.setLayout(new FlowLayout()); labelsPanel.setPreferredSize(new Dimension(400, 400)); add(buttonPanel, BorderLayout.NORTH); add(labelsPanel, BorderLayout.CENTER); } void appendLabel(){ JLabel helloLabel = new JLabel ("Hello"); helloLabel.setBorder(BorderFactory.createLineBorder(Color.white, 1)); helloLabel.setHorizontalAlignment(JLabel.CENTER); helloLabel.setPreferredSize(new Dimension(80,30)); labelsPanel.add(helloLabel); labelsPanel.revalidate(); } public static void main (String[]args){ SwingUtilities.invokeLater(new Runnable(){ public void run(){ Practice frame = new Practice (); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.pack();frame.setVisible(true); } }); }}