12-23-2010, 10:36 AM
I have created a tutorial that will show you how to create a tabbed windowed application. It will show all the components that are necessary to put into the JFrame and then how to put tabs in the Pane and components within each tab.
Make note that I am using all Swing components, and that each component must be declared and given value like all data types(i.e int, char, double, String, Object)
Those are all the components that will be added for this tutorial. Adding components in a GUI for Java is like creating A blank piece of paper, then you create several sheets that lay on top of each other, similar to the overhead that teachers used in school, where they would have a clear plastic sheet they would lay on top of another sheet, so you could see the extra information they added along with the original paper.
This section of code creates a handler that we haven't defined in the code yet. This creates a handler and adds the handler to the button we created earlier, so now the button can do something when it's clicked. this is also called registering the handler.
Here we have created a small internal class known as an action listener. All Action Listeners must note implements ActionListener so that Java will know that it is listening for an action.
This just creates an instance of the TabbedPane class.
So now that were done, here is the full code.
Code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;
public class TabbedPane extends JFrame {
public TabbedPane() //constructor for the tabbed frame you will be creating
{
//This will create the title you see in the upper left of the window
setTitle("Tabbed Pane");
//Here we are creating the object
JTabbedPane jtp = new JTabbedPane();
//This creates the template on the windowed application that we will be using
getContentPane().add(jtp);
JPanel jp1 = new JPanel();//This will create the first tab
JPanel jp2 = new JPanel();//This will create the second tab
Make note that I am using all Swing components, and that each component must be declared and given value like all data types(i.e int, char, double, String, Object)
Code:
//This creates a non-editable label, sets what the label will read
//and adds the label to the first tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");/
jp1.add(label1);
//This adds the first and second tab to our tabbed pane object and names it
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
//This creates a new button called "Press" and adds it to the second tab
JButton test = new JButton("Press");
jp2.add(test);
Those are all the components that will be added for this tutorial. Adding components in a GUI for Java is like creating A blank piece of paper, then you create several sheets that lay on top of each other, similar to the overhead that teachers used in school, where they would have a clear plastic sheet they would lay on top of another sheet, so you could see the extra information they added along with the original paper.
Code:
//This is an Action Listener which reacts to clicking on
//the test button called "Press"
ButtonHandler phandler = new ButtonHandler();
test.addActionListener(phandler);
}
Code:
//This is the internal class that defines what the above Action Listener
//will do when the test button is pressed.
class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "I've been pressed", "What happened?", JOptionPane. INFORMATION_MESSAGE);
}
}
Here we have created a small internal class known as an action listener. All Action Listeners must note implements ActionListener so that Java will know that it is listening for an action.
Code:
public static void main(String[] args)
{
TabbedPane tabbedPane = new TabbedPane();
}
}
This just creates an instance of the TabbedPane class.
So now that were done, here is the full code.
Code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;
public class TabbedPane extends JFrame {
public TabbedPane() {
//This will create the title you see in the upper left of the window
setTitle("Tabbed Pane");
setSize(300,300); //set size so the user can "see" it
//Here we are creating the object
JTabbedPane jtp = new JTabbedPane();
//This creates the template on the windowed application that we will be using
getContentPane().add(jtp);
JPanel jp1 = new JPanel();//This will create the first tab
JPanel jp2 = new JPanel();//This will create the second tab
//This creates a non-editable label, sets what the label will read
//and adds the label to the first tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
//This adds the first and second tab to our tabbed pane object and names it
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
//This creates a new button called "Press" and adds it to the second tab
JButton test = new JButton("Press");
jp2.add(test);
//This is an Action Listener which reacts to clicking on
//the test button called "Press"
ButtonHandler phandler = new ButtonHandler();
test.addActionListener(phandler);
setVisible(true); //otherwise you won't "see" it
}
//This is the internal class that defines what the above Action Listener
//will do when the test button is pressed.
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "I've been pressed", "What happened?", JOptionPane. INFORMATION_MESSAGE);
}
}
//example usage
public static void main (String []args){
TabbedPane tab = new TabbedPane();
}
}