我目前正在为我的Java类工作。它要求我们在文件中更改一些代码以添加joptionpane对话框而不是已存在的字段。以下是作业的确切措辞:
查看Flora的源代码。 java文件。在包含任何消息框的所有行的开头插入注释标记(//),但MessageBox声明语句除外。在注释掉的行的正下方,插入新代码以生成替换消息框的JOptionPane对话框。使用相同的标题,提示和按钮。不要忘记导入必要的Swing包。
我无法弄清楚如何添加一个JOptionPane框。我为joptionpane导入了swing,但我需要协助设置框
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class Flora extends Frame implements ActionListener
{
MessageBox savedBox;
MessageBox errorBox;
DataOutputStream output;
//Construct components
Panel dataFields = new Panel();
Panel firstRow = new Panel();
Panel secondRow = new Panel();
Panel thirdRow = new Panel();
Panel fourthRow = new Panel();
Panel fifthRow = new Panel();
Panel sixthRow = new Panel();
Panel buttonArea= new Panel();
Button newSticker = new Button("New Sticker");
Button renewal = new Button("Renewal");
//Label vinLabel = new Label("Enter Vehicle VIN number: ");
//TextField vin = new TextField(20);
//Label yearLabel = new Label("Year: ");
//TextField year = new TextField(4);
//Label makeLabel = new Label("Make: ");
//TextField make = new TextField(10);
//Label modelLabel = new Label("Model:");
//TextField model = new TextField(10);
//Label firstNameLabel = new Label("Enter First Name: ");
//TextField firstName = new TextField(15);
//Label lastNameLabel = new Label("Enter Last Name:");
//TextField lastName = new TextField(20);
//Label addressLabel = new Label("Enter Flora Address:");
//TextField address = new TextField (35);
public static void main(String[] args)
{
Flora window = new Flora();
window.setTitle("Flora City Stickers");
window.setSize(450, 250);
window.setVisible(true);
}
public Flora()
{
//Set background and layout managers
setBackground(Color.magenta);
setLayout(new BorderLayout());
dataFields.setLayout(new GridLayout(6,1));
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,2);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
thirdRow.setLayout(rowSetup);
fourthRow.setLayout(rowSetup);
fifthRow.setLayout(rowSetup);
sixthRow.setLayout(rowSetup);
buttonArea.setLayout(new FlowLayout());
//Add fields to rows
firstRow.add(vinLabel);
firstRow.add(yearLabel);
firstRow.add(makeLabel);
firstRow.add(modelLabel);
secondRow.add(vin);
secondRow.add(year);
secondRow.add(make);
secondRow.add(model);
thirdRow.add(firstNameLabel);
thirdRow.add(lastNameLabel);
fourthRow.add(firstName);
fourthRow.add(lastName);
fifthRow.add(addressLabel);
sixthRow.add(address);
//Add rows to panel
dataFields.add(firstRow);
dataFields.add(secondRow);
dataFields.add(thirdRow);
dataFields.add(fourthRow);
dataFields.add(fifthRow);
dataFields.add(sixthRow);
//Add buttons to panel
buttonArea.add(newSticker);
buttonArea.add(renewal);
//Add panels to frame
add(dataFields, BorderLayout.NORTH);
add(buttonArea, BorderLayout.SOUTH);
//Add functionality to buttons
newSticker.addActionListener(this);
renewal.addActionListener(this);
//Open the file
try
{
output = new DataOutputStream(new FileOutputStream("Sticker.dat"));
}
catch(IOException c)
{
System.exit(1);
}
//Construct window listener
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
String code;
if (arg == "New Sticker")
code = "N";
else
code = "R";
if (
(vin.getText().compareTo("")<1) ||
(year.getText().compareTo("")<1) ||
(make.getText().compareTo("")<1) ||
(model.getText().compareTo("")<1) ||
(firstName.getText().compareTo("")<1) ||
(lastName.getText().compareTo("")<1) ||
(address.getText().compareTo("")<1)
)
{
errorBox = new MessageBox(this, "Data Entry Error", "You must complete all fields.");
errorBox.setVisible(true);
}
else
{
try
{
output.writeUTF(code);
output.writeUTF(vin.getText());
output.writeUTF(year.getText());
output.writeUTF(make.getText());
output.writeUTF(model.getText());
output.writeUTF(firstName.getText());
output.writeUTF(lastName.getText());
output.writeUTF(address.getText());
savedBox = new MessageBox(this, "Data Submitted", "The vehicle information has been saved.");
savedBox.setVisible(true);
}
catch(IOException c)
{
System.exit(1);
}
clearFields();
}
}
public void clearFields()
{
//Clear fields and reset the focus
vin.setText("");
year.setText("");
make.setText("");
model.setText("");
firstName.setText("");
lastName.setText("");
address.setText("");
vin.requestFocus();
}
}
我知道他们希望原来的盒子被注释掉了,我想我做到了但是我不确定我是否已经得到了所有东西。感谢您提供的任何帮助。
答案 0 :(得分:2)
指令声明“在涉及任何消息框的所有行的开头插入注释标记(//),但MessageBox声明语句”除外。您已注释掉所有Jlabels
和JTextFields
。我认为这不是教学的一部分。
您正在寻找所有saveBox
和errorBox
除声明MessageBox savedBox;
MessageBox errorBox;
所以这些都是你想要评论的内容。
savedBox = new MessageBox(this, "Data Submitted", "The vehicle information has been saved.");
saveBox.setVisible(true);
errorBox = new MessageBox(this, "Data Entry Error", "You must complete all fields.");
errorBox.setVisible(true);
我相信你应该用JOptionPane
JOptionPane.showInputDialog(null, "Data Entry Error", "You must complete all fields.");