设置JTextFrame的高度

问题描述 投票:0回答:1

下面的类创建了一个表单(Jpanel),其任务是从用户那里获取多个字符串,并对它们进行处理。它在功能上是可行的,但它让我感到不舒服的是,这个类的 JTextField的组件(允许修改的组件)。一个 行的文字)自动调整,可以变得格外大。

我试过的方法是 setBounds()但是,

  1. 我不想计算位置和宽度,我不想计算 JTextField,只是它的高度;以及
  2. 它是 限高 JTextField!

有什么建议吗?

public class MultiplesStrings extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1106317992372206473L;

    /** The greater {@link JPanel}.                                                                 */
    private JPanel contentPane;

    private JPanel[] interaction;
    private JLabel[] text;
    private JTextField[] insertText;


    /** The {@link JButton} that submits the form information.                                      */
    JButton button;

    @SuppressWarnings("unused")
    private Consumer<MultiplesStrings> instructions;

    // =========================================================            
        // TODO | Constructor

    /**
     * Create the frame.
     */
    public MultiplesStrings(String title, String[] messages, 
                int x, int y, int width, int height, 
                Consumer<MultiplesStrings> instructions) {

        // ===== MAIN FRAME DEFINITION =====

        this.setTitle(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(x, y, width, height);

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
        setContentPane(contentPane);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    //  contentPane.setBackground(Colours.newColor("DDDDDD"));

        // ===== INTERACTION FRAME DEFINITION =====

        this.interaction = new JPanel[messages.length];
        this.text       = new JLabel[messages.length];
        this.insertText = new JTextField[messages.length];
        for(int i=0 ; i<messages.length ; i++)
            {
            interaction[i] = new JPanel();
            interaction[i].setLayout(new BoxLayout(interaction[i], BoxLayout.LINE_AXIS));
            interaction[i].setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
            interaction[i].add(Box.createHorizontalGlue());

            // ===== TEXT =====
            text[i] = new JLabel(messages[i]);
            text[i].setAlignmentY(RIGHT_ALIGNMENT);
        //  text.setBounds(0, imageResolution + margin, Width, buttonHeight);

            interaction[i].add(text[i]);

            // ===== INSERT TEXT FIELD =====
            insertText[i] = new JTextField();
        //  this.insertTextField.setBounds(Width + margin, imageResolution + margin, moveWidth, buttonHeight);
            insertText[i].setColumns(10);

            interaction[i].add(insertText[i]);

            this.add(interaction[i]);
            }

        // ===== SUBMIT BUTTON DEFINITION =====

        this.button = new JButton("Submit");

        // Button behavior
        MultiplesStrings support = this;
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {         }
            } );
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {     
                instructions.accept(support);       
        //      support.setVisible(false);
                }
            } );
        this.getRootPane().setDefaultButton(button);


        this.add(button);
    }

    // =========================================================            
        // TODO | Input-output manipulation

    /** Acquires all {@link String}s written by the user in the {@link JTextField}s used for {@code interactions}.  */
    public String[] acquireInputs() {
        String[] output = new String[interaction.length];
        for(int i=0 ; i<output.length ; i++)
            output[i] = insertText[i].getText();
        return output;
    }


    // =========================================================            
        // TODO | Main

    public static final int width   = 300;
    public static final int height  = 500;

    private static String[] input;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() { public void run() {
            try {
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                double screenWidth = screenSize.getWidth();
                double screenHeight = screenSize.getHeight();

                // Creates a centered form
                MultiplesStrings ms = new MultiplesStrings("Test", 
                    new String[] { "Insert first string: ", "Insert second string: ", "Insert third string: "},
                    (int) (screenWidth-width)/2,    (int) (screenHeight-height)/2, width, height, 
                    (MultiplesStrings obj) ->
                        {
                        input = obj.acquireInputs();
                        for(int i=0 ; i<input.length ; i++)
                            System.out.println("The " + i + "-th input is: " + input[i]);
                        }
                    );
                ms.setVisible(true); 

                } catch (Exception e) {         e.printStackTrace();                                }
        }
    });
}


}

java swing jtextfield
1个回答
3
投票

这是因为 BoxLayout 使用容器的整个空间。换句话说,它拉伸了所有组件,以利用总的可用空间(因为您使用了 PAGE_AXIS,它指的是可用高度)。)

其中一个解决方案是使用 BorderLayout 作为一个外部容器,并添加这个 BoxLayout-镶板内侧在,与 BorderLayout.PAGE_START 约束。PAGE_START 约束是指 "嘿,你 BoxLayout,没有可用的空间给你"。看看这个例子。

public class BoxLayoutSample extends JFrame {
    public BoxLayoutSample() {
        super("");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));

        contentPane.add(new JTextField(15));
        contentPane.add(new JTextField(15));

        setLocationByPlatform(true);
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new BoxLayoutSample().setVisible(true));
    }

}

它给我们。

preview1

这是你现在所拥有的。

但如果你使用一个外部 BorderLayout-ed面板。

public class BoxLayoutSample extends JFrame {
    public BoxLayoutSample() {
        super("");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = getContentPane(); //This is the outer panel
        contentPane.setLayout(new BorderLayout());

        JPanel boxLayoutPanel = new JPanel(); //This is the nested panel
        boxLayoutPanel.setLayout(new BoxLayout(boxLayoutPanel, BoxLayout.Y_AXIS));

        //Components to nested panel
        boxLayoutPanel.add(new JTextField(15));
        boxLayoutPanel.add(new JTextField(15));


        //PAGE_START to wrap it on the top
        contentPane.add(boxLayoutPanel, BorderLayout.PAGE_START);

        setLocationByPlatform(true);
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new BoxLayoutSample().setVisible(true));
    }
}

你得到。

preview2

还有,打电话 setBounds 方法的组件,如果它包含布局,则会被忽略。为了查看 setBounds 效果你必须 container.setLayout(null) 因为布局负责组件的边界。然而, 不建议这样做. 而不是使用布局管理器。. 让他们为你工作。

© www.soinside.com 2019 - 2024. All rights reserved.