Results 1 to 2 of 2
- 02-13-2008, 05:56 AM #1
Member
- Join Date
- Feb 2008
- Posts
- 1
- Rep Power
- 0
Dynamically create a button, but what happened?
I am new to Swing GUI, now I have a button1, and I want to create a new button in a panel named centerPanel when I click button1, so my code like this:
the program can be launched, but when I click the button1, the new button does not be created, it's not appear in center panel. I try my best to find out the reason but failed, please give me a hand. Your answers are appreciated much.Java Code:public class TestButton extends JFrame { public static void main(String args[]) { try { TestButton frame = new TestButton(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } public TestButton() { super(); setBounds(100, 100, 500, 375); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel center = new JPanel(); getContentPane().add(center, BorderLayout.CENTER); final JPanel south = new JPanel(); getContentPane().add(south, BorderLayout.SOUTH); final JButton button = new JButton(); button.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent arg0) { [B] JButton newbutton = new JButton("Heeeeeeeee"); center.add(newbutton); center.repaint();[/B] } }); button.setText("Button1"); south.add(button); } }
- 02-17-2008, 12:01 AM #2
Similar Threads
-
How to create object dynamically with class name known in string format
By ranu_gokhe in forum Advanced JavaReplies: 1Last Post: 04-09-2008, 02:15 AM -
Create, compile and call a Java source dynamically
By Java Tip in forum Java TipReplies: 0Last Post: 02-17-2008, 08:57 AM -
How to create widgets dynamically
By sarbuland in forum Advanced JavaReplies: 0Last Post: 02-06-2008, 08:08 PM -
dynamically updating clock
By malakaherath in forum New To JavaReplies: 2Last Post: 08-01-2007, 07:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks