Swing App-调整东面板组件的位置 [英] Swing App - Adjusting Position of East Panel Components

查看:116
本文介绍了Swing App-调整东面板组件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在东面板上设置的文本字段和组合框组件有问题.由于某些原因,当我添加Box布局以按Y排列它们时,上面列出的某些组件无法按原样正确对齐和缩放按钮的大小.

I have a problem with text field and combo box components which are set on the east panel. For some reason, when I added Box layout to arrange them by Y, some of the components listed above, doesn't align and scale size properly with buttons, just as they should be.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 *
 * @author Isaac
 */
public class Test2 extends JFrame {

    private JButton addNewColumnButton;
    private JButton calculateColumnButton;
    private JButton resultButton;

    private JLabel textLabel;

    private JTextField columnField;
    private JTextField resultField;            

    private JComboBox columnListCB;
    private JTable table;

    private String[] tableCols = {"Fisrt Column", "Second Column", "Third Column", "", "", "", "", ""};
    private Object[][] tableRows = {
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null},
            {true, null, null, null, null, null, null, null}
    };

    public Test2() {
        this.setSize(new Dimension(600, 280)); 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        this.init();
        this.add(getUIPanel());

        this.pack();
        this.setVisible(true);
    }

    private JPanel getUIPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setBackground(Color.blue);

        JPanel center = new JPanel();
        center.add(table);

        JPanel eastPanel = new JPanel();
        eastPanel.setBackground(Color.MAGENTA);
            Box eastPanelBox = Box.createVerticalBox();
            eastPanelBox.add(addNewColumnButton);
            eastPanelBox.add(Box.createVerticalStrut(14));
            eastPanelBox.add(columnField);
            eastPanelBox.add(Box.createVerticalStrut(5));
            eastPanelBox.add(columnListCB);
            eastPanelBox.add(Box.createVerticalStrut(5));
            eastPanelBox.add(calculateColumnButton);
        eastPanel.add(eastPanelBox);

        JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        southPanel.setBackground(Color.green);
            southPanel.add(resultButton);
            southPanel.add(textLabel);
            southPanel.add(resultField);

        panel.add(center, BorderLayout.WEST);
        panel.add(southPanel, BorderLayout.SOUTH);
        panel.add(eastPanel, BorderLayout.EAST); 
        return panel;
    }

    private void init() {
        final int COMPONENT_WIDTH = 130;
        final int COMPONENT_HEIGHT = 25;

        table = new JTable(tableRows, tableCols);

        addNewColumnButton = new JButton("New Column");
        addNewColumnButton.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        columnField = new JTextField();
        columnField.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        columnListCB = new JComboBox(tableCols);
        columnListCB.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        calculateColumnButton = new JButton("Calculate Column");
        calculateColumnButton.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        resultButton = new JButton("Calculate");
        calculateColumnButton.setPreferredSize(new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT));

        textLabel = new JLabel("Result:");

        resultField = new JTextField(); 
        resultField.setPreferredSize(new Dimension(COMPONENT_WIDTH / 2, COMPONENT_HEIGHT));
    }

    public static void main(String[] args) {
        new Test2();
    }
}

推荐答案

您的示例中出现了几个问题:

Several issues arise in your example:

  • "一般,由顶部到底部BoxLayout对象控制的所有组件都应具有相同的X对齐."

  • "In general, all the components controlled by a top-to-bottom BoxLayout object should have the same X alignment."

使用适当的构造函数指定JTextField的初始大小.

Specify the initial size of a JTextField using the appropriate constructor.

当您真的要覆盖 getPreferredSize() 时,请勿使用setPreferredSize().

调用pack(),然后 设置位置&可见性.

Invoke pack() and then set the location & visibility.

正确使用初始线程.

不必不必要地扩展顶级组件,例如JFrame.

Don't needlessly extend a top-level component, e.g. JFrame.

在不可调整大小的容器上避免此陷阱.

Avoid this pitfall on non-resizable container.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 * @author Isaac
 * @see https://stackoverflow.com/a/18037704/230513
 */
public class Test2 {

    private JButton addNewColumnButton;
    private JButton calculateColumnButton;
    private JButton resultButton;
    private JLabel textLabel;
    private JTextField columnField;
    private JTextField resultField;
    private JComboBox columnListCB;
    private JTable table;
    private String[] tableCols = {
        "Fisrt Column", "Second Column", "Third Column"
    };
    private Object[][] tableRows = {
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null},
        {true, null, null, null, null, null, null, null}
    };

    public Test2() {
        JFrame f = new JFrame("Test2");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(getUIPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private JPanel getUIPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        panel.setBackground(Color.blue);

        JPanel center = new JPanel(new GridLayout());
        table = new JTable(tableRows, tableCols);
        table.setPreferredScrollableViewportSize(new Dimension(240, 120));
        center.add(new JScrollPane(table));

        JPanel eastPanel = new JPanel();
        eastPanel.setBackground(Color.MAGENTA);
        Box eastPanelBox = Box.createVerticalBox();
        addNewColumnButton = new JButton("New Column");
        addNewColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(addNewColumnButton);
        eastPanelBox.add(Box.createVerticalStrut(14));
        columnField = new JTextField();
        columnField.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(columnField);
        eastPanelBox.add(Box.createVerticalStrut(5));
        columnListCB = new JComboBox(tableCols);
        columnListCB.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(columnListCB);
        eastPanelBox.add(Box.createVerticalStrut(5));
        calculateColumnButton = new JButton("Calculate Column");
        calculateColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
        eastPanelBox.add(calculateColumnButton);
        eastPanel.add(eastPanelBox);

        JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
        southPanel.setBackground(Color.green);
        resultButton = new JButton("Calculate");
        southPanel.add(resultButton);
        textLabel = new JLabel("Result:");
        southPanel.add(textLabel);
        resultField = new JTextField(10);
        southPanel.add(resultField);

        panel.add(center, BorderLayout.WEST);
        panel.add(southPanel, BorderLayout.SOUTH);
        panel.add(eastPanel, BorderLayout.EAST);
        return panel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test2();
            }
        });
    }
}

这篇关于Swing App-调整东面板组件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆