如何使用两个带标题的边框包围单独的组件?

How do I use two titled borders to surround separate components?

大家好,我目前正在努力完成学校的作业,但我被卡住了,这里没有任何内容可以直接与我的问题相关。任务是创建一个 GUI 应用程序,显示客户访问 Joe's 的总金额。你们中的一些人可能从学校知道这个问题。 https://bb1ap.tamut.edu/bbcswebdav/pid-281014-dt-content-rid-800291_1/courses/80706_15/week08_activity_25-27.pdf 附件 pdf 的底部是我遇到的问题和成品应该是什么样子的图像。我似乎无法创建两个单独的标题边框。非常感谢对此的任何帮助。如果你能像我是五年级学生那样解释就更好了!

//Programmer: Davis Bentley
//Date: 10/21/2015
//Purpose: To allow user to calculate auto service cost

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

public class autoCalc extends JFrame {

private JPanel topPanel;
private JPanel centerPanel;
private JLabel messageLabel1, messageLabel2;
private JTextField autoTextField1, autoTextField2;
private JButton calcButton, exitButton;
private final int WINDOW_WIDTH = 450;
private final int WINDOW_HEIGHT = 600;
JCheckBox oil = new JCheckBox("Oil Change (.00)");
JCheckBox lube = new JCheckBox("Lube Job (.00)");
JCheckBox radiator = new JCheckBox("Radiator FLush(.00");
JCheckBox trans = new JCheckBox("Transmission FLush (.00)");
JCheckBox inspect = new JCheckBox("Inspection (.00)");
JCheckBox muffler = new JCheckBox("Muffler Replacement (0.00)");
JCheckBox tire = new JCheckBox("Tire Rotation (.00)");

public autoCalc(){
    super("Joe's Automotive");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildPanel();
    setVisible(true);
}
private void buildPanel(){

    JFrame frame = new JFrame("Joe's Automotive");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(topPanel);
    frame.add(centerPanel);

    topPanel = new JPanel();
    topPanel.setBorder(BorderFactory.createTitledBorder("Routine Services"));
    topPanel.add(oil);
    topPanel.add(lube);
    topPanel.add(radiator);
    topPanel.add(trans);
    topPanel.add(inspect);
    topPanel.add(muffler);
    topPanel.add(tire);

    messageLabel1 = new JLabel("Parts charges:");
    autoTextField1 = new JTextField(10);
    messageLabel2 = new JLabel("Hours of Labor:");
    autoTextField2 = new JTextField(10);
    calcButton = new JButton("Calculate");
    calcButton.addActionListener(new CalcButtonListener());
    exitButton = new JButton("Exit");
    exitButton.addActionListener(new exitButtonListener());

    centerPanel = new JPanel();
    centerPanel.setBorder(BorderFactory.createTitledBorder("Nonroutine Services"));
    centerPanel.add(messageLabel1);
    centerPanel.add(autoTextField1);
    centerPanel.add(messageLabel2);
    centerPanel.add(autoTextField2);
    centerPanel.add(calcButton);
    centerPanel.add(exitButton);
}
private class CalcButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e){
        String input, input2;
        double parts, hours, labor;
        double grandTotal = 0;

        if ( oil.isSelected()){
            grandTotal += 26;
        }
        if ( lube.isSelected()){
            grandTotal += 18;
        }
        if ( radiator.isSelected()){
            grandTotal += 30;
        }
        if ( trans.isSelected()){
            grandTotal += 80;
        }
        if ( inspect.isSelected()){
            grandTotal += 15;
        }
        if ( muffler.isSelected()){
            grandTotal += 100;
        }
        if ( tire.isSelected()){
            grandTotal += 20;
        }
        {
            input = autoTextField1.getText();
            parts = Double.parseDouble(input);
            input2 = autoTextField2.getText();
            hours = Double.parseDouble(input2);
            labor = hours * 20;
            grandTotal += parts + labor;
        }

        JOptionPane.showMessageDialog(null, "Total Charges: $" + grandTotal );
    }
}
private class exitButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e){
        dispose();
    }
}

}

这是 运行 的代码:

//Programmer: Davis Bentley
//Date: 10/21/2015
//Purpose: To allow user to calculate auto service cost

public class autoRun {

public static void main(String[] args) {
    autoCalc auto = new autoCalc();
}

}
frame.add(topPanel);
frame.add(centerPanel);
topPanel = new JPanel();

几个问题:

  1. 当您尝试将它们添加到框架时,您的 topPanel 和 centerPanel 变量为空,因此您实际上没有向框架添加任何内容。创建面板,将组件添加到面板,然后将面板添加到框架。

  2. JFrame 的默认布局管理器是 BorderLayout。当您将组件添加到框架但不指定约束时,两个组件都会添加到中心。但是,只能在中心显示一个组件。

尝试:

frame.add(topPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);

并将这些语句移至 buildPanel() 方法的底部。

阅读有关 Using Layout Managers 的 Swing 教程部分,了解更多信息和工作示例。

保留 link 教程以方便学习 Swing 基础知识。