java 我如何让按钮工作?Java编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15453899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:39:34  来源:igfitidea点击:

How Do I get the buttons to work? Java Programming

javaswingjbutton

提问by ??? Chaudhry

I created an Address Book GUI, I just don't understand How to make the save and delete buttons to work, so when the user fills the text fields they can click save and it saves to the JListI have created and then they can also delete from it. How Do I Do this?

我创建了一个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);
        }

    }
}

}

回答by NickJ

Buttons need Event Handlers to work. You have not added any Event Handlers to the Save and Delete buttons. You need to call the addActionListener on those buttons too. I recommend anonymous inner classes:

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

mybutton.addActionListener(new ActionListener() {

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

});

回答by Sharad Tank

You need to addActionListenerto the buttons btSaveand btDelete. You could create a anonymous class like this and perform your work there.

你需要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
 }
}

Edit:

编辑:

I have an example to which you can refer to and accordingly make changes by understanding it. I got it from a professor at our institute.

我有一个例子,你可以参考它,并通过理解它做出相应的改变。我从我们研究所的一位教授那里得到的。

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("");
            }
        }
    }
}