Using Emacs to write Java programs in Linux, instead of using eclipse, netbeans, and other ide tools, gives me a cool feeling.
In fact, the most important thing is to know some details:
1) The format of the running program class after JDK 1.6 is as follows:
Java packagename. classname
The current path for running this command should be outside the directory with the same name as packagename
2) If two classes belong to the same package, use javac to compile a Class A first, and use the following command to compile a Class B dependent on:
Javac-D. B. Java
I used makefile to solve the second compilation problem:
Jflags =-G
JC = javac-D.
. Suffixes:. java. Class
. Java. class:
$ (JC) $ (jflags) $ *. Java
Classes =/
Mainframe. Java/
Main. Java
Default: Classes
Classes: $ (classes:. Java =. Class)
Clean:
$ (RM) *. Class
You can refer to the following article to view the makefile syntax I use:
Http://www.cs.swarthmore.edu /~ Newhall/unixhelp/javamakefiles.html
The make command is executed in the following order:
1) Default: Classes
2) classes: $ (classes:. Java =. Class) // you can change the suffix to. Class.
3) generate mainframe. Class. Of course, it will cause compilation of mainframe. Java
4) generate main. Class, which of course will cause compilation of Main. Java
The result is that a sub-directory freebird (with the same name as package) is created, and the. class files of both classes are generated in this directory.
Then I can use Java freebird. Main to execute the program. (Remember to execute the Command outside the subdirectory freebird)
3) Java swing programming skills
Place the form window in the center of the screen:
First, you need to obtain the screen size by calling toolkit. getdefatooltoolkit () is used to obtain the reference of a toolkit object. The getscreensize method provided by this object can be used to reference a dimension object. The width and height attributes of this object are the width and height of the screen. The following formula allows you to place a window of the specified size in the center of the screen.
X = (screenwidth-formwidth)/2; y = (screenheight-formheight)/2
Multi-layer structure of jframe:
The jframe consists of the root panel, hierarchy panel, glass panel, and content panel from the bottom up. Normally, all our controls are added to the content panel. Even if you call the jframe. Add method directly, the JDK implementation code still adds the control to the content panel.
We can use the jframe. getcontentpane method to obtain the content panel.
The following code creates a read-only text control and adds it to the content panel.
Pingjianame = new jtextfield ("hello", 20 );
Add (pingjianame );
Pingjianame. seteditable (false );
Layout manager:
Here I only use flowlayout, borderlayout, and panel.
Flowlayout is similar to the flow layout of HTML. The control is always automatically arranged from top to bottom, from left to right. When a row does not have enough space, the control is automatically arranged in the next row. We can also set the horizontal and vertical distance between controls and the alignment (center, left or right ).
Borderlayout divides the window into five areas: east, west, south, north, and middle. You can specify the control in one of the regions. A Panel can be viewed as a control with container functions. It can contain other controls or be placed in other containers as a control.
The following example illustrates the effect of the three components. (Note: flowlayout is the default layout)
Package freebird;
Import java. AWT. frame;
Import java. AWT. dimension;
Import java. AWT. toolkit;
Import java. AWT. borderlayout;
Import java. AWT. event .*;
Import javax. Swing .*;
Public class mainframe extends jframe
{
Private jtextfield pingjianame;
Private jpanel;
Public mainframe ()
{
Super ("try to learn Japanese and make progress every day ");
Setsize (1024,768 );
Dimension screensize = toolkit. getdefatooltoolkit (). getscreensize ();
Setlocation (screensize. Width-1024)/2, (screensize. Height-768)/2 );
Addwindowlistener (New windowadapter ()
{
Public void windowclosing (windowevent E)
{
System. Exit (0 );
}
}
);
Initcomponents ();
}
Private void initcomponents ()
{
Panel = new jpanel ();
// Panel. setlayout (New borderlayout ());
Pingjianame = new jtextfield ("hello", 20 );
Pingjianame. seteditable (false );
Panel. Add (pingjianame );
Add (panel, borderlayout. North );
Setdefaclocloseoperation (javax. Swing. windowconstants. exit_on_close );
}
}