Help in positioning and running to the web
i really have a problem in setting a label, button, or textfield or etc in applets. how can i position in it in the coordinates i want? it's pretty embarrassing since everytime i add a label or a button, it will directly attach it in the center of the applet. setBounds or setLocation doesn't work. it's not like JFrame.
here's my code. pls help tnx. I'm making sa simple paint applet
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
import javax.swing.*; //for the maine JFrame design
import java.awt.*; //for the GUI stuff
import java.awt.event.*;//for the event handling
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.*;//for reading the file
public class Notepad extends JFrame implements ActionListener{
private TextArea textArea;
private MenuBar menuBar;
private Menu file;
private MenuItem openFile;
private MenuItem saveFile;
private MenuItem closeFile;
public Notepad(){
super( "Java Notepad Practice" );
this.setSize( 500, 300 );
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.getContentPane().setLayout( new BorderLayout() );//// the BorderLayout bit makes it fill it automatically
this.textArea = new TextArea( "", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY );
this.textArea.setFont( new Font( "Century Gothic", Font.BOLD, 12 ) );
this.getContentPane().add( this.textArea, BorderLayout.CENTER );
this.menuBar = new MenuBar();
this.setMenuBar( this.menuBar ); //add out menu bar into the GUI
this.file = new Menu( "File");
this.menuBar.add( this.file );//add a menu in the menu bar
this.openFile = new MenuItem( "Open File" );
this.file.add( this.openFile );
this.openFile.setShortcut( new MenuShortcut( KeyEvent.VK_O, false ) );
this.file.addActionListener( this );
this.saveFile = new MenuItem( "Save File" );
this.file.add( this.saveFile );
this.saveFile.setShortcut( new MenuShortcut( KeyEvent.VK_S, false ) );
this.closeFile = new MenuItem( "Close File" );
this.file.add( this.closeFile );
this.closeFile.setShortcut( new MenuShortcut( KeyEvent.VK_C, false ) );
this.closeFile.addActionListener( this );
}
public void actionPerformed( ActionEvent e ){
if( e.getSource() == this.closeFile )
this.dispose();
else if( e.getSource() == this.openFile){
JFileChooser open = new JFileChooser(); //open up a file chooser(a dialog for the user to browse files to open)
int option = open.showOpenDialog( this ); // get the option that the user selected(approve or cancel)
//// NOTE: because we are OPENing a file, we call showOpenDialog~
// if the user clicked OK, we have "APPROVE_OPTION"
// so we want to open the file
if( option == JFileChooser.APPROVE_OPTION ){
this.textArea.setText( "" ); //clear the textarea before applying the file context
try{
// create a scanner to read the file
//(getSelectedFile().getPath() will get the path to the file)
Scanner scan = new Scanner( new FileReader( open.getSelectedFile().getPath()) );
while( scan.hasNext() ) this.textArea.append( scan.nextLine() + "\n");
}catch( Exception ex ){
System.out.println( ex.getMessage() );
}
}
}
else if( e.getSource() == this.saveFile ){
JFileChooser save = new JFileChooser(); //again open a file chooser
int option = save.showSaveDialog( this );
if( option == JFileChooser.APPROVE_OPTION ){
try{
//create a buffered writer to write to a file
BufferedWriter out = new BufferedWriter( new FileWriter( save.getSelectedFile().getPath() ) );
out.write( this.textArea.getText() );//write the contents od the textArea to the file
out.close();
}catch( Exception ex ){
System.out.println( ex.getMessage() );
}
}
}
}
public static void main( String args [] ){
new Notepad().setVisible( true );
}
}
OT: why does it won't run on web. here is the ss
http://img145.imageshack.us/img145/6767/appleterror.png
Uploaded with ImageShack.us
Re: Help in positioning and running to the web
> how can i position in it in the coordinates i want?
Don't even try. Lear how to use layouts correctly: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
> why does it won't run on web. here is the ss
Click 'Details' for more information.
Also, programs run on a computer -- not 'on web'
Additionally, Write in clear, grammatical, correctly-spelled language.
db
Re: Help in positioning and running to the web
how do you even get it to run in a web browser
Re: Help in positioning and running to the web
Don't post to old dead threads, don't post an unrelated question in an ongoing thread and don't hijack another poster's thread.
Threads are free; start a new thread and ask your question clearly, in a way that it might actually be answerable.
db
THREAD CLOSED