Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用getter/setter_Java - Fatal编程技术网

在Java中使用getter/setter

在Java中使用getter/setter,java,Java,我在从其他类访问变量的想法上遇到了一些麻烦。我在这里有一个帖子: 在那里我得到了一些有用的信息,我想用一个例子来展示它会更好,并提出一个单独的问题。我有一个可以输入数据的表单,它有一个列表变量。起初我并没有将其设置为静态,但我认为如果我需要从另一个类中获取总大小,那么我不会创建该类的实例来使用该函数获取TotalContacts。我基本上想用列表中的联系人总数更新我的状态栏。其中一位成员在上面的帖子中说,使用原始的Foo成员来获取联系人,但我不确定在这种情况下如何工作。如有任何想法,将不胜感激。

我在从其他类访问变量的想法上遇到了一些麻烦。我在这里有一个帖子: 在那里我得到了一些有用的信息,我想用一个例子来展示它会更好,并提出一个单独的问题。我有一个可以输入数据的表单,它有一个列表变量。起初我并没有将其设置为静态,但我认为如果我需要从另一个类中获取总大小,那么我不会创建该类的实例来使用该函数获取TotalContacts。我基本上想用列表中的联系人总数更新我的状态栏。其中一位成员在上面的帖子中说,使用原始的Foo成员来获取联系人,但我不确定在这种情况下如何工作。如有任何想法,将不胜感激。谢谢

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;

public class AddressBook
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                AddressBookFrame frame = new AddressBookFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JMenuBar menuBar = new JMenuBar();
                frame.setJMenuBar(menuBar);

                JMenu fileMenu = new JMenu("File");
                JMenuItem openItem = new JMenuItem("Open");
                JMenuItem saveItem = new JMenuItem("Save");
                JMenuItem saveAsItem = new JMenuItem("Save As");
                JMenuItem printItem = new JMenuItem("Print");
                JMenuItem exitItem = new JMenuItem("Exit");
                fileMenu.add(openItem);
                fileMenu.add(saveItem);
                fileMenu.add(saveAsItem);
                fileMenu.add(printItem);
                fileMenu.add(exitItem);
                menuBar.add(fileMenu);

                JMenu editMenu = new JMenu("Edit");
                JMenuItem newItem = new JMenuItem("New");
                JMenuItem editItem = new JMenuItem("Edit");
                JMenuItem deleteItem = new JMenuItem("Delete");
                JMenuItem findItem = new JMenuItem("Find");
                JMenuItem firstItem = new JMenuItem("First");
                JMenuItem previousItem = new JMenuItem("Previous");
                JMenuItem nextItem = new JMenuItem("Next");
                JMenuItem lastItem = new JMenuItem("Last");
                editMenu.add(newItem);
                editMenu.add(editItem);
                editMenu.add(deleteItem);
                editMenu.add(findItem);
                editMenu.add(firstItem);
                editMenu.add(previousItem);
                editMenu.add(nextItem);
                editMenu.add(lastItem);
                menuBar.add(editMenu);

                JMenu helpMenu = new JMenu("Help");
                JMenuItem documentationItem = new JMenuItem("Documentation");
                JMenuItem aboutItem = new JMenuItem("About");
                helpMenu.add(documentationItem);
                helpMenu.add(aboutItem);

                menuBar.add(helpMenu);

                frame.setVisible(true);

            }
        });
    }
}

class AddressBookFrame extends JFrame
{
    public AddressBookFrame() 
    {
        setLayout(new BorderLayout());
        setTitle("Address Book");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        AddressBookToolBar toolBar = new AddressBookToolBar();
        add(toolBar, BorderLayout.NORTH);
        AddressBookStatusBar aStatusBar = new AddressBookStatusBar();
        add(aStatusBar, BorderLayout.SOUTH);
        AddressBookForm form = new AddressBookForm();
        add(form, BorderLayout.CENTER);
    }
    public static final int DEFAULT_WIDTH = 500;
    public static final int DEFAULT_HEIGHT = 500;

}

/* Create toolbar buttons and add buttons to toolbar */
class AddressBookToolBar extends JPanel
{
    public AddressBookToolBar()
    {
        setLayout(new FlowLayout(FlowLayout.LEFT));
        JToolBar bar = new JToolBar();
        JButton newButton = new JButton("New");
        JButton editButton = new JButton("Edit");
        JButton deleteButton = new JButton("Delete");
        JButton findButton = new JButton("Find");
        JButton firstButton = new JButton("First");
        JButton previousButton = new JButton("Previous");
        JButton nextButton = new JButton("Next");
        JButton lastButton = new JButton("Last");
        bar.add(newButton);
        bar.add(editButton);
        bar.add(deleteButton);
        bar.add(findButton);
        bar.add(firstButton);
        bar.add(previousButton);
        bar.add(nextButton);
        bar.add(lastButton);
        add(bar);
    }
}

/* Creates the status bar string */
class AddressBookStatusBar extends JPanel 
{
    public AddressBookStatusBar()
    {
        setLayout(new FlowLayout(FlowLayout.LEFT));
        this.statusBarString = new JLabel("Total: " + AddressBookForm.getTotalContacts());
        add(this.statusBarString);
    }

    public void updateLabel()
    {
        contactsLabel.setText(AddressBookForm.getTotalContacts().toString());
    }

    private JLabel statusBarString;
    private JLabel contactsLabel;
}

class AddressBookForm extends JPanel 
{
    public AddressBookForm()
    {   
        // create form panel
        this.setLayout(new GridLayout(2, 1));
        JPanel formPanel = new JPanel();
        formPanel.setLayout(new GridLayout(4, 2));
        firstName = new JTextField(20);
        lastName = new JTextField(20);
        telephone = new JTextField(20);
        email = new JTextField(20);
        JLabel firstNameLabel = new JLabel("First Name: ", JLabel.LEFT);
        formPanel.add(firstNameLabel);
        formPanel.add(firstName);
        JLabel lastNameLabel = new JLabel("Last Name: ", JLabel.LEFT); 
        formPanel.add(lastNameLabel);
        formPanel.add(lastName);
        JLabel telephoneLabel = new JLabel("Telephone: ", JLabel.LEFT);
        formPanel.add(telephoneLabel);
        formPanel.add(telephone);
        JLabel emailLabel = new JLabel("Email: ", JLabel.LEFT);
        formPanel.add(emailLabel);
        formPanel.add(email);
        add(formPanel);

        // create button panel
        JPanel buttonPanel = new JPanel();
        JButton insertButton = new JButton("Insert");
        JButton displayButton = new JButton("Display");

        ActionListener insertAction = new AddressBookListener();
        ActionListener displayAction = new AddressBookListener();
        insertButton.addActionListener(insertAction);
        displayButton.addActionListener(displayAction);
        buttonPanel.add(insertButton);
        buttonPanel.add(displayButton);
        add(buttonPanel);
    }

    public static int getTotalContacts()
    {
        return addressList.size();
    }

    //void addContact(Person contact);

    private JTextField firstName;
    private JTextField lastName;
    private JTextField telephone;
    private JTextField email;
    private JLabel contacts;

    private static List<Person> addressList = new ArrayList<Person>();

    private class AddressBookListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String buttonPressed = e.getActionCommand();
            System.out.println(buttonPressed);
            if (buttonPressed == "Insert") {
                Person aPerson = new Person(firstName.getText(), lastName.getText(), telephone.getText(), email.getText());
                addressList.add(aPerson);
            }
            else {
                for (Person p : addressList) {
                    System.out.println(p);
                }
            }
        }
    }

}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.util.List;
导入java.util.ArrayList;
公共类通讯簿
{
公共静态void main(字符串[]args)
{
invokeLater(新的Runnable()
{
公开募捐
{
AddressBookFrame=新的AddressBookFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar=新的JMenuBar();
frame.setJMenuBar(菜单栏);
JMenu fileMenu=新JMenu(“文件”);
JMenuItem openItem=新的JMenuItem(“打开”);
JMenuItem saveItem=新的JMenuItem(“保存”);
JMenuItem saveAsItem=新的JMenuItem(“另存为”);
JMenuItem printItem=新的JMenuItem(“打印”);
JMenuItem exitItem=新的JMenuItem(“退出”);
fileMenu.add(openItem);
fileMenu.add(保存项);
fileMenu.add(saveAsItem);
fileMenu.add(打印项);
fileMenu.add(exitItem);
菜单栏。添加(文件菜单);
JMenu editMenu=新JMenu(“编辑”);
JMenuItem newItem=新JMenuItem(“新”);
JMenuItem editItem=新的JMenuItem(“编辑”);
JMenuItem deleteItem=新的JMenuItem(“删除”);
JMenuItem findItem=新的JMenuItem(“查找”);
JMenuItem firstItem=新的JMenuItem(“第一”);
JMenuItem previousItem=新的JMenuItem(“先前”);
JMenuItem nextItem=新的JMenuItem(“下一个”);
JMenuItem lastItem=新JMenuItem(“Last”);
编辑菜单。添加(新建项);
编辑菜单。添加(编辑项);
编辑菜单。添加(删除项);
编辑菜单。添加(findItem);
编辑菜单。添加(第一项);
编辑菜单.添加(上一项);
编辑菜单。添加(nextItem);
编辑菜单。添加(最后一项);
菜单栏.添加(编辑菜单);
JMenu helpMenu=新JMenu(“帮助”);
JMenuItem documentationItem=新的JMenuItem(“文档”);
JMenuItem aboutItem=新的JMenuItem(“关于”);
帮助菜单.添加(文档项);
帮助菜单.add(aboutItem);
菜单栏。添加(帮助菜单);
frame.setVisible(true);
}
});
}
}
类AddressBookFrame扩展了JFrame
{
公共地址书架()
{
setLayout(新的BorderLayout());
setTitle(“地址簿”);
设置大小(默认宽度、默认高度);
AddressBookToolBar=新建AddressBookToolBar();
添加(工具栏,BorderLayout.NORTH);
AddressBookStatusBar aStatusBar=新的AddressBookStatusBar();
添加(aStatusBar,BorderLayout.SOUTH);
AddressBookForm=新的AddressBookForm();
添加(表单、边框布局、中心);
}
公共静态最终整数默认值_宽度=500;
公共静态最终int默认值_高度=500;
}
/*创建工具栏按钮并将按钮添加到工具栏*/
类AddressBookToolBar扩展了JPanel
{
公共地址booktoolbar()
{
setLayout(新的FlowLayout(FlowLayout.LEFT));
JToolBar=新的JToolBar();
JButton newButton=新JButton(“新”);
JButton editButton=新JButton(“编辑”);
JButton deleteButton=新JButton(“删除”);
JButton findButton=新JButton(“Find”);
JButton firstButton=新JButton(“第一”);
JButton previousButton=新JButton(“先前”);
JButton nextButton=新JButton(“下一步”);
JButton lastButton=新JButton(“Last”);
添加(新按钮);
添加(编辑按钮);
添加(删除按钮);
添加(findButton);
添加(第一个按钮);
添加(上一个按钮);
添加(下一个按钮);
添加(lastButton);
添加(条);
}
}
/*创建状态栏字符串*/
类AddressBookStatusBar扩展了JPanel
{
公共地址bookstatusbar()
{
setLayout(新的FlowLayout(FlowLayout.LEFT));
this.statusBarString=new JLabel(“总计:+AddressBookForm.getTotalContacts());
添加(this.statusBarString);
}
公共void updateLabel()
{
contactsLabel.setText(AddressBookForm.getTotalContacts().toString());
}
私有JLabel statusBarString;
私人JLabel联系人slabel;
}
类AddressBookForm扩展了JPanel
{
公共地址书格式()
{   
//创建窗体面板
这个.setLayout(新的GridLayout(2,1));
JPanel formPanel=新的JPanel();
setLayout(新的GridLayout(4,2));
firstName=新的JTextField(20);
lastName=新的JTextField(20);
电话=新的JTextField(20);
email=新的JTextField(20);
JLabel firstnamelab=新的JLabel(“名字:”,JLabel.LEFT);
formPanel.add(firstnamelab);
formPanel.add(名字);
JLabel lastnamelab=新的JLabel(“姓氏:”,JLabel.LEFT);
添加(lastNameLabel);
formPanel.add(姓氏);
JLabel电话标签=新的JLabel(
int a = 5;
String aString = a + "";
// http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html
public static Integer getTotalContacts()
{
    return new Integer( addressList.size() );
}