Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: James Rivera   on Jan 08 In Java Category.

  
Question Answered By: Jay Richards   on Jan 08

u need to modify just a bit in your program.
1.in stead of mPanel1.add("Center", 1); add the component u want to add like
mPanel1.add("Center",l1);
2. declare each label variable (l1,l2,l3)seperately.
3.use main() method & in that declare & initialise youe declare class.

plz see following program  again to run  it as application.

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class  MyFrame extends JFrame implements ChangeListener
{
JTabbedPane mTabbedPane;
JPanel mPanel1;
JPanel mPanel2;
JPanel mPanel3;
public MyFrame()
{
setTitle("JTabbedPanel");
JPanel contentPane = (JPanel)getContentPane();
contentPane.setLayout(new BorderLayout());
mTabbedPane = new JTabbedPane();

mPanel1 = new JPanel();
mPanel1.setLayout(new BorderLayout());
JLabel l1 = new JLabel("This is panel  1");
l1.setFont(new Font("TimesRoman", Font.BOLD, 36));
//mPanel1.add("Center", 1);
mPanel1.add("Center",l1);
mTabbedPane.addTab("Panel 1", mPanel1);

mPanel2 = new JPanel();
mPanel2.setLayout(new BorderLayout());
JLabel l2 = new JLabel("This is panel 2");
l2.setFont(new Font("TimesRoman", Font.BOLD, 36));
//mPanel2.add("Center", 1);
mPanel2.add("Center", l2);
mTabbedPane.addTab("Panel 2", mPanel2);

mPanel3 = new JPanel();
mPanel3.setLayout(new BorderLayout());
JLabel l3 = new JLabel("This is panel 3");
l3.setFont(new Font("TimesRoman", Font.BOLD, 36));
//mPanel3.add("Center", 1);
mPanel3.add("Center", l3);
mTabbedPane.addTab("Panel 3", mPanel3);

contentPane.add("Center",mTabbedPane);
mTabbedPane.setSelectedIndex(0);
mTabbedPane.addChangeListener(this);
}
public void  stateChanged(ChangeEvent e)
{
System.out.println(e);
}
//main method for declaration & initialisation of class.
public static void main(String args[])
{
MyFrame mf = new MyFrame();
mf.setVisible(true);
mf.setSize(200,200);
mf.show();
mf.pack();
}
}

Share: 

 

This Question has 10 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on JFC / Swing Or get search suggestion and latest updates.


Tagged: