Home > Java > javaTutorial > Can I write window programs in java?

Can I write window programs in java?

(*-*)浩
Release: 2019-05-28 13:38:33
Original
3911 people have browsed it

Java is an object-oriented programming language that can write cross-platform application software. The advantage of using Java lies in network applications, but Java also provides powerful APIs for developing desktop programs, which are included in the javax.swing package. Use this package to write simple Java desktop applications (window programs).

Can I write window programs in java?

Java's javax.swing package provides many component classes for designing GUIs. When learning GUI programming, you need to master two concepts: container classes (Container) and components Class (Component), the following are basic knowledge points often mentioned in GUI programming.

Java calls an object created by a subclass or indirect subclass of the Component class a component.

Java The object created by a subclass or indirect subclass of Container is called a container.

You can add components to the container. The Container class provides a public method add(), and a container can call this method to add components. Go to the container.

The container calls the removeAll() method to remove all components in the container, and calls the remove(Component c) method to remove the component specified by the container's total parameter c.

The container itself is also a component, so you can add a container to another container to achieve nesting of containers.

Whenever the container adds a new component or removes a component, the container should call the validate method. , to ensure that the components in the container can be displayed correctly.

Next, write a simple desktop program in Java.

A Java application needs to use an instance of the JFrame class to provide an underlying container to interact with the operating system. When a window is required, use JFrame or its subclass to create an object. A window is also a container, and components can be added to the window. It should be noted that windows are added to the monitor screen by the system by default, and one window is not allowed to be added to another container at a time.

package gui;  
import javax.swing.*;  
import java.awt.*;  
import java.awt.event.KeyEvent;  
import java.awt.event.InputEvent;   
  
public class Text {  
  
    public static void main(String[] args) {  
        JFrame a=new JFrame("浏览器");//顶层容器  
        JMenuBar b=new JMenuBar();//菜单条  
        JMenu c=new JMenu("文件A");//菜单  
        c.setMnemonic('A');     
        //c.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));    
        JMenu d=new JMenu("编辑B");//菜单  
        d.setMnemonic('B');   
        //d.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_MASK));  
        JMenu e=new JMenu("查看C");//菜单  
        c.setMnemonic('C');   
        //e.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));   
        JMenuItem f=new JMenuItem("保存");//菜单项  
        f.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));           
        JMenu g=new JMenu("打开");//子菜单  
        //g.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));  
        JMenuItem h=new JMenuItem("打开x");//菜单项  
        h.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));  
        JMenuItem i=new JMenuItem("打开y");//菜单项  
        i.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));  
        JMenuItem j=new JMenuItem("复制");//菜单项  
        j.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));  
        JMenuItem k=new JMenuItem("停止");//菜单项  
        k.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,InputEvent.CTRL_MASK));  
        JMenuItem l=new JMenuItem("刷新");//菜单项  
        l.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));  
        g.add(h);  
        g.add(i);  
        c.add(g);  
        c.add(f);  
        d.add(j);  
        e.add(k);  
        e.add(l);  
        b.add(c);  
        b.add(d);  
        b.add(e);  
        a.setJMenuBar(b);  //将菜单条加入窗口  
        a.setBounds(260,100,188,108);  
        a.setVisible(true);  
        a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
            
  
    }  
  
}
Copy after login

The above is the detailed content of Can I write window programs in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template