• Tidak ada hasil yang ditemukan

Java 9 introduced a utility call jshell, which allows you to try out bits of Java code and see the results immediately. jshell is a REPL—a Read Evaluate Print Loop. Many lan‐

guages have them, and prior to Java 9 there were many third-party variations avail‐

able, but nothing built into the JDK itself. We saw a hint of what jshell can do in the previous chapter; let’s look a little more carefully at its capabilities.

You can use a terminal or command window from your operating system, or you can open a terminal tab in IntelliJ IDEA, as shown in Figure 3-2. Just type jshell at your command prompt and you’ll see a bit of version information along with a quick reminder about how to view help from within the REPL.

Figure 3-2. Starting jshell inside IDEA

Let’s go ahead and try that help command now:

| Welcome to JShell -- Version 12

| For an introduction type: /help intro jshell> /help intro

|

| intro

| =====

|

| The jshell tool allows you to execute Java code, getting immediate results.

| You can enter a Java definition (variable, method, class, etc),

| like: int x = 8

| or a Java expression, like: x + x

| or a Java statement or import.

| These little chunks of Java code are called 'snippets'.

|

| There are also the jshell tool commands that allow you to understand and

jshell is quite powerful, and we won’t be using all of its features in this book. However, we will certainly be using it to try Java code and make quick tweaks here and throughout most of the remaining chapters. Think back to our HelloJava2 example,

“HelloJava2: The Sequel” on page 53. We can create UI elements like that JFrame right in the REPL and then manipulate them—all while getting immediate feedback! No need to save, compile, run, edit, save, compile, run, etc. Let’s try:

jshell> JFrame frame = new JFrame( "HelloJava2" )

| Error:

| cannot find symbol

| symbol: class JFrame

| JFrame frame = new JFrame( "HelloJava2" );

| ^----^

| Error:

| cannot find symbol

| symbol: class JFrame

| JFrame frame = new JFrame( "HelloJava2" );

| ^----^

Oops! jshell is smart and feature rich, but it is also quite literal. Remember that if you want to use a class not included in the default package, you have to import it. That’s true in Java source files, and it’s true when using jshell. Let’s try again:

jshell> import javax.swing.*

jshell> JFrame frame = new JFrame( "HelloJava2" )

frame ==> javax.swing.JFrame[frame0,0,23,0x0,invalid,hidden ... led=true]

That’s better. A little strange, probably, but better. Our frame object has been created.

That extra information after the ==> arrow is just the details about our JFrame, such as its size (0x0) and position on-screen (0,23). Other types of objects will show other details. Let’s give our frame some width and height like we did before and get our frame on the screen where we can see it:

jshell> frame.setSize(300,200) jshell> frame.setLocation(400,400) jshell> frame.setVisible(true)

You should see a window pop up right before your very eyes! It will be resplendent in modern finery, as shown in Figure 3-3.

Figure 3-3. Showing a JFrame from jshell

By the way, don’t worry about making mistakes in the REPL. You’ll see an error mes‐

sage, but you can just correct whatever was wrong and keep going. As a quick exam‐

ple, imagine making a typo when trying to change the size of the frame:

jshell> frame.setsize(300,300)

| Error:

| cannot find symbol

| symbol: method setsize(int,int)

| frame.setsize(300,300)

| ^---^

Java is case-sensitive so setSize() is not the same as setsize(). jshell gives you the same kind of error information that the Java compiler would, but presents it inline.

Correct that mistake and watch the frame get a little bigger (Figure 3-4)!

Amazing! Well, alright, perhaps it is less than useful, but we’re just starting. Let’s add some text using the JLabel class:

jshell> JLabel label = new JLabel("Hi jshell!") label ==>

javax.swing.JLabel[, 0,0,0x0, ...

rticalTextPosition=CENTER]

jshell> frame.add(label)

$8 ==>

javax.swing.JLabel[,0,0,0x0, ...

Figure 3-4. Changing the size of our frame

Neat, but why didn’t our label show up in the frame? We’ll go into much more detail on this in the chapter on user interfaces, but Java allows some graphical changes to build up before realizing them on your screen. This can be an immensely efficient trick but it can sometimes catch you off guard. Let’s force the frame to redraw itself (Figure 3-5):

jshell> frame.revalidate() jshell> frame.repaint()

Figure 3-5. Adding a JLabel to our frame

Now we can see our label. Some actions will automatically trigger a call to revali date() or repaint(). Any component already added to our frame before we make it visible, for example, would appear right away when we do show the frame. Or we can remove the label similarly to how we added it. Watch again to see what happens when we change the size of the frame immediately after removing our label (Figure 3-6):

jshell> frame.remove(label) // as with add(), things don't change immediately jshell> frame.setSize(400,150)

Figure 3-6. Removing a label and resizing our frame

See? We have a new, slimmer window and no label—all without forced repainting.

We’ll do more work with UI elements in later chapters, but let’s try one more tweak to our label just to show you how easy it is to try out new ideas or methods you looked up in the documentation. We can center the label’s text, for example, resulting in something like Figure 3-7:

jshell> frame.add(label)

$45 ==>

javax.swing.JLabel[,0,0,300x278,..., text=Hi jshell!,...]

jshell> frame.revalidate() jshell> frame.repaint()

jshell> label.setHorizontalAlignment(JLabel.CENTER)

Figure 3-7. Centering the text on our label

We know this was another whirlwind tour with several bits of code that might not make sense yet, like why is CENTER in all caps? Or why is the class name JLabel used before our center alignment? Hopefully, typing along, probably making a few small mistakes, correcting them, and seeing the results makes you want to know more. We just want to make sure you have the tools needed to continue playing along as you go throughout the rest of this book. Like so many other skills, programming benefits from doing in addition to reading!

Dokumen terkait