【发布时间】:2014-09-01 07:25:32
【问题描述】:
我是SO新手,我用java设计了一个表单,我添加了三个按钮提交、清除和显示记录。我需要获取有关提交事件的数据并将其传递给 txt 文件,并且我想在显示记录按钮单击事件中显示整个记录。我需要你们快速修复,我的时间不多了。这是我的代码:
import java.awt.*; //import older gui library for content pane
import javax.swing.*; //import newer gui library for labels, textfields, and button
import java.awt.event.*; //import gui event action library
import javax.swing.JRadioButton;
public class CustomerRecord extends JFrame implements ActionListener {
// declare labels used on GUI screen
private JLabel labelId, labelName, labelGender,labelcategory, labelPItem, labeldiscount;
private JLabel labelError, labelRegistration;
private JTextField textId, textName, textGender, textState, textcategory, textPItem, textdiscount;
// declare button used on GUI screen
private JButton submitButton, clearButton, readButton;
final JRadioButton jRadioMale = new JRadioButton("Male");
final JRadioButton jRadioFemale = new JRadioButton("Female");
// declare content pane
private Container contentPane;
public CustomerRecord() {
createGUI();
} // ends constructor
private void createGUI() {
try {
// get content pane and set the layout to null
contentPane = getContentPane();
contentPane.setLayout(null); //free-form layout
setFont(new Font("TimesRoman", Font.ITALIC, 14));
// create the name label
labelId = new JLabel(); //instantiate new JLabel
labelId.setText("C.ID"); //set label text to name
labelId.setLocation(38, 10); //set location of JLabel
labelId.setSize(200, 25); //set size of JLabel
labelId.setForeground(Color.BLACK);//set initial background color
contentPane.add(labelId); //add JLabel to content pane
// create the name text box
textName = new JTextField(); //instantiate new JTextField
textName.setText(""); //clear JTextField
textName.setToolTipText("Please enter ID");
textName.setLocation(75, 10); //set location of JTextFfield
textName.setSize(200, 25); //set size of JTextField
contentPane.add(textName); //add jextfield to content pane
// create the address label
labelName = new JLabel();
labelName.setText("Name:");
labelName.setLocation(23, 50);
labelName.setSize(80, 25);
labelName.setForeground(Color.BLACK);
contentPane.add(labelName);
// create the address text box
textName = new JTextField();
textName.setText("");
textName.setToolTipText("Please type in full name");
textName.setLocation(75, 50);
textName.setSize(300, 25);
contentPane.add(textName);
labelGender = new JLabel();
labelGender.setText("Gender");
labelGender.setLocation(30, 90);
labelGender.setSize(300, 25);
labelGender.setForeground(Color.BLACK);
contentPane.add(labelGender);
textGender = new JTextField();
textGender.setText("");
textGender.setToolTipText("M/F");
textGender.setLocation(75, 90);
textGender.setSize(130, 25);
contentPane.add(textGender);
labelcategory = new JLabel();
labelcategory.setText("Category");
labelcategory.setLocation(18, 170);
labelcategory.setSize(300, 25);
labelcategory.setForeground(Color.BLACK);
contentPane.add(labelcategory);
textcategory = new JTextField();
textcategory.setText("");
textcategory.setToolTipText("Item Type (Grocery)");
textcategory.setLocation(75, 170);
textcategory.setSize(130, 25);
contentPane.add(textcategory);
labelPItem = new JLabel();
labelPItem.setText("Total Item");
labelPItem.setLocation(15, 210);
labelPItem.setSize(250, 25);
labelPItem.setForeground(Color.BLACK);
contentPane.add(labelPItem);
textPItem = new JTextField();
textPItem.setText("");
textPItem.setToolTipText("Purchased items must be between start with 1 or 70");
textPItem.setLocation(75, 210);
textPItem.setSize(130, 25);
contentPane.add(textPItem);
labeldiscount = new JLabel();
labeldiscount.setText("Discount");
labeldiscount.setLocation(18, 250);
labeldiscount.setSize(300, 25);
labeldiscount.setForeground(Color.BLACK);
contentPane.add(labeldiscount);
textdiscount = new JTextField();
textdiscount.setText("");
textdiscount.setToolTipText("Entered Value must be containing ' % '");
textdiscount.setLocation(75, 250);
textdiscount.setSize(130, 25);
contentPane.add(textdiscount);
submitButton = new JButton();
submitButton.setText("Submit");
submitButton.setToolTipText("Click \"submit \" when the form is completely filled out");
submitButton.setLocation(125, 450);
submitButton.setSize(100, 30);
contentPane.add(submitButton);
submitButton.addActionListener(this);
readButton = new JButton();
readButton.setText("Show Records");
readButton.setToolTipText("Click Show Record if you want to check record");
readButton.setLocation(225, 350);
readButton.setSize(200, 30);
contentPane.add(readButton);
readButton.addActionListener(this);
clearButton = new JButton();
clearButton.setText("Clear");
clearButton.setToolTipText("Click \"clear \" when you want to clear the form");
clearButton.setLocation(250, 450);
clearButton.setSize(100, 30);
contentPane.add(clearButton);
clearButton.addActionListener(this);
// create the error label
labelError = new JLabel();
labelError.setText("Please correct items in red");
labelError.setLocation(150, 500);
labelError.setSize(190, 25);
labelError.setForeground(Color.RED);
labelError.setVisible(false);
contentPane.add(labelError);
// create the registration label
labelRegistration = new JLabel();
labelRegistration.setText("Thank you for your entry.");
labelRegistration.setLocation(145, 500);
labelRegistration.setSize(190, 25);
labelRegistration.setForeground(Color.BLACK);
labelRegistration.setVisible(false);
contentPane.add(labelRegistration);
setTitle("Customer Form"); //set window title
setSize(475, 600); //set window size
setVisible(true);
} catch (Exception e) {
}
}// ends creatGUI method.
public static void main(String args[]) {
CustomerRecord application = new CustomerRecord();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}// ends main
public void actionPerformed(ActionEvent event) {
try {
if (event.getActionCommand().equals("Submit")) {
if (checkID() & checkName() & checkGender() & check() & checkPItem() & checkdiscount()) {
labelRegistration.setVisible(true);
labelError.setVisible(false);
submitButton.removeActionListener(this);
clearButton.removeActionListener(this);
} else {
labelError.setVisible(true);
labelRegistration.setVisible(false);
}
} if (event.getActionCommand().equals("Clear"))
{
textName.setText("");
textName.setText("");
textGender.setText("");
textState.setText("");
textcategory.setText("");
textPItem.setText("");
textdiscount.setText("");
labelError.setVisible(false);
labelRegistration.setVisible(false);
labelId.setForeground(Color.BLACK);
labelName.setForeground(Color.BLACK);
labelGender.setForeground(Color.BLACK);
labelcategory.setForeground(Color.BLACK);
labelPItem.setForeground(Color.BLACK);
labeldiscount.setForeground(Color.BLACK);
}
} catch (Exception e) { }
}
private boolean checkID() {
if (textName.getText().length() == 0) {
labelId.setForeground(Color.RED); //name is not correct
return false;
} else {
labelId.setForeground(Color.BLACK); //name is correct
return true;
}
}
private boolean checkName() {
if (textName.getText().length() < 5) {
labelName.setForeground(Color.RED);
return false;
} else {
labelName.setForeground(Color.BLACK);
return true;
}
}
private boolean checkGender() {
if (textGender.getText().length() == 0) {
labelGender.setForeground(Color.RED);
return false;
} else {
labelGender.setForeground(Color.BLACK);
return true;
}
}
private boolean check() {
try {
if (textcategory.getText().length() == 5) {
labelcategory.setForeground(Color.BLACK);
return true;
} else {
labelcategory.setForeground(Color.RED);
return false;
}
} catch (Exception e) {
labelcategory.setForeground(Color.RED);
return false;
}
}
private boolean checkPItem() {
if (textPItem.getText().startsWith("1") || textPItem.getText().startsWith("70"))
{
labelPItem.setForeground(Color.BLACK);
return true;
} else {
labelPItem.setForeground(Color.RED);
return false;
}
}
private boolean checkdiscount() {
if (textdiscount.getText().contains("%"))
{
labeldiscount.setForeground(Color.BLACK);
return true;
} else
{
labeldiscount.setForeground(Color.RED);
return false;
}
}
}
【问题讨论】:
-
最好的开始是分离你的代码。理想情况下,您将拥有在单独的层中处理 txt 文件(读取、写入......)的代码,但至少在不同的 java 类中。这将使它更容易阅读,更不用说测试了。您还可以对类进行单元测试,而不必牢记 UI。另外:当心这样的代码:catch (Exception e) { } 隐藏异常(尤其是在开发过程中)是你能做的最糟糕的事情。
-
探索
java.io包。 -
避免使用
null布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正 -
这个问题似乎离题了,因为它是关于我们弥补 OP 糟糕的时间管理技能。