【发布时间】:2015-11-04 01:14:19
【问题描述】:
以下是我的代码副本列表,请提供帮助。我迷路了,我无法让它工作,我没有收到错误,因此我没有具体问题。我从编写这个程序的其他方式开始。我需要输入一个名称并将其写入表单并呈现。
主要
// Main Class//
import java.awt.Dimension;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args){
JFrame frame = new JFrame("Contact Data Vault");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(300,300));
frame.setVisible(true);
}
{
}
}
框架
//Frame Class
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Frame extends JFrame {
private JButton addButton, printButton;
private JLabel firstLabel, lastLabel;
private JTextField firstField, lastField;
private JPanel panel;
private JTextArea textArea;
private ArrayList<Person> people = new ArrayList<Person>();
public Frame() {
super("MY APP");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(800,300));
setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
firstLabel = new JLabel("First Name: ");
lastLabel = new JLabel("Last Name: ");
firstField = new JTextField(10);
lastField = new JTextField(10);
addButton = new JButton("Add Person");
addButton.addActionListener(new ButtonListener());
printButton = new JButton("Display People");
printButton.addActionListener(new ButtonListener());
panel.add(firstLabel);
panel.add(firstField);
panel.add(lastLabel);
panel.add(lastField);
panel.add(addButton);
panel.add(printButton);
textArea = new JTextArea();
add(panel, BorderLayout.NORTH);
add(textArea, BorderLayout.CENTER);
setVisible(true);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e){
if(e.getSource() == printButton){
for(Person p: people){
textArea.append(p+"\n");
}
textArea.append("Display Button Pushed\n");
}else if(e.getSource() == addButton){
System.out.println("add button pressed");
String firstName = firstField.getText();
String lastName = lastField.getText();
people.add(new Person(firstName, lastName));
firstField.setText("");
firstField.setText("");
}
}
}
}
人
//Person Class
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName){
this.firstName= firstName;
this.lastName= lastName;
}
public String toString(){
return lastName + ", " + firstName;
}
}
【问题讨论】:
-
你的调试告诉你什么?
-
它什么也没告诉我它只是显示一个空白窗口 jframe。一些它不引用框架类的方式。我已经尝试了几种方法,但仍然没有。
标签: java arrays swing class jframe