Click to See Complete Forum and Search --> : Right Track


args
June 16th, 2001, 03:28 PM
Hi All
I am trying to replicate a program in swing that i made in AWT. What i would like to know am i on the right track the reason i ask is i want to be able to open other frame's from the buttons on the main frame and also be able to put a menu on





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



class TroisTestFrame extends JFrame{
public TroisTestFrame(){
setTitle("An application using Swing");
setBounds(90,30,660,520);
addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);
}


});

Container contentPane = getContentPane();
contentPane.add(new TroisPanel());

}
}
public class Trois
{ public static void main(String args [] )
{ JFrame frame = new TroisTestFrame();

frame.show();
}
}

class TroisPanel extends JPanel implements ActionListener{
public void paintComponent(Graphics g){
super.paintComponent(g);

Font p = new Font ("Ariel",Font.BOLD, 24);
g.setFont(p);
g.drawString("My First Swing Application",170,50);

String msg = new String(" VERSION 2. ");

setBackground(Color.green);

JButton e = new JButton("English");
e.setBounds(150,140,100,30);
add(e);
e.addActionListener(this);

JButton ger = new JButton("German");
ger.setBounds(150,210,100,30);
add(ger);


JButton fre = new JButton("French");
fre.setBounds(150,280,100,30);
add(fre);

Toolkit kit = Toolkit.getDefaultToolkit();

Image image3 = kit.getImage("de-t.gif");
g.drawImage(image3,50,137, this);

Image image = kit.getImage("uk.gif");
g.drawImage(image,50,206, this);

Image image2 = kit.getImage("fr-t.gif");
g.drawImage(image2,50,276, this);


}
public void actionPerformed (ActionEvent e) {

// To test Button//
if (e.getActionCommand().equals("English")){
java.awt.FileDialog fd = new java.awt.FileDialog(new java.awt.Frame());
fd.show();

}

}

}

dlorde
June 16th, 2001, 04:36 PM
It looks as if you're creating display components inside the paintComponent() method of TroisPanel. This method gets called everytime the panel needs to be painted, so you're creating these components over and over. Better to create them once outside the paintComponent() method.

Dave

To email me remove '_spamjam' from my email address