大家好,我目前正在尝试为学校完成作业,但我已经陷入困境,这里没有任何与我的问题直接相关的内容。该任务是创建一个GUI应用程序,显示客户访问Joe的总数。有些人可能从学校知道这个问题。 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 ($26.00)");
JCheckBox lube = new JCheckBox("Lube Job ($18.00)");
JCheckBox radiator = new JCheckBox("Radiator FLush($30.00");
JCheckBox trans = new JCheckBox("Transmission FLush ($80.00)");
JCheckBox inspect = new JCheckBox("Inspection ($15.00)");
JCheckBox muffler = new JCheckBox("Muffler Replacement ($100.00)");
JCheckBox tire = new JCheckBox("Tire Rotation ($20.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();
}
}
答案 0 :(得分:0)
frame.add(topPanel);
frame.add(centerPanel);
topPanel = new JPanel();
有几个问题:
当您尝试将topPanel和centerPanel变量添加到框架时,它们为null,因此您实际上不向框架添加任何内容。创建面板,向面板添加组件,然后将面板添加到框架中。
JFrame的默认布局管理器是BorderLayout。向框架添加组件并且未指定约束时,两个组件都将添加到CENTER中。但是,中心只能显示一个组件。
尝试:
frame.add(topPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);
并将这些陈述移至buildPanel()
方法的底部。
阅读Using Layout Managers上Swing教程中的部分,了解更多信息和工作示例。
保留教程的链接,以便学习Swing基础知识。