JButton myButton = new JButton("Button"); JCheckBox myCheckBox = new JCheckBox("Check"); JTextArea myText = new JTextArea("My text"); JPanel bottomPanel = new JPanel(); JPanel holdAll = new JPanel(); frame = new JFrame( "My Frame" ); frame.setLocation(200, 200); frame.setSize(300, 300); bottomPanel.setLayout(new FlowLayout()); bottomPanel.add(myCheckBox); bottomPanel.add(myButton); holdAll.setLayout(new BorderLayout()); holdAll.add(bottomPanel, BorderLayout.SOUTH); holdAll.add(myText, BorderLayout.CENTER); frame.getContentPane().add(holdAll, BorderLayout.CENTER); frame.setVisible(true); myButton.addActionListener(this); myCheckBox.addActionListener(this); void actionPerformed(ActionEvent e) { if (e.getSource() == myButton) myText.setText("A button click"); else if (e.getSource() == myCheckBox) myText.setText("The checkbox state changed to " + myCheckBox.isSelected()); else myText.setText("E ...?"); }
ausführen