我对从其他类中访问变量的想法有些困难。我在这里有一个帖子:

Having access to a private variable from other classes in Java

在那里我得到了一些有用的信息,并认为一个例子会更好地展示它,并提出一个单独的问题。我有一个可以输入数据的表单,它有一个列表变量。起初我并没有将其设置为静态的,但是我想如果需要从另一个类中获取总大小,那么我就不会创建该类的实例来使用函数来获取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 addressList = new ArrayList();

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);

}

}

}

}

}

我的另一个问题是,为什么我会得到错误,“int不能被取消引用contactsLabel.settext(addressBookForm.getTotalContacts().ToString());

谢谢!

java st_在Java中使用GETSt/Stter相关推荐

  1. java string 占位符_驳《阿里「Java开发手册」中的1个bug》?

    前两天写了一篇关于<阿里Java开发手册中的 1 个bug>的文章,评论区有点炸锅了,基本分为两派,支持老王的和质疑老王的. 首先来说,无论是那一方,我都真诚的感谢你们.特别是「二师兄」, ...

  2. SearchHit转成java对象_Java开发中最常犯的10个错误,你中招了吗?

    http://www.programcreek.com/2014/05/top-10-mistakes-java-developers-make/ 阅读目录 Array转ArrayList 判断一个数 ...

  3. java mod %区别_Java中 % 与Math.floorMod() 区别详解

    %为取余(rem),Math.floorMod()为取模(mod) 取余取模有什么区别呢? 对于整型数a,b来说,取模运算或者取余运算的方法都是: 1.求 整数商: c = a/b; 2.计算模或者余 ...

  4. android java style_Android 在Java代码中设置style属性--使用代码创建ProgressBar对象

    强烈推荐: 在andriod开发中,很大一部分都要与资源打交道,比如说:图片,布局文件,字符串,样式等等.这给我们想要开发一些公共的组件带来很大的困难,因为公共的组件可能更愿意以jar包的形式出现.但 ...

  5. java string与integer_Java中Integer和String浅谈

    http://qxzxcjq-126-com.iteye.com/blog/883283 Java中的基本数据类型有八种:int.char.boolean.byte.long.double.float ...

  6. maven只打包java目录_ssm项目中maven对resources目录打包的路径_默认路径,自定义路径...

    博客引用处(以下内容在原有博客基础上进行补充或更改,谢谢这些大牛的博客指导): ssm项目打包后mybatis的mapper.xml文件没有放进去 问题出现的原因: ssm项目打包后mybatis的m ...

  7. JAVA坏境变量中的JAVA_HOME path classpath 的设置与作用。

    JAVA坏境变量中的JAVA_HOME path classpath 的设置与作用. 今天再次设置java的环境变量,突然发现每次设置都只是按照步骤一步步将其设置完,并不了解为啥要设置,于是上网找了找 ...

  8. 类的包访问权限:《Java编程思想》中一段话的困惑

    类的包访问权限:<Java编程思想>中一段话的困惑 在<java编程思想第三版>(陈昊鹏 饶若楠等译)的第五章隐藏具体实现中,5.4节的最后一段话是: "正如前面所提 ...

  9. java初学者的书中收获

    Java初学者的书中收获 搜索关于java老师的记忆碎片,第一节课的记忆······其实java比c语言简单许多,也许老师的这句话是对的.可是作为一个初学者,感觉自己因为付出的时间没有在学c语言的时候 ...

最新文章

  1. Redis最佳实践:7个维度+43条使用规范,带你彻底玩转Redis | 附实践清单
  2. 【WebGoat笔记】--- Cross-Site Scripting(XSS)
  3. python写接口测试代码_python写运单接口测试(增改查)完整代码
  4. centos基础命令 第二节
  5. centos7 安装java和tomcat9
  6. tomcat启动Configuration Error: deployment source ‘xxx:war exploded‘ is not valid
  7. 使用migration创建表时,出错的解决方法
  8. Microsoft Store 微软商店中 APP 独立安装包下载方法
  9. 技嘉主板bios设置方法
  10. android导航软件安装,【图】手把手教你安装免费且无需流量的安卓版本凯立德导航软件!...
  11. 个人收集资料分享(电子、计算机相关)
  12. 运维(20) 制作启动U盘安装Win10
  13. Unity-动画机学习
  14. pytest常用参数
  15. 过五关,斩六将!「网易/美团/菜鸟」已拿offer【Java岗】
  16. 微信网页授权获取用户基本信息 --- 20/03/16
  17. 实现HTML页面在手机浏览器上全屏的方式
  18. 001-云E办_后端项目搭建
  19. 开源一个微信+wap的建站系统
  20. Linux 设置安排每天重启任务

热门文章

  1. python gcm channel_python – 无法使用Django在GCM中发送POST请求
  2. 使用BP神经网络进行预测(电力负荷预测)
  3. 半导体的非平衡载流子----陷阱效应介绍
  4. 弘辽科技:优化好淘宝标题关键词,提升排名不愁怕
  5. ES7、ES8、ES9、ES10、ES11新特性
  6. 并发编程知识的简单整理(一)
  7. Allegro如何快速删除孤立铜皮操作指导
  8. java stopwatch_Java计时新姿势StopWatch详解
  9. Calendar 设置某年某月的日历
  10. XJTU大计基作业-1(第7周)