[英]Is there a way to call a method from a separate class in a Java GUI?
我正在為一個班級做一個項目。 該項目要求我創建 3 個類,一個父類和兩個子類,每個類有 3 個方法。 然后它要求我創建一個 GUI,該 GUI 將根據幾個字段(包括文本和單選按鈕)計算銷售稅。 在我的腦海中,如果單擊某個單選按鈕,我會調用我的方法運行,即如果單擊 Hybrid 按鈕,它將運行 Hybrid 類中的 toString。 有人可以幫我弄清楚如何做到這一點嗎?
package projecttwo;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javafx.scene.control.ToggleGroup;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import projectTwo.Automobile;
import projectTwo.Electric;
import projectTwo.Hybrid;
/**
* CMIS242
* November 14, 2020
* Sabrina Riley
* This program computes the sales tax for a collection of automobiles
*/
public class ProjectTwo extends JFrame implements ActionListener {
/**
* @param args the command line arguments
*/
//Variable declaration
private String select;
private JFrame frame;
private JPanel panel;
private JLabel label, label2, label3, label4, label5;
private JFormattedTextField mmText, salesText, mpgText, weightText, compute;
private JButton button, button2, button3;
private JRadioButton hybrid, electric, other;
private ButtonGroup bg;
private JPanel radioPanel;
Automobile car = new Automobile();
public ProjectTwo() {
//initialize GUI
frame = new JFrame();
//HandlerClass handler = new HandlerClass();
//add compute sales tax button
button = new JButton("Compute Sales Tax");
button.setBounds(0,100,150,50);
add(button);
button.addActionListener(this);
//add display results button
button2 = new JButton("Display Results");
button2.setBounds(0,200, 150, 50);
button2.addActionListener(this);
//add clear fields button
button3 = new JButton("Clear Fields");
button3.setBounds(200,200, 150, 50);
button3.addActionListener(this);
//add text field to display results from compute sales button
compute = new JFormattedTextField();
compute.setBounds(200,100,150,50);
compute.setEditable(false);
//add all other text fields
mmText = new JFormattedTextField();
mmText.addKeyListener(null);
//mmText.addActionListener(this);
salesText = new JFormattedTextField();
mpgText = new JFormattedTextField();
weightText = new JFormattedTextField();
//add radio buttons to pick between hybrid, electric, and other
hybrid = new JRadioButton("Hybrid", true);
electric = new JRadioButton("Electric", false);
other = new JRadioButton("Other", false);
//group the radio buttons together
bg = new ButtonGroup();
bg.add(hybrid);
bg.add(electric);
bg.add(other);
//arrange buttons vertically
radioPanel = new JPanel();
radioPanel.setLayout(new GridLayout(3,1));
//radioPanel.setBounds(100,200,150,200);
radioPanel.add(hybrid);
radioPanel.add(electric);
radioPanel.add(other);
setContentPane(radioPanel);
pack();
//make them listen to clicks
hybrid.addActionListener(this);
electric.addActionListener(this);
other.addActionListener(this);
//label all text fields
label = new JLabel("Make and Model");
label.setBounds(0, 0, 100, 150);
label2 = new JLabel("Sales Price");
label3 = new JLabel("Automobile Type");
label4 = new JLabel("Miles Per Gallon");
label5 = new JLabel("Weight in Pounds");
//implement panel with all labels, text fields, buttons, and radio buttons
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
//panel.setLayout(GridLayout(0,2));
panel.setLayout(new GridLayout(0,2));
panel.setSize(500, 500);
panel.add(label);
panel.add(mmText);
panel.add(label2);
panel.add(salesText);
panel.add(hybrid);
panel.add(electric);
panel.add(other);
panel.add(label3);
panel.add(mpgText);
panel.add(label4);
panel.add(weightText);
panel.add(label5);
panel.add(button);
panel.add(compute);
panel.add(button2);
panel.add(button3);
panel.add(radioPanel);
//set frame dimensions
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Automobile Sales Tax Calculator");
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String user = mmText.getText();
String sales = salesText.getText();
String mpg = mpgText.getText();
String weight = weightText.getText();
this.clearFields();
e.getActionCommand();
if(e.getSource()==hybrid) {
System.out.println("car is a hybrid");
} else if (e.getSource()==electric){
System.out.println("car is electric");
} else
System.out.println("car is basic");
System.out.println(", " + user + sales + ", " + weight + ", " + mpg);
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void clearFields() {
mmText.setText(null);
salesText.setText(null);
mpgText.setText(null);
weightText.setText(null);
compute.setText(null);
hybrid.setSelected(false);
electric.setSelected(false);
other.setSelected(false);
}
public static void main(String[] args) {
// TODO code application logic here
new ProjectTwo();
}
}
package projectTwo;
/**
* CMIS242
* November 14, 2020
* Sabrina Riley
* This program computes the sales tax for a collection of automobiles
*/
public class Automobile {
protected String make;
protected String model;
protected double salesPrice;
protected double salesTax;
public Automobile() {
make = "brand";
model = "type";
}
//A constructor that allows the make and purchase price to be initialized
public Automobile(String make, String model, double salesPrice, double salesTax) {
this.make = make;
this.model = model;
this.salesPrice = salesPrice;
this.salesTax = salesTax;
}
//A method that returns the base sales tax computed as 5% of the sales price
public double salesTax() {
salesTax = salesPrice * 0.05;
return salesTax;
}
//A toString method that returns a string containing the make and model of the
//automobile, the sales price, and the sales tax
public String toString() {
return make + model + salesTax;
}
}
package projectTwo;
/**
* CMIS242
* November 14, 2020
* Sabrina Riley
* This program computes the sales tax for a collection of automobiles
*/
public class Hybrid extends Automobile {
protected int mpg;
protected double discount;
//A constructor that allows the make and purchase price to be initialized
public Hybrid(int mpg) {
super();
mpg = 0;
}
public Hybrid(String make, String model, double salesPrice, double salesTax, int mpg) {
super(make, model, salesPrice, salesTax);
this.mpg = mpg;
}
//A method that returns the base sales tax computed as 5% of the sales price
@Override
public double salesTax() {
if (mpg < 40)
discount = salesTax + 100;
else
discount = salesTax + 100 + (mpg * 2);
return discount;
}
//A toString method that returns a string containing the make and model of the
//automobile, the sales price, and the sales tax
@Override
public String toString() {
return make + model + salesPrice + discount + discount;
}
}
以我的拙見,如果正確理解您想要實現的目標,您必須在 ProjectTwo 的構造函數中創建一個“Hybrid”類的實例,就像:
在那里聲明變量,你有所有的變量聲明,只是為了讓它成為全局的:
private Hybrid hybr;
在構造函數中創建實例:
hybr = new Hybrid("make", "hybrid's model", 20500.50, 12.5, 5);
然后在您的代碼中,您需要使用 Hybrid 的方法調用您的方法,在本例中為hybr.toString()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.