微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JTextField 可以在网格布局中调整大小吗?

如何解决JTextField 可以在网格布局中调整大小吗?

下面是我测试BorderLayoutGridLayout代码,但是我发现使用JTextField的{​​{1}}中的JPanel不能调整大小,不管我更改了多少次 GridLayout,它仍然保持不变。希望有人能帮帮我,哈哈哈,我卡在这两天了....TQ

(我解决问题的时候会上传解决方案)

JTextField.setPreferredSize

问题已解决 请参考 Abra 的评论。 通过我到现在的理解,主要原因是GridLayout不支持set preffered size。解决办法是改成GridBagLayout。

解决方法

参考How to Use GridLayout
这是该网页的引述:

GridLayout 对象将组件放置在单元格网格中。每个组件占用其单元格内的所有可用空间,并且每个单元格的大小完全相同。

换句话说,GridLayout 不会考虑它包含的组件的首选大小。

确实考虑其包含的组件的首选大小的布局管理器是 GridBagLayout

BorderLayout 仅考虑其包含的组件的首选大小的一部分。对于 EAST 组件,BorderLayout 使用其首选宽度而不是首选高度。 BorderLayout 最初将使用其 CENTER 组件的首选宽度和高度。

这是我运行你的代码时得到的窗口。

OP code screen capture

这是您使用 GridBagLayout 而不是 GridLayout 修改后的代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Boderlayout implements ActionListener {
    JButton bCalculate;
    JLabel lPrinAmount,lInterestRate,lPayPerYear,lResult;
    JTextArea tResult;
    JTextField tPrinAmount,tInterestRate,tPayPerYear;

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

    public Boderlayout() {
        JFrame frame = new JFrame();

        JPanel p2 = new JPanel();
        p2.setBackground(Color.BLUE);
        p2.setPreferredSize(new Dimension(600,100));
        frame.add(p2,BorderLayout.SOUTH);

        bCalculate = new JButton("Calculate");
        bCalculate.setPreferredSize(new Dimension(550,85));
        p2.add(bCalculate);
        bCalculate.addActionListener(this);

        JPanel p3 = new JPanel();
        p3.setBackground(Color.GREEN);
        p3.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        p3.setPreferredSize(new Dimension(300,300));
        frame.add(p3,BorderLayout.CENTER);

        lPrinAmount = new JLabel("Principle Amount: ");
        lPrinAmount.setPreferredSize(new Dimension(200,40));
        gbc.gridx = 0;
        gbc.gridy = 0;
        p3.add(lPrinAmount,gbc);

        tPrinAmount = new JTextField(20);
        tPrinAmount.setPreferredSize(new Dimension(170,40));
        gbc.gridy = 1;
        p3.add(tPrinAmount,gbc);

        tPrinAmount.addActionListener(this);

        lInterestRate = new JLabel("Interest Rate: ");
        lInterestRate.setPreferredSize(new Dimension(200,40));
        gbc.gridy = 2;
        p3.add(lInterestRate,gbc);

        tInterestRate = new JTextField(20);
        tInterestRate.setPreferredSize(new Dimension(100,40));
        gbc.gridy = 3;
        p3.add(tInterestRate,gbc);

        tInterestRate.addActionListener(this);

        lPayPerYear = new JLabel("Payment Period (Year): ");
        lPayPerYear.setPreferredSize(new Dimension(200,40));
        gbc.gridy = 4;
        p3.add(lPayPerYear,gbc);

        tPayPerYear = new JTextField(20);
        tPayPerYear.setPreferredSize(new Dimension(170,40));
        gbc.gridy = 5;
        p3.add(tPayPerYear,gbc);

        tPayPerYear.addActionListener(this);

        JPanel p5 = new JPanel();
        p5.setLayout(new BoxLayout(p5,BoxLayout.PAGE_AXIS));
        p5.setBackground(Color.WHITE);
        p5.setPreferredSize(new Dimension(300,300));
        frame.add(p5,BorderLayout.EAST);

        lResult = new JLabel("Result");
        lResult.setPreferredSize(new Dimension(170,40));
        p5.add(lResult);

        tResult = new JTextArea();
        tResult.setPreferredSize(new Dimension(170,200));
        tResult.setEditable(false);
        tResult.setBorder(BorderFactory.createLineBorder(Color.darkGray));
        p5.add(tResult);

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

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bCalculate) {
            double prinAmount = Double.parseDouble(tPrinAmount.getText());
            double interestRate = Integer.parseInt(tInterestRate.getText());
            double paymentYear = Integer.parseInt(tPayPerYear.getText());

            double interestRatePercentage = interestRate / 100;
            double loanInterest = prinAmount * interestRatePercentage;
            double year = 12 * paymentYear;
            double mortgageResult = (loanInterest
                    * (Math.pow((1 + (interestRatePercentage / 12)),year))
                    / (Math.pow((1 + (interestRatePercentage) / 12),year) - 1)) / 12;

            String stringmortgageResult = Double.toString(mortgageResult);
            String newline = System.getProperty("line.separator");
            String finalResult = "Principle Amount: " + tPrinAmount.getText() + newline
                    + "Interest Rate: " + tInterestRate.getText() + "%" + newline
                    + "Payment Period: " + tPayPerYear.getText() + newline + "Mortgage: "
                    + stringmortgageResult;
            tResult.setText(finalResult);
        }
    }
}

这是我运行上面代码时得到的窗口。

enter image description here

请注意,我将 tResult 改为 JTextArea 而不是 JTextField

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。