Type in:. All the rest of the code from this tutorial goes between the two curly brackets. The TopLevelWindow class is like the covers of a book; it shows the compiler where to look for the main application code.
It's good programming style to group sets of similar commands into functions. This design makes the program more readable, and if you want to run the same set of instructions again, all you have to do is run the function. With this in mind, I'm grouping all the Java code that deals with creating the window into one function. Enter the createWindow function definition:.
Anytime the createWindow function is called, the Java application will create and display a window using this code. Now, let's look at creating the window using a JFrame object. Type in the following code, remembering to place it between the curly brackets of the createWindow function:. What this line does is create a new instance of a JFrame object called "frame". You can think of "frame" as the window for our Java application.
The JFrame class will do most of the work of creating the window for us. It handles the complex task of telling the computer how to draw the window to the screen, and leaves us the fun part of deciding how it's going to look. We can do this by setting its attributes, such as its general appearance, its size, what it contains, and more. For starters, let's make sure that when the window is closed, the application also stops. The JFrame.
Since an empty window has little use, let's now put a graphical component inside it. Add the following lines of code to the createWindow function to create a new JLabel object. A JLabel is a graphical component that can contain an image or text. Now that we have created the JLabel, add it to the JFrame:. The last lines of code for this function are concerned with how the window is displayed. Add the following to ensure that the window appears in the center of the screen:.
Next, set the window's size:. The pack method looks at what the JFrame contains, and automatically sets the size of the window. Ferenc Deak Ferenc Deak The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Visit chat. Linked Related Hot Network Questions.
Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. Asked 9 years, 1 month ago. Active 4 years, 3 months ago.
Viewed 8k times. Help would be appreciated, a lot. Using Netbeans 7. Improve this question. BenMorel Burbanana Burbanana 49 1 1 gold badge 3 3 silver badges 12 12 bronze badges. And JDK 1. If you did have a prior 7 version, it would be best to delete them on Linux a.
Add a comment. Active Oldest Votes. Improve this answer. Jasmeet Singh Jasmeet Singh 7 7 bronze badges. I had already done that, didn't work.. Then I was able to create Java Project. TechnoCrat TechnoCrat 10 10 silver badges 20 20 bronze badges.
0コメント