Java : Small GUI application Frame,Panel and Button Click (Ver 2.0)
// Identifies button name though the action performed
// Use JDialog instead of JFrame for thread to pause until an event
package start;
import java.awt.*;//ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
GUI Test1=new GUI();
JFrame oFrame=Test1.Create_Panel();
}
}
//http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
class GUI extends JFrame implements ActionListener {
JPanel Panel;
JFrame Frame;
JButton Button;
JLabel Label;
public JFrame Create_Panel() // Return type = JFrame
{
this.Frame=new JFrame("FrameDemo"); //Frame
this.Panel=new JPanel(); //Panel
this.Button=new JButton("Hi"); //Button
Button.addActionListener(this);
Button.setSize(20, 30);
Panel.setSize(20, 30);
Frame.setSize(100, 100);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Optional: What happens when the frame closes?
Panel.add(Button); //Panel<-Button
Frame.add(Panel); //Frame<-Panel
//Frame<-Panel<-Button
Frame.setVisible(true); //Show it.
return Frame;
}
public void actionPerformed(ActionEvent e)
{
String buttonText = ((JButton) e.getSource()).getText();
System.out.print(buttonText);
JOptionPane.showMessageDialog(null, "Button Pressed2");
}
}
import java.awt.*;//ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
GUI Test1=new GUI();
JFrame oFrame=Test1.Create_Panel();
}
}
//http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
class GUI extends JFrame implements ActionListener {
JPanel Panel;
JFrame Frame;
JButton Button;
JLabel Label;
public JFrame Create_Panel() // Return type = JFrame
{
this.Frame=new JFrame("FrameDemo"); //Frame
this.Panel=new JPanel(); //Panel
this.Button=new JButton("Hi"); //Button
Button.addActionListener(this);
Button.setSize(20, 30);
Panel.setSize(20, 30);
Frame.setSize(100, 100);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Optional: What happens when the frame closes?
Panel.add(Button); //Panel<-Button
Frame.add(Panel); //Frame<-Panel
//Frame<-Panel<-Button
Frame.setVisible(true); //Show it.
return Frame;
}
public void actionPerformed(ActionEvent e)
{
String buttonText = ((JButton) e.getSource()).getText();
System.out.print(buttonText);
JOptionPane.showMessageDialog(null, "Button Pressed2");
}
}
No comments:
Post a Comment