Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JFC / Swing

  Asked By: James    Date: Jan 08    Category: Java    Views: 1641
  

Can any one help me to compile the program I tried using jdk but nothing
happen. Please also tell me how to run the program thanks

Write a Swing program to create the Tabbed Panels.





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 l = new JLabel("This is panel 1");
l.setFont(new Font("TimesRoman", Font.BOLD, 36));
mPanel1.add("Center", 1);
mTabbedPane.addTab("Panel 1", mPanel1);

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

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

contentPane.add("Center",mTabbedPane);
mTabbedPane.setSelectedIndex(0);
mTabbedPane.addChangeListener(this);
}
public void stateChanged(ChangeEvent e)
{
System.out.println(e);
}
}

Share: 

 

11 Answers Found

 
Answer #1    Answered By: Frederick Greene     Answered On: Jan 08

To compile  :
javac MyFrame.java

To run:
java MyFrame

Few things must be fixed in order to make it work.

1. add(String, Component) method is obsolete.
change it to add(Component)
2. add main method:

public static void  main(String[] args) {
MyFrame f = new MyFrame();
f.setDefaultCloseOperation(JFRAME.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}

 
Answer #2    Answered By: Jay Richards     Answered 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();
}
}

 
Answer #3    Answered By: Wade Jordan     Answered On: Jan 08

try setvisible property on the frame

 
Answer #4    Answered By: Roderick King     Answered On: Jan 08

make sure you call pack() in the Frame class  befre setVisible( true ).
pack will arrange all the components in the frame according to the
layout manager. if you call pack after setVisVisible( true ) you will
see all the components get shuffled arround when you call pack().

 
Answer #5    Answered By: Fatih Farooq     Answered On: Jan 08

Thanks for your reply and I tried its working fine .

Can u help  me to do this program  for me please thanks

Write a Swing program to create  the Tabbed Panels

 
Answer #6    Answered By: Isaac Evans     Answered On: Jan 08

how can i help  u? tell me. which swing  code u want?
Write a Swing program  to create  the Tabbed Panels
i think we just discussed that code, rite.... its working fine. (i didnt
tested , just corrected it logically but u r saying,it worked then thats
good .......& it should work, i see nothing goof's in that.)
mail me for any query.

 
Answer #7    Answered By: Erin Dunn     Answered On: Jan 08

the following program  working well but I have another program to do, I am
trying these few days ,can u help  me to do this program thanks for your help

Write a swing  program to create  the Tabbed Panels.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
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 jLbl1 = new JLabel("This is panel  1");
jLbl1.setFont(new Font("TimesRoman", Font.BOLD, 36));
mPanel1.add("Center", jLbl1);
mTabbedPane.addTab("Panel 1", mPanel1);

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

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

contentPane.add("Center",mTabbedPane);
mTabbedPane.setSelectedIndex(0);
mTabbedPane.addChangeListener(this);
}

public void  stateChanged(ChangeEvent e) {
System.out.println(e);
}

public static void main(String args[]) {
MyFrame mf = new MyFrame();
mf.setVisible(true);
mf.setSize(200,200);
mf.show();
mf.pack();

mf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

Write a Swing program to create Buttons with

a) Tool tip text b) Image c) Border d) Short cut Key

 
Answer #8    Answered By: Ana Bradley     Answered On: Jan 08

Write a Swing program  to create  Buttons with

a) Tool tip text b) Image c) Border d) Short cut Key



Please help  me to do this program for me urgently please I am trying too

 
Answer #9    Answered By: Bernard Gutierrez     Answered On: Jan 08

Write a swing  program to create  Buttons with

a) Tool tip text b) Image c) Border d) Short cut Key



Please help  me to do this program  for me urgently please I am trying too
Compile I have error please correct me


import javax.swing.JToolBar;

import javax.swing.JButton;

import javax.swing.ImageIcon;



import javax.swing.JTextField;



import javax.swing.JFrame;

import javax.swing.JTextArea;

import javax.swing.JScrollPane;

import javax.swing.JPanel;



import java.awt.*;

import java.awt.event.*;



public class  ToolBarDemo2 extends JFrame {

protected JTextArea textArea;

protected String newline = "\n";



public ToolBarDemo2() {

//Do frame stuff.

super("ToolBarDemo2");

addWindowListener(new WindowAdapter() {

public void  windowClosing(WindowEvent e) {

System.exit(0);

}

});



//Create the toolbar.

JToolBar toolBar = new JToolBar();

toolBar.setFloatable(false);

addButtons(toolBar);



//Create the text area used for output.

textArea = new JTextArea(5, 30);

JScrollPane scrollPane = new JScrollPane(textArea);



//Lay out the content pane.

JPanel contentPane = new JPanel();

contentPane.setLayout(new BorderLayout());

contentPane.setPreferredSize(new Dimension(400, 100));

contentPane.add(toolBar, BorderLayout.NORTH);

contentPane.add(scrollPane, BorderLayout.CENTER);

//contentPane.add("cover.jpg");

setContentPane(contentPane);

}



protected void addButtons(JToolBar toolBar) {

JButton button = null;



//first button

button = new JButton(new ImageIcon("images/left.gif"));

button.setToolTipText("This is the left button");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

displayResult("Action for first button");

}

});

toolBar.add(button);



//second button

button = new JButton(new ImageIcon("images/middle.gif"));

button.setToolTipText("This is the middle button");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

displayResult("Action for second button");

}

});

toolBar.add(button);



//third button

button = new JButton(new ImageIcon("images/right.gif"));

button.setToolTipText("This is the right button");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

displayResult("Action for third button");

}

});

toolBar.add(button);



//separator

toolBar.addSeparator();



//fourth button

button = new JButton("Another button");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

displayResult("Action for fourth button");

}

});

toolBar.add(button);



//fifth component is NOT a button!

JTextField textField = new JTextField("A text field");

//Action handler implementation would go here.

toolBar.add(textField);

}



protected void displayResult(String actionDescription) {

textArea.append(actionDescription + newline);

}



public static void main(String[] args) {

ToolBarDemo2 frame = new ToolBarDemo2();

frame.pack();

frame.setVisible(true);

}

}

 
Answer #10    Answered By: Vilhelm Fischer     Answered On: Jan 08

Why not import  javax.swing.* in place of import javax.swing.JButon
(...JToolBar...and all the other...)?

 
Answer #11    Answered By: Kyle Fox     Answered On: Jan 08

My program  is working but don't how to create  short cut key please try to
help me

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




Tagged: