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);
}
}
} |