Java 如何将另一个类中的数据添加到JTextField
我有一个小工具、手机、MP3和小工具商店的课程。Gadget是超类,Mobile和MP3是超类的子类,GadgetShop是我制作的GUI 我只需要我的GadgetShop收集信息,并将其放置在我单击按钮时创建的文本字段中。我不知道如何传递信息 顺便说一下,我用的是BlueJ 这是我的手机课:Java 如何将另一个类中的数据添加到JTextField,java,swing,jtextfield,Java,Swing,Jtextfield,我有一个小工具、手机、MP3和小工具商店的课程。Gadget是超类,Mobile和MP3是超类的子类,GadgetShop是我制作的GUI 我只需要我的GadgetShop收集信息,并将其放置在我单击按钮时创建的文本字段中。我不知道如何传递信息 顺便说一下,我用的是BlueJ 这是我的手机课: /**Mobile phone class that: * allows calling credit to be applied that is more than 0 * shows remain
/**Mobile phone class that:
* allows calling credit to be applied that is more than 0
* shows remaining calling credit
* allows a phone number to be added
* shows duration of phone call
*/
public class Mobile extends Gadget
{
private int credit;
private int duration;
private String number = "";
/**
* Constructor for objects of class Mobile.
*/
public Mobile(double ThePrice, int TheWeight, String TheModel, String TheSize)
{
// initialise instance variables
super(ThePrice, TheWeight, TheModel, TheSize);
}
/**
* Insert the duration of the call.
*/
public void insertDuration(int length)
{
System.out.println("A duration of " + length + " minutes for the phone call has been inserted!");
duration = length;
}
/**
* Insert the phone number.
*/
public void insertNumber(String numberAdder)
{
System.out.println("The phone number " + numberAdder + " has been inserted!");
number = numberAdder;
}
/**
* Insert credit for the call.
* A positive amount will need to be added in order for successfull credit to be applied,
* otherwise an error message is displayed.
*/
public void insertCredit(int calls)
{
if(calls > 0) {
System.out.println("Successfully added " + calls + " pounds of credit!");
credit = credit + calls;
}
else {
System.out.println("You need to insert a positive amount more than " + credit + " pounds.");
}
}
/**
* Make the phone call.
* If the credit is equal to or more than the duration, a message displays the phone number and duration of the call.
* If the credit is 0, a message will be displayed to insert more than 0.
* The number of minutes the call lasted is reduced by the amount that was available.
*/
public void makePhoneCall()
{
if(credit == 0)
System.out.println("Insert more than 0 credit to make a phone call!");
else {
if(credit >= duration) {
System.out.println("The phone number " + number + " has been dialed and is being called for " + duration + " minutes");
credit = credit - duration;
duration = 0;
}
else {
if(credit < duration)
System.out.println("You do not have enough credit to make a phone call! Your credit is " + credit + " pounds");
}
}
}
public void mobilePrint()
/**
* Print the details of the mobile.
**/
{
System.out.println("The price of the mobile is " + price + " pounds");
System.out.println("The weight is " + weight + " grams");
System.out.println("The model is " + model);
System.out.println("The size is " + size);
}
}
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class GadgetShop implements ActionListener
{
private JTextField model, price, weight, size, credit, memory, phoneNo, duration, download, displayNumber;
private JButton addMobile, addMP3, clear, displayAll;
//These JTextField's are for the labels
private JTextField model2, price2, weight2, size2, credit2, memory2, phoneNo2, duration2, download2, displayNumber2;
private JFrame frame;
private ArrayList<Gadget> gadgetDetails;
public GadgetShop()
{
makeFrame();
}
public void addGadget(Gadget newGadget)
{
ArrayList<Gadget> GadgetList = new ArrayList<Gadget>();
Gadget Object = new Gadget(1.00,20,"model","big");
GadgetList.add(Object);
model.setText("hi");
}
public void makeFrame()
{
frame = new JFrame("Gadget Shop");
Container contentPane = frame.getContentPane();
model = new JTextField(10);
price = new JTextField(10);
weight = new JTextField(10);
size = new JTextField(10);
credit = new JTextField(10);
memory = new JTextField(10);
phoneNo = new JTextField(10);
duration = new JTextField(10);
download = new JTextField(10);
displayNumber = new JTextField(10);
//Display Model text box and model info
model2 = new JTextField("Model:");
contentPane.add(model2);
model2.setOpaque(false);
contentPane.add(model);
price2 = new JTextField("Price:");
contentPane.add(price2);
price2.setOpaque(false);
contentPane.add(price);
weight2 = new JTextField("Weight:");
contentPane.add(weight2);
weight2.setOpaque(false);
contentPane.add(weight);
size2 = new JTextField("Size:");
contentPane.add(size2);
size2.setOpaque(false);
contentPane.add(size);
credit2 = new JTextField("Credit:");
contentPane.add(credit2);
credit2.setOpaque(false);
contentPane.add(credit);
memory2 = new JTextField("Memory:");
contentPane.add(memory2);
memory2.setOpaque(false);
contentPane.add(memory);
phoneNo2 = new JTextField("Phone Number:");
contentPane.add(phoneNo2);
phoneNo2.setOpaque(false);
contentPane.add(phoneNo);
duration2 = new JTextField("Duration:");
contentPane.add(duration2);
duration2.setOpaque(false);
contentPane.add(duration);
download2 = new JTextField("Download:");
contentPane.add(download2);
download2.setOpaque(false);
contentPane.add(download);
displayNumber2 = new JTextField("Display Number:");
contentPane.add(displayNumber2);
displayNumber2.setOpaque(false);
contentPane.add(displayNumber);
addMobile = new JButton("Add Mobile Number");
contentPane.add(addMobile);
addMobile.addActionListener(this);
addMP3 = new JButton("Add MP3 Song");
contentPane.add(addMP3);
addMP3.addActionListener(this);
clear = new JButton("Clear");
contentPane.add(clear);
clear.addActionListener(this);
displayAll = new JButton("Display All");
contentPane.add(displayAll);
displayAll.addActionListener(this);
contentPane.setLayout (new GridLayout(4,4));
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (command.equals("Add Mobile Number")) {
addMobile();
}
}
public void addMobile()
{
model.setText("");
}
public void clearText()
{
model.setText("");
price.setText("");
weight.setText("");
size.setText("");
credit.setText("");
memory.setText("");
phoneNo.setText("");
duration.setText("");
download.setText("");
displayNumber.setText("");
}
}
我想添加手机的详细信息类似地,您可以在
addGadget
方法中将Mobile
参数添加到addMobile
public void addMobile(Mobile mobile)
{
// You should add getter methods for attribute
//in the Mobile class to get meaningful values here instead of toString
model.setText(mobile.toString());
}
无需在每个
JTextField
中调用setOpaque(false)
。actionPerformed
应该是if(event.getSource().equals(addMobile){//Your code here}
…但是,即使这样,我仍然不确定你的问题是什么。为了更好地帮助你,尽快发布一个有效的、2JTextField
足以演示你的GUI,MCVE不是一个代码片段或你的全部代码,而是一个小的、可运行的示例,它包括一个main
方法和你的导入,我们可以复制粘贴、编译并运行w/o mo我们为查看您的issueVariable名称而进行的修改不应以大写字符开头。有些名称是正确的。有些名称是不正确的。请保持一致!!!@Frakcool不是用于删除背景的设置不透明,因此它们都需要这样做?如果在JTex中使用文本字符串时效果良好,为什么不能执行操作呢t字段作为测试?@Bob my bad,我被搞糊涂了,我以为你是想让它们不透明……我认为比较对象比比较字符串更好,因为你可能无法正确编写命令(我是指输入错误),正如suggestion@Frakcool是的,我只想知道如何从我的移动类中获取信息,因此当它有信息时,我的“添加移动”按钮将显示它包含的详细信息。我该如何做?
public void addMobile(Mobile mobile)
{
// You should add getter methods for attribute
//in the Mobile class to get meaningful values here instead of toString
model.setText(mobile.toString());
}