
Ayuda con Programación Orientada
Publicado por Alejandro (14 intervenciones) el 02/06/2016 00:15:53
Buenas tardes, ¿Me podrían ayudar a orientar a objetos este código? Me he estado quebrando la cabeza y no veo como poder orientarlo... Les agradecería su ayuda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class Gui {
private static void ventana() {
Random rnd = new Random();
JFrame frame = new JFrame("Ejercicio GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
pane.setLayout(new GridLayout(3,2));// Cualquier cantidad de renglones y 3 columnas
int prim= (int)(rnd.nextDouble() * 100 + 0);
int prim1= (int)(rnd.nextDouble() * 100 + 0);
//System.out.println(prim);
String z=String.valueOf(prim);
String y=String.valueOf(prim1);
JLabel etiq1=new JLabel(z);
JLabel etiq2=new JLabel(y);
JButton botMas1=new JButton("+");
JButton botMenos1=new JButton("-");
JButton botMas2=new JButton("+");
JButton botMenos2=new JButton("-");
pane.add(etiq1);
pane.add(etiq2);
pane.add(botMas1);
pane.add(botMas2);
pane.add(botMenos1);
pane.add(botMenos2);
botMas1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
int n1;
String r;
n1=Integer.parseInt(etiq1.getText());
if(n1<100){
n1=n1+1;
r=String.valueOf(n1);
etiq1.setText(r);}
}catch(NumberFormatException nfe){
System.out.println("Error");
}
}
});
botMas2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
int n2;
String t;
n2=Integer.parseInt(etiq2.getText());
if(n2<100){
n2=n2+1;
t=String.valueOf(n2);
etiq2.setText(t);}
}catch(NumberFormatException nfe){
System.out.println("Error");
}
}
});
botMenos1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
int n1;
String r;
n1=Integer.parseInt(etiq1.getText());
if(n1>=1){
n1=n1-1;
r=String.valueOf(n1);
etiq1.setText(r);}
}catch(NumberFormatException nfe){
System.out.println("Error");
}
}
});
botMenos2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
int n2;
String t;
n2=Integer.parseInt(etiq2.getText());
if(n2>=1){
n2=n2-1;
t=String.valueOf(n2);
etiq2.setText(t);}
}catch(NumberFormatException nfe){
System.out.println("Error");
}
}
});
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ventana();
}
});
}
}
Valora esta pregunta


0