Trang chủ Tin Học Lớp 12 Code để tạo Open Dialog Box bởi sử dụng Java...

Code để tạo Open Dialog Box bởi sử dụng Java Swing câu hỏi 1080409 - hoctapsgk.com

Câu hỏi :

Code để tạo Open Dialog Box bởi sử dụng Java Swing

Lời giải 1 :

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; public class OpenMenu extends JFrame implements ActionListener{ JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; OpenMenu(){ open=new JMenuItem("Open File"); open.addActionListener(this); file=new JMenu("File"); file.add(open); mb=new JMenuBar(); mb.setBounds(0,0,800,20); mb.add(file); ta=new JTextArea(800,800); ta.setBounds(0,20,800,800); add(mb); add(ta); } public void actionPerformed(ActionEvent e) { if(e.getSource()==open){ openFile(); } } void openFile(){ JFileChooser fc=new JFileChooser(); int i=fc.showOpenDialog(this); if(i==JFileChooser.APPROVE_OPTION){ File f=fc.getSelectedFile(); String filepath=f.getPath(); displayContent(filepath); } } void displayContent(String fpath){ try{ BufferedReader br=new BufferedReader(new FileReader(fpath)); String s1="",s2=""; while((s1=br.readLine())!=null){ s2+=s1+"\n"; } ta.setText(s2); br.close(); }catch (Exception e) {e.printStackTrace(); } } public static void main(String[] args) { OpenMenu om=new OpenMenu(); om.setSize(800,800); om.setLayout(null); om.setVisible(true); om.setDefaultCloseOperation(EXIT_ON_CLOSE)

Thảo luận

Lời giải 2 :

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.io.*;

public class OpenMenu extends JFrame implements ActionListener{

JMenuBar mb;

JMenu file;

JMenuItem open;

JTextArea ta;

OpenMenu(){

open=new JMenuItem("Open File");

open.addActionListener(this);

file=new JMenu("File");

file.add(open);

mb=new JMenuBar();

mb.setBounds(0,0,800,20);

mb.add(file);

ta=new JTextArea(800,800);

ta.setBounds(0,20,800,800);

add(mb);

add(ta);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource()==open){

openFile();

}

}

void openFile(){

JFileChooser fc=new JFileChooser();

int i=fc.showOpenDialog(this);

if(i==JFileChooser.APPROVE_OPTION){

File f=fc.getSelectedFile();

String filepath=f.getPath();

displayContent(filepath);

}

}

void displayContent(String fpath){

try{

BufferedReader br=new BufferedReader(new FileReader(fpath));

String s1="",s2="";

while((s1=br.readLine())!=null){

s2+=s1+"\n";

}

ta.setText(s2);

br.close();

}catch (Exception e) {e.printStackTrace(); }

}

public static void main(String[] args) {

    OpenMenu om=new OpenMenu();

    om.setSize(800,800);

    om.setLayout(null);

    om.setVisible(true);

    om.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

Bạn có biết?

Tin học, tiếng Anh: informatics, tiếng Pháp: informatique, là một ngành khoa học chuyên nghiên cứu quá trình tự động hóa việc tổ chức, lưu trữ, xử lý và truyền dẫn thông tin của một hệ thống máy tính cụ thể hoặc trừu tượng (ảo). Với cách hiểu hiện nay, tin học bao hàm tất cả các nghiên cứu và kỹ thuật có liên quan đến việc mô phỏng, biến đổi và tái tạo thông tin.

Nguồn : Wikipedia - Bách khoa toàn thư

Tâm sự 12

Lớp 12 - Năm cuối ở cấp tiểu học, năm học quan trọng nhất trong đời học sinh trải qua bao năm học tập, bao nhiêu kì vọng của người thân xung quanh ta. Những nỗi lo về thi đại học và định hướng tương lai thật là nặng. Hãy tin vào bản thân là mình sẽ làm được rồi tương lai mới chờ đợi các em!

Nguồn : ADMIN :))

Copyright © 2021 HOCTAP247