Java Button control array Implementation Calculator Interface sample sharing _java

Source: Internet
Author: User

Ideas are as follows:

Create a class that inherits the form class JFrame by extends;
Creates a JFrame object and uses the SetVisible () method of the JFrame class to set the form visible;
In the constructor, use the super () method to inherit the constructor method of the parent class;
Use the Settitle () method to set the title of the form;
Use the SetBounds () method to set the display position and size of the form;
Use the Setdefaultcloseoperation () method to set the action of the form Close button to exit;
Use GridLayout to create a Grid Layout manager object;
Use the Sethgap () method of the GridLayout class to set the horizontal spacing of the components;
Use the Setvgap () method of the GridLayout class to set the vertical spacing of the component;
Create a JPanel container object;
The SetLayout () method of JPanel class is used to set up the container using Grid layout manager.
Creates a string two-dimensional array that initializes its value to the value displayed on the corresponding button on the calculator;
Creates a two-dimensional array of JButton, and assigns the space corresponding to the previous string-type two-dimensional array;
Traversing a string-type two-dimensional array, each element of it is assigned to the corresponding button in the JButton two-dimensional array, and an event is added to each button, which displays the corresponding value in the text input box when the button is clicked, and finally adds the button to the panel using the Add () method of the JPanel class.

Copy Code code as follows:

Import Java.awt.BorderLayout;
Import java.awt.Dimension;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;

Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JTextField;
Import javax.swing.SwingConstants;
Import Javax.swing.UIManager;

public class Buttonarrayexample extends JFrame {//Inheriting form class JFrame
/**
*
*/
Private static final long serialversionuid = 6626440733001287873L;
Private JTextField TextField;

public static void Main (String args[]) {
try {
Uimanager.setlookandfeel ("Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
catch (Throwable e) {
E.printstacktrace ();
}
Buttonarrayexample frame = new Buttonarrayexample ();
Frame.setvisible (TRUE); Set form visible, default is not visible
}

Public Buttonarrayexample () {
Super (); To inherit the constructor method of the parent class
BorderLayout BorderLayout = (borderlayout) Getcontentpane (). GetLayout ();
Borderlayout.sethgap (20);
Borderlayout.setvgap (10);
Settitle ("button array Implementation calculator interface"); Set the title of a form
SetBounds (100, 100, 290, 282); Set the display position and size of a form
Setdefaultcloseoperation (Jframe.exit_on_close); Sets the action of the form Close button to exit
TextField = new JTextField ();
Textfield.sethorizontalalignment (swingconstants.trailing);
Textfield.setpreferredsize (New Dimension (12, 50));
Getcontentpane (). Add (TextField, Borderlayout.north);
Textfield.setcolumns (10);
Final GridLayout GridLayout = new GridLayout (4, 0); Create a Grid Layout manager object
Gridlayout.sethgap (5); Set Horizontal spacing for components
Gridlayout.setvgap (5); Set Vertical spacing for components
JPanel panel = new JPanel (); Get Container Object
Panel.setlayout (gridLayout); Set up container with Grid layout manager
Getcontentpane (). Add (Panel, borderlayout.center);
String[][] names = {"1", "2", "3", "" + "}, {" 4 "," 5 "," 6 ","-"}, {" 7 "," 8 "," 9 "," X "}, {". "," 0 "," = "," C "}};
jbutton[][] buttons = new Jbutton[4][4];
for (int row = 0; row < names.length; row++) {
for (int col = 0; col < names.length; col++) {
Buttons[row][col] = new JButton (Names[row][col]); Create a Button object
Buttons[row][col].addactionlistener (new ActionListener () {

@Override
public void actionperformed (ActionEvent e) {
JButton button = (JButton) e.getsource ();
String text = Textfield.gettext ();
Textfield.settext (text + button.gettext ());
}
});
Panel.add (Buttons[row][col]); Add a button to a panel
}
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.