Home > Java > JavaBase > body text

How to create and set a button in java

angryTom
Release: 2019-11-12 13:51:18
Original
16226 people have browsed it

How to create and set a button in java

How to create and set a button in java

AWT (Abstract Window Toolkit): is provided by java Basic tools for establishing and setting up java graphical user interface

Swing (lightweight graphical interface component): It is a new component built based on the AWT platform. Compared with AWT, The Swing component does not include any code that depends on a specific platform when implemented, so it has higher platform independence and good portability, so Swing has become the first choice for graphical user interface development.

Recommended tutorial: java tutorial

The following describes how to create a window program and set a button.

1. First import the necessary packages java.awt.*, javax.swing.*;

2. Then create a new layout and create a button on it;

3. Finally, use the button method to set the button.

The code is as follows:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class KeyDemo extends JFrame{
    int index;
    JLabel jl = new JLabel("你好啊~");
    JButton jb = new JButton("点击改变标签上的文字");
    public KeyDemo() {
        //设置界面的布局为边界布局
        this.setLayout(new BorderLayout());
        //设置标签文字的位置在 布局的中间
        this.add(jl, BorderLayout.CENTER);
        //设置按钮在布局的南部
        this.add(jb, BorderLayout.SOUTH);
        //设置窗口的位置和大小
        this.setBounds(350, 100, 200, 120);
        //设置窗口的关闭事件的响应,如果点击关闭按钮,那么就退出
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置窗口的标题
        this.setTitle("窗口");
        //设置窗口是否可见
        this.setVisible(true);
         
        //为按钮注册事件响应,有了这句代码就能让按钮能够响应点击事件了
        jb.addActionListener(new MyActionListener());
    }
    public static void main(String[] args) {
        //实例化窗口对象
        new KeyDemo();
    }
    //实现动作Listener接口。实现里面的actionPerformed方法
    class MyActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            index++;
            jl.setText("你点击了"+index+"次按钮");
        }
    }
}
Copy after login

Commonly used methods for buttons:

getText()

Usage: button name.getText();

Function: Get the string information in the button.

setBounds( int Parameters/)

Function: Set the position of the button in the program interface and the size of the button. The first two parameters are the button position, which can be understood as the horizontal and vertical coordinates; the last two parameters are the width and size of the button. high.

setBackgrond(color bg) Usage: button name.setBackground(/fill in the color you want, the format is Color.color /)

Function: Set the background color of the button.

setForeground(color bg) Usage: button name.setForeground(/same as above/)

Function: setting The foreground color of the button.

setFont(font) Usage: button name.setFont(/font name you have defined/)

Function: Set the font format of the text in the button

setOpaque(boolean is Opaque) Usage: button name.setOpaque(/Boolean Value/)

Function: Set whether the background of the button component is visible, false is transparent

setFocusPainted(boolean b) Usage: button name.setFocusPainted(/Boolean value/)

Function: Set whether the focus box of the text in the button is visible. In fact, it defines whether the button will become the focus after being clicked.

setBorderPainted(boolean b) Usage: button name.setBorederPainted(/Boolean value/)

Function: You can set whether the component border is displayed.

setToolTipText(String str) Usage: button name.setToolTipText(/String to be displayed/)

Function: You can generate a prompt box. When the mouse is placed on the component, the string set in the parameter will be displayed in the prompt box.

The above is the detailed content of How to create and set a button 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