public class Names {
Scanner scan;
static String Firstname;
static String Surname;
static String Fullname;
public void OpenFile()
{
try
{
scan = new Scanner(new File("test.txt"));
System.out.println("File found!");
}
catch(Exception e)
{
System.out.println("File not found");
}
}
public void ReadFile()
{
while(scan.hasNext())
{
Firstname = scan.next();
Surname = scan.next();
Fullname = Firstname + " " + Surname;
System.out.println(Fullname);
}
}
public void CloseFile()
{
scan.close();
}
}
그럼 내가 이름 클래스를 호출이 주요 클래스가 잘 작동 테스트 파일에서 이름을 읽고 다음과 같은 코드입니다. 그것은 테스트 파일의 마지막 이름 만 보여주기 때문에 잘 작동합니다.
public class NameSwing implements ActionListener {
private JTextArea tf = new JTextArea(20,20);
private JFrame f = new JFrame("names");
private JButton b = new JButton("view");
static String fullName;
public NameSwing(){
f.add(new JLabel("Name"));
tf.setEditable(true);
f.add(tf);
b.addActionListener(this);
f.add(b);
f.setLayout(new FlowLayout());
f.setSize(300,100);
f.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)
{
tf.setText(fullName);
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
NameSwing nameSwing = new NameSwing();
Names t = new Names();
t.OpenFile();
t.ReadFile();
t.CloseFile();
fullName = Names.Fullname;
}
}
이것은 테스트 파일의 내용입니다.
닉 해리스
올리 딘
엠마 Sammons
헨리 블랙웰
나는 텍스트 영역이 독자로부터 모든 이름, 그냥 마지막으로하지를 보여주고받을 수있는 방법 이름?
+1; 또, 새로운 JScrollPane (centerPanel)를 고려해, 주위의 컴퍼넌트에 보더를 추가해주세요. 스크롤 구획. – trashgod
@trashgod : Ahha, 아이디어가 마음에 들었고 마찬가지로 업데이트되었습니다 .-) –