JTextField

i mport javax.swing.*;


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TextFieldFrame extends JFrame {
private JTextField a;
private JTextField b;
private JTextField c;


private JPasswordField password;


public TextFieldFrame ()
{   
super("testing ******");
setLayout(new FlowLayout());
a=new JTextField(10);

add(a);//添加到JFRame,10列


b=new JTextField("enter here");

add(b);


c=new JTextField("this three$$$",10);
c.setEditable(false);//不可编辑

add(c);


password=new JPasswordField("hideer password");

add(password);


TextListener  hinder=new TextListener(); //创建对象
a.addActionListener(hinder);//注册事件处理器,并且当客户按回车键的时候出现焦点,产生event 事件,,此事件会交给ActionListener 处理
b.addActionListener(hinder);
c.addActionListener(hinder);
password.addActionListener(hinder);




             }
private class TextListener implements ActionListener//创建监听事件,私有类
{


@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
String s=" ";
if(event.getSource()==a)//如果event事件源是a
{
s=String.format("aaaaa is:%s ", event.getActionCommand());
}

else if(event.getSource()==b)
{
s=String.format("bbbbb is:%s ", event.getActionCommand());
}
else if(event.getSource()==c)
{
s=String.format("ccccc is:%s ", event.getActionCommand());

}
else if(event.getSource()==password)
{
s=String.format("password is:%s ", event.getActionCommand());

}
JOptionPane.showInputDialog(null,s);

}

}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I understand your question. You want me to write a class named ProductSystem, which extends JFrame class and performs the following tasks: 1. The appearance of the application should look like the following (as shown in Figure 1). 2. At the beginning, input name, price and number at three JTextField fields, then click “Add” button (event source), which binds to a method (event listener) that creates a Product object (using name, price, and number) and adds it to ArrayList<Product>, at the same time display “Add Product xxx success!” in JTextArea on the GUI (as shown in Figure 2). 3. When click “Display” button, then read the records from ArrayList, and display the records in JTextArea on the GUI (as shown in Figure 3). 4. When click “Save to File” button, then write the records from ArrayList to file product.txt, at the same time display "file write success" in JTextArea on the GUI (as shown in Figure 4). Here is the code for the ProductSystem class: ```java import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; class Product { private String name; private double price; private int number; public Product(String name, double price, int number) { this.name = name; this.price = price; this.number = number; } public String getName() { return name; } public double getPrice() { return price; } public int getNumber() { return number; } public String toString() { return String.format("%-20s $%.2f %d", name, price, number); } } public class ProductSystem extends JFrame implements ActionListener { private ArrayList<Product> productList = new ArrayList<>(); private JLabel nameLabel = new JLabel("Name:"); private JTextField nameField = new JTextField(20); private JLabel priceLabel = new JLabel("Price:"); private JTextField priceField = new JTextField(20); private JLabel numberLabel = new JLabel("Number:"); private JTextField numberField = new JTextField(20); private JButton addButton = new JButton("Add"); private JButton displayButton = new JButton("Display"); private JButton saveButton = new JButton("Save to File"); private JTextArea textArea = new JTextArea(10, 30); public ProductSystem() { setTitle("Product System"); setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.anchor = GridBagConstraints.WEST; constraints.insets = new Insets(10, 10, 10, 10); constraints.gridx = 0; constraints.gridy = 0; panel.add(nameLabel, constraints); constraints.gridx = 1; panel.add(nameField, constraints); constraints.gridx = 0; constraints.gridy = 1; panel.add(priceLabel, constraints); constraints.gridx = 1; panel.add(priceField, constraints); constraints.gridx = 0; constraints.gridy = 2; panel.add(numberLabel, constraints); constraints.gridx = 1; panel.add(numberField, constraints); constraints.gridx = 0; constraints.gridy = 3; constraints.gridwidth = 2; constraints.anchor = GridBagConstraints.CENTER; panel.add(addButton, constraints); constraints.gridx = 0; constraints.gridy = 4; constraints.gridwidth = 2; panel.add(displayButton, constraints); constraints.gridx = 0; constraints.gridy = 5; constraints.gridwidth = 2; panel.add(saveButton, constraints); add(panel, BorderLayout.NORTH); JScrollPane scrollPane = new JScrollPane(textArea); add(scrollPane, BorderLayout.CENTER); addButton.addActionListener(this); displayButton.addActionListener(this); saveButton.addActionListener(this); } public void actionPerformed(ActionEvent event) { if (event.getSource() == addButton) { String name = nameField.getText(); double price = Double.parseDouble(priceField.getText()); int number = Integer.parseInt(numberField.getText()); Product product = new Product(name, price, number); productList.add(product); textArea.append(String.format("Add Product %s success!\n", product.getName())); } else if (event.getSource() == displayButton) { textArea.setText(""); for (Product product : productList) { textArea.append(product.toString() + "\n"); } } else if (event.getSource() == saveButton) { try (PrintWriter out = new PrintWriter(new FileWriter("product.txt"))) { for (Product product : productList) { out.println(product.getName() + "," + product.getPrice() + "," + product.getNumber()); } textArea.append("File write success!\n"); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { ProductSystem system = new ProductSystem(); system.setVisible(true); } } ``` In the above code, the Product class represents a product with its name, price, and number. The ProductSystem class extends the JFrame class and implements the ActionListener interface to handle events from buttons. The GUI of the application is created using GridBagLayout and contains three text fields for name, price, and number, and three buttons for adding, displaying, and saving products. The JTextArea is used to display the results of adding, displaying, and saving products. When the user clicks the "Add" button, the actionPerformed() method reads the values from the text fields, creates a new Product object, adds it to the productList ArrayList, and displays the message "Add Product xxx success!" in the JTextArea. When the user clicks the "Display" button, the actionPerformed() method reads the records from the productList ArrayList and displays them in the JTextArea. When the user clicks the "Save to File" button, the actionPerformed() method writes the records from the productList ArrayList to a file named product.txt and displays the message "File write success!" in the JTextArea. I hope this code helps you out! Let me know if you have any questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值