Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- class sortData extends JFrame {
- // Private Declarations
- private JTextField txtBOX_1 = new JTextField(2);
- private JTextField txtBOX_2 = new JTextField(2);
- private JTextField txtBOX_3 = new JTextField(2);
- private JButton jbt = new JButton("SORT!");
- private JLabel greeting = new JLabel("Enter Data to Sort");
- private JLabel comma1 = new JLabel(",");
- //
- public sortData()
- {
- JPanel content = new JPanel();
- content.setLayout(new FlowLayout());
- this.setTitle("PROGRAM 1");
- setContentPane(content);
- pack();
- // sets content layout
- content.add(greeting);
- content.add(txtBOX_1);
- content.add(comma1);
- content.add(txtBOX_2);
- content.add(comma1);
- content.add(txtBOX_3);
- content.add(jbt);
- //Shape line = new Line2D.Float(100, 120, 200, 22);
- //content.addShape(line);
- jbt.addActionListener(new btnListener()); // calls button listener
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //closes program with frame
- setLocationRelativeTo(null); // centers window
- }
- class btnListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- int a = txtBOX_1.getText();
- int b = txtBOX_2.getText();
- int c = txtBOX_3.getText();
- txtBOX_1.setText("");
- int [] Arr = {a, b, c};
- Arr = sort(Arr);
- for (i = 0; i < Arr.length; i ++)
- {
- System.out.print(Arr[i]);
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////
- public static void main(String [] args) {
- }
- public static void swap (int loc1, int loc2, int [] x)
- {
- int temp = x[loc1];
- x[loc1] = x[loc2];
- x[loc2] = temp;
- }
- public static int [] sort(int [] arr)
- {
- for (int x = (arr.length - 1); x > 0; x--)
- {
- for (int i = 0; i < (arr.length - 1);i++)
- {
- if (arr[i] > arr[i + 1]) // checks if current is greater than the one to the right
- {
- swap(i, (i + 1), arr);
- }
- }
- }
- return arr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement