Imitating a JFrame extended program with JPanel; help needed...
hi all,
there is a simple prog in the book "Sams teach ..." which extends
JFrame. its an example for actionListener.
since i heard i cant apply lookandfeel() for JFrame, i wanted to write
the programe extending JPanel.
the code i wrote correctly gets compiled. but nothing is getting
displayed ( i mean no buttons popping out).
please can you check whats the prob?
below are the two codes. (first one from the book extending JFrame and
next mine extending JPanel)
thank you for your time and support.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package DayEleven;
/**
*
* @author arshad
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class AL extends Frame implements WindowListener,ActionListener
{
TextField text = new TextField(20);
Button b;
private int numClicks = 0;
public static void main(String[] args) {
AL myWindow = new AL("My first window");
myWindow.setSize(350,100);
// myWindow.lookAndFeel();
myWindow.setVisible(true);
}
public AL(String title) {
super(title);
setLayout(new FlowLayout());
addWindowListener(this);
b = new Button("Click me");
add(b);
add(text);
b.addActionListener(this);
lookAndFeel();
}
public void lookAndFeel(){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e){
System.out.println("errr");
}
}
public void actionPerformed(ActionEvent e) {
numClicks++;
text.setText("Button Clicked " + numClicks + " times");
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
--------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;
/**
*
* @author arshad
*/
public class AL2 extends JPanel implements ActionListener{
JTextField countText=new JTextField();
JButton button=new JButton("Click to increment");
private int numClicks=0;
public AL2(){
super();
themes();
Dimension
d=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setSize(d);
JPanel pane=new JPanel(new MigLayout("Wrap 1"));
pane.add(countText);
pane.add(button);
add(pane);
button.addActionListener(this);
setVisible(true);
}
public void actionPerformed(){
numClicks++;
countText.setText("Button CLicked"+numClicks+"Times");
}
public void actionPerformed(ActionEvent arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
public static void main(String arg[]){
AL2 a=new AL2();
}
public void themes(){
try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(this);}catch (Exception e)
{
System.out.println("errror in applying the theme");
}
}
}