Java - ejercicio GUI en java

 
Vista:
sin imagen de perfil
Val: 4
Ha aumentado su posición en 6 puestos en Java (en relación al último mes)
Gráfica de Java

ejercicio GUI en java

Publicado por kotito (4 intervenciones) el 04/06/2016 00:54:38
Hola!!

intento hacer mi primer ejercicio en java acerca de crear una GUI. Es un ejercicio de un libro que me pide que cree una GUI. Es el que adjunto en una imagen
Sin-titulo-2

Como no tengo mucho control sobre este tema pues no soy capaz de hacerlo correctamente.
El código que al que he llegado es el siguiente:
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
/*
contruir una GUI
 */
package tema11;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.*;
 
public class ocho extends JFrame implements ActionListener{
 
 
    private JPanel adjPanel;
    private JPanel xyPanel;
    private JPanel buttonPanel;
 
    private JButton botones[];
    private final String nombres[] = {"Aceptar","Cancelar","Ayuda"};
 
    private JLabel labelX;
    private JLabel labelY;
    private JTextField textX;
    private JTextField textY;
 
    private JCheckBox ajustarCheck;
    private JCheckBox mostrarCheck;
    private JLabel ajustarL;
    private JLabel mostrarL;
 
 
    //constructor
    public ocho()
    {
        super("ocho");
 
        //-----CREACION DE PANELES
//        mainPanel = new JPanel(new FlowLayout());
 
        adjPanel = new JPanel();
            adjPanel.setLayout(new GridLayout(2,2));
 
        xyPanel = new JPanel();
            xyPanel.setLayout(new GridLayout(2,2));
 
        buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel,BoxLayout.Y_AXIS));
        //---------------------------------------------
 
 
        //-----dentro de buttonPanel------------------------------------------------------------------------------------
        botones = new JButton[nombres.length]; //creamos la cantidad de botones. en este caso son 3
        //apartir de aqui creamos visualmente los botones (los objetos) con sus nombres y sus componentes de escucha
        for(int i=0; i<nombres.length; i++)
        {
            botones[i] = new JButton(nombres[i]); //objeto boton con su nombre
            botones[i].addActionListener(this); //y para este boton tendra su componente de escucha
            buttonPanel.add(botones[i]);
        }
        //--------------------------------------------------------------------------------------------------------------
 
 
        //------dentro de xyPanel---------------------------------------------------------------------------------------
        labelX = new JLabel("X");
            xyPanel.add(labelX);
        textX = new JTextField();
            xyPanel.add(textX);
 
        labelY = new JLabel("Y");
            xyPanel.add(labelY);
        textY = new JTextField();
            xyPanel.add(textY);
        //--------------------------------------------------------------------------------------------------------------
 
 
        //-----dentro de adjPanel---------------------------------------------------------------------------------------
        ajustarCheck = new  JCheckBox();
            adjPanel.add(ajustarCheck);
        ajustarL = new JLabel("ajustar a la cuadricula");
            adjPanel.add(ajustarL);
        mostrarCheck = new JCheckBox();
            adjPanel.add(mostrarCheck);
        mostrarL = new JLabel("mostrar la cuadricula");
            adjPanel.add(mostrarL);
        //--------------------------------------------------------------------------------------------------------------
 
 
        setLayout(new BorderLayout(25,25));
        add(adjPanel, BorderLayout.WEST);
        add(xyPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.EAST);
 
    }//fin constructor
 
 
    //maneja los eventos de los botones. Aqui declaramos lo que los botones harán
    public void actionPerformed(ActionEvent e)
    {
        for(JButton boton : botones)
        {
            if(e.getSource() == boton)
                JOptionPane.showMessageDialog(ocho.this, String.format("Oprimio: %s",e.getActionCommand()));
        }
    }
}


aqui adjunto la clase principal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package tema11;
 
import javax.swing.JFrame;
 
public class Tema11 {
 
    public static void main(String[] args) {
 
 
                ocho OCHO = new ocho();
                OCHO.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                OCHO.setSize(775, 120);
                OCHO.setVisible(true);
}}

Algo me sale, pero no soy capaz de manipular bien los frames, ni tampoco hacer que la ventana sea vea ordenado.

agradezco cualquier ayuda, consejo, como seguir mejorando, etece
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