SWT:如何使各种RowLayout中的所有按钮具有相同的宽度

时间:2018-11-05 13:37:38

标签: java layout swt composite

我想显示一个5列9行的表格。该表将显示45个按钮,每个按钮都有在按钮文本中写的数字。第一列具有从1到9的数字,因此按钮的宽度小于后面四列的宽度,因为下一列具有两位数字。

目标是在五个RowLayout的45个按钮中具有相同的宽度。

    Composite numbersComposite = new Composite(composite, SWT.NONE);
    numbersComposite.setLayout(new GridLayout(5, true));

    int rows = 9;
    int columns = 5;                        

    for (int i=0; i<columns; i++) {
        Composite column = new Composite(numbersComposite, SWT.NONE);
        RowLayout columnLayout = new RowLayout(SWT.VERTICAL);
        columnLayout.fill = true;
        column.setLayout(columnLayout);

        for (int j=0;j<rows; j++) {         
            button = new Button(column, SWT.TOGGLE);
            button.setText(""+number);      
            buttons.add(button);    
            number++;
        }
    }

我不想为所有按钮设置固定大小,因为如果用户增加计算机中的字体大小,则文本将无法以固定大小完全显示。

2 个答案:

答案 0 :(得分:1)

这对所有内容使用from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.figure import Figure from tkinter import ttk import tkinter as tk class MainApplication(tk.Tk): def __init__(self): tk.Tk.__init__(self) notes = ttk.Notebook(self) notes.grid(column=0, row=0, sticky='nsew') notes.rowconfigure(0, weight=1) self.page = ttk.Frame(notes) notes.add(self.page, text='Picture') self.plotter() input_frame = ttk.Frame(self) input_frame.grid(column=1, row=0, sticky='nsew') button = ttk.Button(input_frame, text='Plot', command=self.new_draw) button.grid(column=0, row=4, columnspan=2, sticky='ew') def plotter(self): self.figure = Figure(dpi=100) self.plot_canvas = FigureCanvasTkAgg(self.figure, self.page) self.axes = self.figure.add_subplot(111) self.plot_canvas.get_tk_widget().grid(column=0, row=0, sticky='nsew') def new_draw(self): self.axes.clear() x_list = [x for x in range(0, 100)] y_list = [x^3 for x in x_list] self.axes.plot(x_list, y_list, color='y') self.plot_canvas.draw_idle() MainApplication().mainloop() ,并在所有控件和组合上设置GridLayout以填充列:

GridData

答案 1 :(得分:0)

只需将pack设置为false。

  

pack 指定布局中的所有控件是否都使用其   首选尺寸

import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.io.Serializable;

import static org.apache.commons.lang3.StringUtils.isNotBlank;


public class InitialRequestParametersFilter implements Filter {

    @Inject
    private Provider<InitialRequestParameters> parameters;

    @SessionScoped
    public static class InitialRequestParameters implements Serializable {
        private String contractNumber = null;

        InitialRequestParameters() {}

        private void setContractNumber(String contractNumber) {
            this.contractNumber = contractNumber;
        }

        public Optional<String> getContractNumber() {
            return Optional.fromNullable(contractNumber);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        final String contractNumberFromRequest = servletRequest.getParameter("contractno");
        if(isNotBlank(contractNumberFromRequest)) {
            parameters.get().setContractNumber(contractNumberFromRequest);
        }
        // this returns the request parameter
        System.out.println(parameters.get().contractNumber);
        // this returns Optional.absent() !!!
        System.out.println(parameters.get().getContractNumber());
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }
}