- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试制作一个简单的地址簿 GUI,最终能够将姓名等所有字段添加到数据库中。目前,我正在尝试添加方法来更改按下按钮时会发生的情况,但我不断收到此错误......
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at AddressBook.actionPerformed(AddressBook.java:122)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
.
.
. theres some more but you get the point
我想知道是否有人可以帮助我找出为什么我不断收到此错误,甚至无论如何都可以改进该程序的结构。谢谢!
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
public class AddressBook implements ActionListener{
JFrame frame;
JPanel Master, centerInfo, buttonInfo;
GridBagConstraints constraints;
GridBagConstraints topconstraint;
JLabel jlb_firstName, jlb_lastName, jlb_phoneNum, jlb_searchInfo;
JTextField jtf_firstName, jtf_lastName, jtf_phoneNum;
JTextArea jta_searchInfo;
JButton jb_add, jb_search, jb_share;
Container pane;
String firstName, lastName, phoneNum;
//address book designed in GridBagLayout via swing
public static void main(String[] args)
{
AddressBook a = new AddressBook();
System.out.println("hello world");
a.start_gui();
}
public void start_gui()
{
JFrame frame = new JFrame("Address Book");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = new Container();
pane = frame.getContentPane();
layComponents(pane);
//frame.setSize(400, 400);
frame.setSize(400,400);
frame.setVisible(true);
}
private void layComponents(Container contentPane)
{
//PANELS,CONTENTPANE,CONSTRAINTS
JPanel Master = new JPanel();
JPanel centerInfo = new JPanel(new GridBagLayout());
JPanel buttonInfo = new JPanel(new FlowLayout(FlowLayout.LEADING,10,5));
Master.add(centerInfo, BorderLayout.CENTER);
Master.add(buttonInfo, BorderLayout.SOUTH);
contentPane.add(Master); //set style of layout for GUI
GridBagConstraints constraints = new GridBagConstraints(); //layout constraints
GridBagConstraints topconstraint = new GridBagConstraints(); //constraint against top of GUI
constraints.insets = new Insets(5,5,10,10);
//components: firstName, lastName, phoneNum, info, Add, Search(need to add oracle sql db)
//LABELS
JLabel jlb_firstName = new JLabel("First Name");
topconstraint.gridx =1;
topconstraint.gridy =0;
topconstraint.insets = new Insets(20,5,10,10); //Insets(top,left,right,down)
centerInfo.add(jlb_firstName, topconstraint);
JLabel jlb_lastName = new JLabel("Last Name");
constraints.gridx =1;
constraints.gridy =2;
centerInfo.add(jlb_lastName,constraints);
JLabel jlb_phoneNum = new JLabel("Phone Number");
constraints.gridx =1;
constraints.gridy =4;
centerInfo.add(jlb_phoneNum,constraints);
JLabel jlb_searchInfo = new JLabel("Search Info");
constraints.gridx =1;
constraints.gridy =6;
centerInfo.add(jlb_searchInfo, constraints);
//TEXTFIELDS
JTextField jtf_firstName = new JTextField("Enter New First Name"); //to clear textfield when clicked need actionlisteners
topconstraint.gridx =3;
topconstraint.gridy =0;
centerInfo.add(jtf_firstName,topconstraint);
JTextField jtf_lastName = new JTextField("Enter New Last Name");
constraints.gridx =3;
constraints.gridy =2;
centerInfo.add(jtf_lastName, constraints);
JTextField jtf_phoneNum = new JTextField("Enter New Phone Number");
constraints.gridx =3;
constraints.gridy =4;
centerInfo.add(jtf_phoneNum, constraints);
//TEXTAREA
Border loweredetched;
loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
JTextArea jta_searchInfo = new JTextArea(10,20);
jta_searchInfo.append("click me");
jta_searchInfo.selectAll();
jta_searchInfo.requestFocus();
jta_searchInfo.setBorder(loweredetched);
constraints.gridx =3;
constraints.gridy =6;
centerInfo.add(jta_searchInfo, constraints);
//buttons will use flowlayout
//BUTTONS
JButton jb_add = new JButton("Add");
buttonInfo.add(jb_add);
JButton jb_search = new JButton("Search");
buttonInfo.add(jb_search);
JButton jb_share = new JButton("Share");
buttonInfo.add(jb_share); //share info with ppl. need a new share gui
//Listen for any component actions performed
jb_add.addActionListener(this);
//jb_search.addActionListener(this);
//jb_share.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
firstName = jtf_firstName.getText();
System.out.println(firstName);
}
}
最佳答案
您已经隐藏了实例变量...
您已声明...
JTextField jtf_firstName, jtf_lastName, jtf_phoneNum;
作为实例字段,但在 layComponents
中,您已重新声明它们
JTextField jtf_firstName = new JTextField("Enter New First Name");
这意味着实例字段为null
。
关于Java swing简单通讯录事件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28057192/
您好,我是个菜鸟,如果您对此有任何帮助,我将不胜感激。我正在尝试使用用户保存在他们地址簿中的数据,之后我想让他们选择使用该地址进行递送。 这是我的地址 Controller class Address
我目前正在为我的公司开发一个网络应用程序。在此应用程序中,有一个表单,其中的输入字段包含有关联系人的信息。我想创建一个按钮,单击即可在地址簿中创建联系人。 是否可以使用 JavaScript 或其他网
在隐藏通讯录界面之前,是否可以一次选择多个联系人?还是每次地址簿界面出现时实际上只需要选择一个联系人? 最佳答案 是的,Whatsapp做到了。 如果您需要一个示例,请参见以下示例: MultiCon
我正在尝试获取 Android 中联系人的所有电话号码。我的代码如下所示: ContentResolver cr = context.getContentResolver(); String[] pr
我正在为域中的所有用户构建一个表。我让表格正常工作,并在其中为我的分页添加了一些代码,但现在我不断收到错误... //connection $conn = new mysqli($servername
我正在开发一个可以访问 iPhone 通讯录的应用程序。模拟器仅包含几个示例条目。我想找到一个包含数千个条目的示例地址簿字符集(英语、中文、阿拉伯语等)来全面测试我的应用程序。自己填写地址簿将花费永远
是否有任何 android API 可以提取 vcard 3.0 格式的联系人?我遇到了一个开源 vcard 项目 android-vcard 但在用法示例中提到了以下注释。 重要提示:如果您正在为
我的应用程序能够在应用程序的不同实例之间共享其内容,即有一个按钮可以让我将文档附加到电子邮件并将其发送给应用程序的另一个用户。当他们收到此文件时,它会在他们的应用程序实例中打开。这一切正常。 但是,在
我正在开发 iPhone 联系人管理应用程序。一位用户报告说他的所有联系人都是重复的。当他选择特定来源时,联系人不会重复。显然,这是因为他的地址簿同步了两次:一次是与他 PC 上的 iTunes 同步
我有两个继承自 UIViewController 的 Objective-C 类,我正在尝试一种不同的方法来学习如何与 iPhone 的地址簿进行交互。 example Apple 假设一切都在一个类
我想添加在 iOS 通讯录中保存联系人(电话号码、姓名、地址和电子邮件)的功能,方法是单击我的应用程序中的一个按钮!我该怎么做? 最佳答案 非常简单,只需按照以下步骤操作, 首先,您需要导入以下框架,
我正在使用新的 API 在我的手机上插入/更新联系人,我正在使用这种方法来更新它们.. String selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.
我目前正在开发 Contact Importer 网络应用程序(使用 PHP),这样我就可以从 Gmail、Yahoo 等网站的用户帐户中获取电子邮件地址,并将它们用于我自己的邪恶目的。开个玩笑,我的
我与 Google 通讯录合作,一切顺利。但一个小时后,我需要手动刷新授予的访问权限。据此SO question我应该能够通过这个调用更新我的 refresh_token : refresh_toke
我正在使用以下代码获取 google 联系人姓名和电话号码。授权页面本身没有正常显示错误,如“您请求的页面无效”。 :( 请帮我解决这个问题... ` google.load("gdata",
正在尝试使用 google contacts api,如果我使用的话 https://www.google.com/m8/feeds/contacts/default/full 我有 25 个联系人,
由于 Google 停止了对旧版 Auth 的支持,现在我们必须使用 oAuth 2,我们简单的桌面应用程序无法再从我的 Google 帐户读取联系人。 很好 - 我明白这一点,但是这个新的 oAut
我想以编程方式将联系人直接保存到 iOS 设备的地址簿。 我该怎么做? 最佳答案 这是一个小例子: CFErrorRef error = NULL; NSLog(@"%@", [self descri
我正在尝试使用 Javascript 提取我在 Google 通讯录 ( https://contacts.google.com ) 上的联系人。为此,我需要将联系人面板滚动到底部,以便获取所有页面源
我目前正在开发一个 VoIP 应用程序,但我真的很受地址簿困扰。 由于自定义设计, native 地址簿不适合我的应用程序。此外,我想添加一些 native 地址簿中不存在的额外数据。但这会导致一些问
我是一名优秀的程序员,十分优秀!