-1

我创建了一个Address Book GUI,我只是不明白如何使保存和删除按钮起作用,因此当用户填写文本字段时,他们可以单击保存并将其保存到JList我创建的文件中,然后他们也可以从中删除。我该怎么做呢?

import javax.swing.*;

import java.awt.event.*; 

import java.awt.*;

public class AddressBook {

private JLabel lblFirstname,lblSurname, lblMiddlename,  lblPhone,
lblEmail,lblAddressOne, lblAddressTwo, lblCity, lblPostCode, picture;
private JTextField txtFirstName, txtSurname, txtAddressOne, txtPhone,
txtMiddlename, txtAddressTwo, txtEmail, txtCity, txtPostCode;
private JButton btSave, btExit, btDelete;
private JList contacts;
private JPanel panel;


public static void main(String[] args) {
    new AddressBook();
}

public AddressBook(){
    JFrame frame = new JFrame("My Address Book");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(900,400);
    frame.setVisible(true);


    panel = new JPanel();
    panel.setLayout(null);
    panel.setBackground(Color.cyan);


    lblFirstname = new JLabel("First name");
    lblFirstname.setBounds(135, 50, 150, 20);
    Font styleOne = new Font("Arial", Font.BOLD, 13);
    lblFirstname.setFont(styleOne);
    panel.add(lblFirstname);
    txtFirstName = new JTextField();
    txtFirstName.setBounds(210, 50, 150, 20);
    panel.add(txtFirstName);

    lblSurname = new JLabel ("Surname");
    lblSurname.setBounds(385,50,150,20);
    Font styleTwo = new Font ("Arial",Font.BOLD,13);
    lblSurname.setFont(styleTwo);
    panel.add(lblSurname);
    txtSurname = new JTextField();
    txtSurname.setBounds(450,50,150,20);
    panel.add(txtSurname);

    lblMiddlename = new JLabel ("Middle Name");
    lblMiddlename.setBounds(620,50,150,20);
    Font styleThree = new Font ("Arial", Font.BOLD,13);
    lblMiddlename.setFont(styleThree);
    panel.add(lblMiddlename);
    txtMiddlename = new JTextField();
    txtMiddlename.setBounds(710,50,150,20);
    panel.add(txtMiddlename);

    lblPhone = new JLabel("Phone");
    lblPhone.setBounds(160,100,100,20);
    Font styleFour = new Font ("Arial", Font.BOLD,13);
    lblPhone.setFont(styleFour);
    panel.add(lblPhone);
    txtPhone = new JTextField();
    txtPhone.setBounds(210,100,150,20);
    panel.add(txtPhone);

    lblEmail = new JLabel("Email");
    lblEmail.setBounds(410,100,100,20);
    Font styleFive = new Font ("Arial", Font.BOLD,13);
    lblEmail.setFont(styleFive);
    panel.add(lblEmail);
    txtEmail = new JTextField();
    txtEmail.setBounds(450,100,150,20);
    panel.add(txtEmail);

    lblAddressOne = new JLabel("Address 1");
    lblAddressOne.setBounds(145,150,100,20);
    Font styleSix = new Font ("Arial", Font.BOLD,13);
    lblAddressOne.setFont(styleSix);
    panel.add(lblAddressOne);
    txtAddressOne = new JTextField();
    txtAddressOne.setBounds(210,150,150,20);
    panel.add(txtAddressOne);

    lblAddressTwo = new JLabel("Address 2");
    lblAddressTwo.setBounds(145,200,100,20);
    Font styleSeven = new Font ("Arial", Font.BOLD,13);
    lblAddressTwo.setFont(styleSeven);
    panel.add(lblAddressTwo);
    txtAddressTwo = new JTextField();
    txtAddressTwo.setBounds(210,200,150,20);
    panel.add(txtAddressTwo);

    lblCity = new JLabel("City");
    lblCity.setBounds(180,250,100,20);
    Font styleEight = new Font ("Arial", Font.BOLD,13);
    lblCity.setFont(styleEight);
    panel.add(lblCity);
    txtCity = new JTextField();
    txtCity.setBounds(210,250,150,20);
    panel.add(txtCity);

    lblPostCode = new JLabel("Post Code");
    lblPostCode.setBounds(380,250,100,20);
    Font styleNine = new Font ("Arial", Font.BOLD,13);
    lblPostCode.setFont(styleNine);
    panel.add(lblPostCode);
    txtPostCode = new JTextField();
    txtPostCode.setBounds(450,250,150,20);
    panel.add(txtPostCode);

    //image
    ImageIcon image = new ImageIcon("C:\\Users\\Hassan\\Desktop\\icon.png");
    picture = new JLabel(image);
    picture.setBounds(600,90, 330, 270);
    panel.add(picture);

    //buttons
    btSave = new JButton ("Save");
    btSave.setBounds(380,325,100,20);
    panel.add(btSave);

    btDelete = new JButton ("Delete");
    btDelete.setBounds(260,325,100,20);
    panel.add(btDelete);

    btExit = new JButton ("Exit");
    btExit.setBounds(500,325,100,20);
    panel.add(btExit);
    btExit.addActionListener(new Action());

    //list
    contacts=new JList();
    contacts.setBounds(0,10,125,350);
    panel.add(contacts);

    frame.add(panel); 
    frame.setVisible(true);
}
//button actions
static class Action implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        JFrame option = new JFrame();
        int n = JOptionPane.showConfirmDialog(option, 
                "Are you sure you want to exit?", 
                "Exit?", 
                JOptionPane.YES_NO_OPTION);
        if(n == JOptionPane.YES_OPTION){
            System.exit(0);
        }

    }
}

}
4

2 回答 2

3

按钮需要事件处理程序才能工作。您尚未将任何事件处理程序添加到“保存”和“删除”按钮。您还需要在这些按钮上调用 addActionListener。我推荐匿名内部类:

mybutton.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent event) {
    //do whatever should happen when the button is clicked...
  }

});
于 2013-03-16T19:59:53.657 回答
0

你需要到addActionListener按钮btSavebtDelete。您可以像这样创建一个匿名类并在那里执行您的工作。

btSave.addActionListener(new ActionListener()
{
 public void actionPerformed(ActionEvent ae)
 {
   //Do you work for the button here
 }
}


btDelete.addActionListener(new ActionListener()
{
 public void actionPerformed(ActionEvent ae)
 {
   //Do you work for the button here
 }
}

编辑:

我有一个示例,您可以参考并通过理解它进行相应的更改。我是从我们研究所的一位教授那里得到的。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TooltipTextOfList{
    private JScrollPane scrollpane = null;
    JList list;
    JTextField txtItem;
    DefaultListModel model;
    public static void main(String[] args){
        TooltipTextOfList tt = new TooltipTextOfList();
    }

    public TooltipTextOfList(){
        JFrame frame = new JFrame("Tooltip Text for List Item");
        String[] str_list = {"One", "Two", "Three", "Four"};
        model = new DefaultListModel();
        for(int i = 0; i < str_list.length; i++)
            model.addElement(str_list[i]);
        list = new JList(model){
            public String getToolTipText(MouseEvent e) {
                int index = locationToIndex(e.getPoint());
                if (-1 < index) {
                    String item = (String)getModel().getElementAt(index);
                    return item;
                } else {
                    return null;
                }
            }
        };
        txtItem = new JTextField(10);
        JButton button = new JButton("Add");
        button.addActionListener(new MyAction());
        JPanel panel = new JPanel();
        panel.add(txtItem);
        panel.add(button);
        panel.add(list);
        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public class MyAction extends MouseAdapter implements ActionListener{
        public void actionPerformed(ActionEvent ae){
            String data = txtItem.getText();
            if (data.equals(""))
                JOptionPane.showMessageDialog(null,"Please enter text in the Text Box.");
            else{
                model.addElement(data);
                JOptionPane.showMessageDialog(null,"Item added successfully.");
                txtItem.setText("");
            }
        }
    }
}
于 2013-03-16T20:01:20.990 回答