Array Example 2: Two-dimensional array of row and column swapping and button control array to implement the calculator interface (currently does not support the calculation function, only interface and simple input)

Source: Internet
Author: User
Tags array example

1. row-column SWAps in two-dimensional arrays

The columns and columns of the array are interchangeable, which may be useful in the future.

Before row/column swaps: 123456789 after row/column swaps: 147258369

The idea is to use

Int arr2 [] [] = new int [arr. length] [arr. length]; for (int I = 0; I <arr. length; I ++) {// adjust the array row and column data for (int j = 0; j <arr [I]. length; j ++) {arr2 [I] [j] = arr [j] [I];}

To implement array swapping, note that the print () function is to output array elements, and then wrap them through println)

Code details

Public class ArrayRowColumnSwap {// create a class public static void main (String [] args) {// create a two-dimensional array int arr [] [] = new int [] [] {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }}; System. out. println ("before row and column intermodulation:"); // outputs a two-dimensional array printArray (arr); int arr2 [] [] = new int [arr. length] [arr. length]; for (int I = 0; I <arr. length; I ++) {// adjust the array row and column data for (int j = 0; j <arr [I]. length; j ++) {arr2 [I] [j] = arr [j] [I] ;}} System. out. println (":"); // outputs the two-dimensional array printArray (arr2) after );} /*** traverse the array and output all elements of the array */private static void printArray (int [] [] arr) {for (int I = 0; I <arr. length; I ++) {// traverse the array for (int j = 0; j <arr. length; j ++) {System. out. print (arr [I] [j] + ""); // outputs array elements without line breaks} System. out. println (); // line feed }}}

--------------------------------------------

2. Implement the calculator interface using the array of button controls

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1433122O5-0.jpg "title =" _x5'goky4w1_1_s1_1_qc1_ayna.jpg "alt =" 195537897.jpg"/>

It mainly uses button arrays to manage all button controls on the interface, reducing the amount of code.

All button controls are considered as an array mainly implemented through the above sentence, which simplifies a lot:

String [] [] names = {"1", "2", "3", "+"}, {"4", "5", "6 ", "-" },{ "7", "8", "9", "× "},{". "," 0 "," = "," success "}}; 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

The complete code is annotated as follows:

Package com. lixiyu; import java. awt. borderLayout; import java. awt. dimension; import java. awt. gridLayout; import java. awt. textField; 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 CalculatorButtonArray extends JFrame {private static final long serialVersionUID = 6634740733001287873l; // maintain compatibility with private JTextField textField; public static void main (String args []) {try {UIManager. setLookAndFeel ("com. sun. java. swing. plaf. nimbus. nimbusLookAndFeel "); // appearance} catch (Throwable e) {e. printStackTrace ();} CalculatorButtonArray frame = new CalculatorButtonArray (); frame. setVisible (true); // set the form to be visible, invisible by default} public CalculatorButtonArray () {super (); // inherit the construction method of the parent class BorderLayout borderlayout = (BorderLayout) getContentPane (). getLayout (); // The layout manager, which is not familiar with borderlayout yet. setHgap (20); // horizontal interval borderlayout. setVgap (20); // vertical interval setTitle ("button array implement computer simple function interface test1)"); setBounds (100,100,290,282 ); // set the display position and size of the form setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // The default form close mode 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 the grid layout manager object gridLayout. setHgap (5); // set the horizontal margin of the component gridLayout. setVgap (5); // set the Vertical spacing between components. JPanel panel = new JPanel (); // obtain the container object panel. setLayout (gridLayout); // set the container to use the grid layout manager getContentPane (). add (panel, BorderLayout. CENTER); String [] [] names = {"1", "2", "3", "+" },{ "4", "5 ", "6", "-" },{ "7", "8", "9", "× "},{". "," 0 "," = "," success "}}; 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 the button object buttons [row] [col]. addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {JButton button = (JButton) e. getSource (); String text = textField. getText (); textField. setText (text + button. getText () ;}}); panel. add (buttons [row] [col]); // add the button to the Panel }}}}

Currently, the computing function is not added, so it is just a simple shell, mainly to reflect the practical application of arrays.

Therefore, you still need to continue to learn more to get it up and running.

This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1304048

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.