JAVA

Java GUI: AWT & Swing

mossybeach 2024. 7. 10. 17:46

There are two types of interfaces to use when using Java: CLI and GUI.

  • CLI (Command Line Interface) : this is where we use the cmd panel to execute code. Simple and lightweight but visually minimal and could be limiting.
  • GUI: (Graphic User Interface): Heavier than CLI but visually easier for users to navigate and use

Today we focused on GUI!!


three main outlets for GUI:

  • AWT: Follow the Operating system's characteristics when creating the visual interface = therefore each operating system differs (replaced by AWT)
  • Swing: Applies the look and feel of Java uniformly across all operating systems.
    interestingly uses "j" infront of code:
JButton jBtn = new JButton();
  • Java Fx: uses Rich Internet Application which is a graphic and media package to test, debug and deploy 
    Since it is based on Java, it can be used anywhere once created.

Fun terminology for Java:

Container:

  • Works as a window for Java.
  • If there is more than one, they stack on top of each other
  • Containers are smaller than Components
  • examples: frame, window, panel, dialog. applet etc.

Component:

  • parts that are actually stacked on top of containers to create a window
  • examples: BoarderLayout, GridLayout

LayoutManager: Rules and ways to adjust and assign space for components on top of a container.

FlowLayout shape: placed from left to right (default)


 

Example: 

public class Notepad extends JFrame {

 

public Notepad() {

super("메모장 만들기"); //bring all instances of parent: JfFrame

JTextArea jTextArea = new JTextArea("내용을 입력하세요"); //create new text area

JScrollPane jScrollPane = new JScrollPane(jTextArea); //create a scrollpane and insert textarea 

 

JMenu Menu = new JMenu("파일"); //create menu (think ul in css)

JMenu Menu2 = new JMenu("편집");

JMenu Menu3 = new JMenu("보기");

 

JMenuBar jMenuBar = new JMenuBar(); //think nav in css

 

Menu.add(new JMenuItem("새 탭")); //create menuitem (think li)

 

//used setAccelerator method: lets users use assigned hotkeys without direct use of actionlisteners ( think eventlisteners in js) and applied hotkeys for each menu

Menu.getItem(0).setAccelerator(KeyStroke.getKeyStroke('N' , InputEvent.CTRL_DOWN_MASK))

Menu.add(new JMenuItem("새 창"));

Menu.getItem(0).setAccelerator(KeyStroke.getKeyStroke('T' , InputEvent.CTRL_DOWN_MASK));

Menu.add(new JMenuItem("열기"));

Menu.getItem(0).setAccelerator(KeyStroke.getKeyStroke('O' , InputEvent.CTRL_DOWN_MASK));

 

//used actionlistener method to assign an event when clicked

Menu.getItem(2).addActionListener(new ActionListener() {

 

@Override

public void actionPerformed(ActionEvent e) {

//created a fileChooser object to summon when an event happens 

JFileChooser fileChooser = new JFileChooser();

//use showOpenDialog method which Pops up an "Open File" file chooser dialog

fileChooser.showOpenDialog(null);

}

});

 

Menu.add(new JMenuItem("저장"));

Menu.getItem(0).setAccelerator(KeyStroke.getKeyStroke('S' , InputEvent.CTRL_DOWN_MASK));

 

Menu.addSeparator(); //adds line divider

 

//attaching menuItems to menus

Menu.add(new JMenuItem("인쇄"))

Menu.add(new JMenuItem("닫기"));

Menu2.add(new JMenuItem("실행 취소"));

Menu2.add(new JMenuItem("복사"));

Menu2.add(new JMenuItem("잘라내기"));

Menu2.add(new JMenuItem("붙여넣기"));

Menu2.addSeparator();

Menu2.add(new JMenuItem("찾기"));

Menu2.add(new JMenuItem("모두 선택"));

Menu2.add(new JMenuItem("글꼴"));

Menu3.add(new JMenuItem("확대/축소"));

Menu3.add(new JMenuItem("상태 표시줄"));

Menu3.add(new JMenuItem("자동 줄바꿈"));

Menu3.add(new JMenuItem("도움말"));

Menu3.getItem(3).addActionListener(new ActionListener() {

//created a new action listener to create a pop up

@Override

public void actionPerformed(ActionEvent e) {

//showMessageDialog = alert() in JS

JOptionPane.showMessageDialog(null,"도움말을 클릭했습니다.","도움말" , JOptionPane.INFORMATION_MESSAGE);

}

});

 

 

jMenuBar.add(Menu);

jMenuBar.add(Menu2);

jMenuBar.add(Menu3);

 

//aded Menubar to the "NORTH" using BorderLayout()

this.add(jMenuBar, BorderLayout.NORTH);

 

this.add(jScrollPane, BorderLayout.CENTER);

this.setSize(600, 600); //set size ,width and height

this.setVisible(true); //set visiblitiy of window

}

 

public static void main(String[] args) {

//create notepad object so the window can load

Notepad notepad = new Notepad();

}

 

}

 

'JAVA' 카테고리의 다른 글

JAVA - NETWORKING  (0) 2024.07.15
Threads: an intro  (0) 2024.07.09
Creating JSP: using lists and getter/setter & Collections continued  (0) 2024.07.04
Importing data into JSP, Java Collection Framework: Lists  (0) 2024.07.03
Interface and Exceptions  (0) 2024.07.02