Results 1 to 1 of 1
Thread: New button in new frame
- 03-01-2009, 10:32 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
New button in new frame
my program is compiling and running but when i click on my button named "New frame" i need the new frame to open with the third button on the interface, but at the moment it is opening in the original frame, any help in getting the 'Third' button to the new frame would be greatly appreciated.
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GuiDemo4 extends JFrame { private JButton myFirstButton; private JButton button3; private JButton myFourthbutton; public GuiDemo4() { super("My First Button Program"); myFirstButton = new JButton("FIRST"); myFirstButton.setFont(new Font("Arial", Font.BOLD, 18)); myFirstButton.setBackground(Color.red); button3 = new JButton("Original Frame"); button3.setFont(new Font ("Arial", Font.BOLD, 18)); button3.setBackground(Color.green); Container c = getContentPane(); FlowLayout fl = new FlowLayout(FlowLayout.LEFT); c.setLayout(fl); c.add(myFirstButton); c.add(button3); ButtonHandler handler = new ButtonHandler(); myFirstButton.addActionListener(handler); button3.addActionListener(handler); setSize(400, 300); setVisible(true); } public static void main(String[] args) { GuiDemo4 f = new GuiDemo4(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("Exit via windowClosing."); System.exit(0); } }); } private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == myFirstButton) { System.out.println("Left Button has been pressed."); } if (e.getSource() == button3) { JFrame frame = new JFrame("New Frame"); pack(); frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); Container c = getContentPane(); FlowLayout f1 = new FlowLayout(FlowLayout.LEFT); c.setLayout(f1); myFourthbutton = new JButton ("Third"); myFourthbutton.setFont(myFirstButton.getFont()); myFourthbutton.setBackground(new Color(180, 100, 255)); c.add(myFourthbutton); } ButtonHandler handler = new ButtonHandler(); myFourthbutton.addActionListener(handler); setSize(400, 300); setVisible(true); } } }
Similar Threads
-
GUI new frame
By billbo123 in forum New To JavaReplies: 15Last Post: 03-02-2009, 04:24 AM -
frame
By arunkumarinfo in forum NetBeansReplies: 0Last Post: 02-07-2009, 10:26 AM -
how disable the display of close button on the frame
By kalanidhi in forum New To JavaReplies: 6Last Post: 11-19-2008, 09:51 AM -
Frame to other Frame
By Aswq in forum New To JavaReplies: 2Last Post: 07-19-2008, 04:27 PM -
Frame Query
By Daniel in forum AWT / SwingReplies: 1Last Post: 07-05-2007, 06:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks