我正在尝试从刚读的书中学习GUI,但是我遇到了很多问题(已附加我的代码)。当我启动此应用程序时,我得到的只是一个最小的窗口,每次都需要扩展,并且它唯一显示的是我的单选按钮之一。我显然在这里做错了。有人可以建议我吗?



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

public class CarPayment
{

    public static void main(String[] args)
    {
        new CarPaymentCalc();
    } // main
} // CarPayment

class CarPaymentCalc extends JFrame
{
    private JLabel labelTitle, labelInterest, labelLoan;
    private JTextField tfLoan, tfInterest, tfAnswer;
    private ButtonGroup bgSelect;
    private JRadioButton rbPmts36, rbPmts48, rbPmts60;
    private JButton bClear;

    public CarPaymentCalc()
    {
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // Centers the window
        setTitle("Car Payments Calculator");

        // Labels
        labelTitle = new JLabel("Calculate My Car Payment");
        labelTitle.setVerticalAlignment(JLabel.TOP);
        add(labelTitle, JLabel.CENTER);

        labelLoan = new JLabel("Loan Amount");
        labelLoan.setLocation(0, 10);
        add(labelLoan);

        labelInterest = new JLabel("Interest");
        labelInterest.setLocation(0, 45);
        add(labelInterest);

        // Input Fields
        tfLoan = new JTextField(20);
        tfLoan.setLocation(0, 25);
        add(tfLoan);

        tfInterest = new JTextField(5);
        tfInterest.setLocation(0, 60);
        add(tfInterest);

        JTextArea tfAnswer = new JTextArea(50,10);
        tfAnswer.setLocation(0, 110);
        add(tfAnswer);

        // Radio buttons
        bgSelect = new ButtonGroup();

        rbPmts36 = new JRadioButton();
        rbPmts36.setText("36 Payments");
        rbPmts36.setLocation(0, 80);
        bgSelect.add(rbPmts36);
        add(rbPmts36);

        bgSelect.add(rbPmts48);
        rbPmts48.setText("48 Payments");
        rbPmts48.setLocation(150, 80);
        rbPmts48 = new JRadioButton();
        add(rbPmts48);

        bgSelect.add(rbPmts60);
        rbPmts60.setText("60 Payments");
        rbPmts60.setLocation(300, 80);
        rbPmts60 = new JRadioButton();
        add(rbPmts60);

        setLayout(null);
        pack();
    } // CarPaymentCalc
}

最佳答案

不要使用null布局。完美的像素布局是现代UI设计中的一种错觉,您无法控制字体,DPI,渲染管线或其他因素,这些因素会改变组件在屏幕上的渲染方式。

Swing旨在与布局经理一起克服这些问题。如果您坚持不理会这些功能并反对API设计,请做好准备以应对许多麻烦,并且永不结束艰苦的工作。

通过查看JavaDocs for pack ...


  使此窗口的大小适合所需的大小和布局
  它的子组件。窗口的最终宽度和高度为
  如果任一尺寸小于
  上一次调用setMinimumSize所指定的最小大小
  方法。如果窗口和/或其所有者不可显示
  但是,在计算
  首选大小。在确定窗口大小之后,对其进行验证
  计算。


您将注意到,pack依赖于布局管理器API来确定框架内容的首选可见尺寸。通过将布局管理器设置为null,可以防止它确定此信息,因此,基本上,它什么也没做。

如果您的书告诉您使用null布局,请摆脱它,这不是在教您良好的习惯或做法。

请查看Laying Out Components Within a Container,以获取有关布局管理器以及如何使用它们的更多详细信息。

您遇到的其他问题:

在完成构建UI之前调用setVisible(true);有时会阻止UI出现其预期的显示方式。您可以在框架上调用revalidate,但最后一次调用setVisible更简单。

setLocationRelativeTo使用的计算使用帧的当前大小,但是尚未设置。相反,您应该执行以下操作:

public CarPaymentCalc() {
    //...build UI here with appropriate layout managers...

    pack();
    setLocationRelativeTo(null);
    setVisible(true);
}


除了不向框架本身添加任何功能之外,我还不鼓励您直接从顶级容器(例如JFrame)进行扩展,这会阻止您以后重新使用IU。

最好从JPanel开始,然后将其添加到您想要的任何内容中,但这就是我。

10-05 18:09