Java - ayuda con scroll en java

 
Vista:
sin imagen de perfil

ayuda con scroll en java

Publicado por keyner andres (3 intervenciones) el 18/03/2015 18:54:43
buenos dias es que estoy haciendo un cuestionario de preguntas en java con netbeans y quiero hacer una barra espaciadora a mi jframe, este es el codigo que tengo.

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package formulario;
 
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class formulario extends JFrame implements ActionListener {
 
 
    private JLabel nombre, pregunta1, pregunta2, pregunta3, pregunta4, pregunta5, pregunta6, pregunta7;
     private JLabel pregunta8, pregunta9, pregunta10;JCheckBox check1,check2,check3;JTextArea text1;
      private JRadioButton Uno, Dos, Tres,A,B,C;JComboBox combo1,ballena,elefante,leon;JList list1,listaNombres;
    private JTextField n1;
    JScrollBar scroll1;
 
 
    public formulario(){
    this.setTitle("FORMULARIO PREGUNTAS");
    this.setLayout(null);
 
 
    interfaz();
    pregunta1();
    pregunta2();
    pregunta3();
    pregunta4();
    pregunta5();
     pregunta6();
 
}
    public void interfaz() {
 
       // setLayout(new FlowLayout());
        nombre = new JLabel("NOMBRE DEL ESTUDIANTE: ");
        nombre.setBounds(90, 10, 300, 20);
      this.add(nombre);
       n1=new JTextField(15);
       n1.setBounds(240, 10, 300, 20);
       this.add(n1);
 
 
       //  pregunta1
       pregunta1= new JLabel("--PREGUNTA N.1");
        pregunta1.setBounds(10, 50, 300, 20);
       this .add(pregunta1);
       pregunta1= new JLabel("***ELIGA UN MINERAL DE LAS OPCIONES****");
        pregunta1.setBounds(10, 70, 300, 20);
       this .add(pregunta1);
       //pregunta2
       pregunta2=new JLabel("--PREGUNTA N.2 ");
        pregunta2.setBounds(10, 160, 300, 20);
       this .add(pregunta2);
       pregunta2= new JLabel("***CUAL ES EL ANIMAL MAS GRANDE DEL MUNDO****");
        pregunta2.setBounds(10, 180, 300, 20);
       this .add(pregunta2);
       //pregunta 3
 
       pregunta3=new JLabel("--PREGUNTA N.3 ");
        pregunta3.setBounds(10, 230, 300, 20);
       this .add(pregunta3);
       pregunta3= new JLabel("ELIGA EL REINO AL QUE PERTENECEN LOS ANIMALES");
        pregunta3.setBounds(10, 250, 300, 20);
       this .add(pregunta3);
       //pregunta4
       pregunta4=new JLabel("--PREGUNTA N.4 ");
        pregunta4.setBounds(10, 350, 300, 20);
       this .add(pregunta4);
       pregunta4= new JLabel("CUANTAS CLASES DE AVES HAY EN COLOMBIA");
        pregunta4.setBounds(10, 370, 300, 20);
       this .add(pregunta4);
       //pregunta5
       pregunta5=new JLabel("--PREGUNTA N.5 ");
        pregunta5.setBounds(10, 420, 300, 20);
       this .add(pregunta5);
       pregunta5= new JLabel("ESCRIBA LA PALABRA ANIMAL EN INGLES");
        pregunta5.setBounds(10, 430, 300, 20);
       this .add(pregunta5);
       //PREGUNTA 6
       pregunta6=new JLabel("--PREGUNTA N.6 ");
        pregunta6.setBounds(10, 560, 300, 20);
       this .add(pregunta6);
       pregunta6= new JLabel("EN QUE PARTE VIVEN LOS OSOS POLARES");
        pregunta6.setBounds(10, 580, 300, 20);
        this .add(pregunta6);
        //pregunta7
        pregunta7=new JLabel("--PREGUNTA N.7 ");
        pregunta7.setBounds(10, 670, 300, 20);
       this .add(pregunta7);
       pregunta7= new JLabel("QUE COMEN LOS SERES HERVIVOROS");
        pregunta7.setBounds(10, 690, 300, 20);
        this .add(pregunta7);
 
    }
    public void pregunta1(){
        ButtonGroup buttonGroup = new ButtonGroup();
Uno = new JRadioButton("HIERRO");
this.add(Uno);
Uno.setBounds(10, 90, 300, 20);
Dos = new JRadioButton("PC");
Dos.setBounds(10, 110, 300, 20);
this.add(Dos);
Tres = new JRadioButton("PAPEL");
Tres.setBounds(10, 130, 300, 20);
this.add(Tres);
}
    public void pregunta2(){
        combo1= new JComboBox();
         combo1.setBounds(10, 200, 120, 20);
        add(combo1);
        combo1.addItem("balleza azul");
        combo1.addItem("leon");
        combo1.addItem("elefante");
       // combo1.addItemListener(this);
 
    }
    public void pregunta3(){
        check1=new JCheckBox("reino animal");
        check1.setBounds(10,260,150,30);
       // check1.addChangeListener(this);
        add(check1);
        check2=new JCheckBox("reino vegetal");
        check2.setBounds(10,290,150,30);
        //check2.addChangeListener(this);        
        add(check2);
        check3=new JCheckBox("reino monera");
        check3.setBounds(10,320,150,30);
        //check3.addChangeListener(this);        
        add(check3);
    }
    public void pregunta4(){
 
           JTextField p4= new JTextField();
           p4.setBounds(10,390,100,20);
           this.add(p4);
 
            }
 
    public void pregunta5(){
        text1=new JTextArea();
        text1.setBounds(10,450,100,100);
        add(text1);
    }
 
    public void pregunta6(){
         ButtonGroup osos = new ButtonGroup();
A = new JRadioButton("artico");
this.add(A);
A.setBounds(10, 600, 300, 20);
B = new JRadioButton("africa");
B.setBounds(10, 620, 300, 20);
this.add(B);
C = new JRadioButton("colombia");
C.setBounds(10, 640, 300, 20);
this.add(C);
    }
 
    public void actionPerformed(ActionEvent e) {
 
 
 
}
 
 
    public static void main(String[] arg) {
        JFrame ventana = new formulario();
        ventana.setSize(40000,40000);
        ventana.setVisible(true);
        ventana.setDefaultCloseOperation(EXIT_ON_CLOSE);
      JPanel panel = new JPanel();
 
//ventana.setContentPane(panel_con_barra ()); 
//ventana.setVisible(true); 
       // ventana.setContentPane (nuevos ScrollBarExample ());
 
 
    }
 
    }
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

ayuda con scroll en java

Publicado por eyanez (16 intervenciones) el 19/03/2015 16:06:32
Por que no usas GridBagLayout es super flexible mira este ejemplo que hice, hay coloque 80 etiquetas pero puedes colocar lo que quieras en donde quieras y no usando setBounds, eso lo puedes usar sin problema cuando la ventana tiene tamaño fijo, como una ventana de login pero para ubicación variable o ventanas redimensionables lo mejor es usar un layout

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
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
 
 
public class TestScroll extends JFrame  {
	private static final long serialVersionUID = 1L;
 
	public TestScroll(){
 
		int max = 80;
	    JPanel panelFormulario = new JPanel(new GridBagLayout());
 
	    GridBagConstraints bagConstraints = new GridBagConstraints();
	    bagConstraints.gridx = 1;
	    bagConstraints.insets.left = 30;
	    bagConstraints.insets.top = 10;
	    bagConstraints.fill = GridBagConstraints.BOTH;
	    bagConstraints.weightx = 0.1;
	    bagConstraints.weighty = 0.1;
 
 
	    JLabel lbl;
	    for(int i = 1; i <= max; i++) {
	    	lbl = new JLabel("JLabel " + i);
	    	bagConstraints.gridy = i;
 
	    	panelFormulario.add(lbl, bagConstraints);
	    }
 
 
	    JScrollPane scrollPane = new JScrollPane(panelFormulario);
 
	   getContentPane().setLayout(new BorderLayout());
	   getContentPane().add(scrollPane, BorderLayout.CENTER);
	   getContentPane().add(new JLabel("SOUTH"), BorderLayout.SOUTH);
 
    }
 
 
 
    public static void main(String[] arg) {
    	Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
 
        JFrame ventana = new TestScroll();
        ventana.setSize(dimension);
        ventana.setDefaultCloseOperation(EXIT_ON_CLOSE);
        ventana.setVisible(true);
    }
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar