Pregunta

Aquí está el código:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class TestGrid {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Colored Trails");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4, 9));
        panel.setMaximumSize(new Dimension(9*30-20,4*30));

        JButton btn;
        for (int i=1; i<=4; i++) {
            for (int j=1; j<=4; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(30, 30));
                panel.add(btn);
            }

            btn = new JButton();
            btn.setPreferredSize(new Dimension(30, 10));
            panel.add(btn);

            for (int j=1; j<=4; j++) {
                btn = new JButton();
                btn.setPreferredSize(new Dimension(30, 30));
                panel.add(btn);
            }

        }
        mainPanel.add(panel);
        frame.add(mainPanel);

        frame.setSize(450,950);
        frame.setVisible(true);
    }
}

Supongo que tengo una tabla de botones con 4 filas y 9 columnas. Y la columna central debe ser más estrecha que otras columnas. Lo intenté Dimension(30, 10) y Dimension(30, 10) Ambos no tienen efecto sobre el ancho de la columna central. ¿Por qué?

¿Fue útil?

Solución

Los gerentes de diseño son libres de ignorar el tamaño preferido. Específicamente, GridLayout Siempre hará que cada celda en la cuadrícula sea exactamente del mismo tamaño (es un administrador de diseño bastante inútil por ese motivo).

Tendrá que usar un administrador de diseño diferente, como anidados BoxLayout o GroupLayout.

Otros consejos

Para usar los colores de la Web Host, deberá cargar dinámicamente la hoja de estilo de la Web Host con un poco de JavaScript.Si está utilizando JQery en la página, puede usar el siguiente bloque de JavaScript para inyectar la hoja de estilo de Web Host en su página.

(function () {
    // Retrieve the host web's URL from the query string
    var scriptbase = $.queryString('SPHostUrl') + '/_layouts/15/';

    // Create a <link> tag for the style sheet
    var $doclink = $('link').attr('rel', 'stylesheet');
    // The style sheet is loaded through an ASP.NET HTTP handler (defaultcss.ashx)
    $doclink.attr('href', scriptbase + 'defaultcss.ashx');

    // Add the style sheet link to the 
    $('head').append($doclink);
})();

Si eso no funciona, también puede revisar el artículo de Microsoft Cómo: Use una hoja de estilo de un sitio web de SharePoint en Aplicaciones para SharePoint para un ejemplo que no requiere jquery.

setPreferredSize no cambiará el tamaño del botón hasta que se establezca la dimensión utilizando la dimensión.

Ejemplo:-

Dimension dim = new Dimension(20,20), then use setPerferredSize(dim).

Encontré esta versión del método, que se ajusta perfecta a su solución:

.addComponent(<<a jButton instance>>, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, <<put here an integer to set you max width button size>>)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top