Create a program that will display all numbers from 1 to 100.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class LabExercise47 extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LabExercise47 frame = new LabExercise47();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LabExercise47() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 220);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("Display 1-100");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String h = "";
String j = "";
String a = "";
String b = "";
for(int i = 1;i<=25;i++){
h = h + i +" ";
}
for(int i = 26;i<=50;i++){
j = j + i +" ";
}
for(int i = 51;i<=75;i++){
a = a + i +" ";
}
for(int i = 76;i<=100;i++){
b = b + i +" ";
}
textField.setText(String.valueOf(h));
textField_1.setText(String.valueOf(j));
textField_2.setText(String.valueOf(a));
textField_3.setText(String.valueOf(b));
}
});
btnNewButton.setBounds(5, 5, 110, 23);
contentPane.add(btnNewButton);
textField = new JTextField();
textField.setBounds(5, 39, 471, 35);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(5, 73, 471, 35);
contentPane.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(5, 107, 471, 35);
contentPane.add(textField_2);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(5, 141, 471, 35);
contentPane.add(textField_3);
}
}
No comments:
Post a Comment