以下是你需要的代碼,你可以選擇任何你需要打開(kāi)的文件在文本框下打開(kāi)
創(chuàng)新互聯(lián)建站-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比行唐網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式行唐網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋行唐地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴(lài)。
import java.io.File ;import java.io.FileInputStream ;import java.io.FileOutputStream ;import java.io.PrintStream ;import java.util.Scanner ;import java.awt.BorderLayout ;import java.awt.event.WindowAdapter ;import java.awt.event.WindowEvent ;import java.awt.event.ActionEvent ;import java.awt.event.ActionListener ;import javax.swing.JFrame ;import javax.swing.JTextArea ;import javax.swing.JLabel ;import javax.swing.JButton ;import javax.swing.JPanel ;import javax.swing.JFileChooser ;import javax.swing.JScrollPane ;class Note implements ActionListener{ private JTextArea area = new JTextArea(8,10) ; // 定義文本區(qū) private JFrame frame = new JFrame("Welcome To MLDN") ; private JButton open = new JButton("打開(kāi)文件") ; private JButton save = new JButton("保存文件") ; private JLabel label = new JLabel("現(xiàn)在沒(méi)有打開(kāi)的文件") ; private JPanel butPan = new JPanel() ; public Note(){ this.butPan.add(open) ; // 在面板中加入按鈕 this.butPan.add(save) ; // 在面板中加入按鈕 this.frame.setLayout(new BorderLayout(3,3)) ; this.frame.add(this.label,BorderLayout.NORTH) ; this.frame.add(this.butPan,BorderLayout.SOUTH) ; this.frame.add(new JScrollPane(this.area),BorderLayout.CENTER) ; this.frame.setSize(330,180) ; this.frame.setVisible(true) ; this.frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(1) ; } } ) ; this.open.addActionListener(this) ; this.save.addActionListener(this) ; } public void actionPerformed(ActionEvent e){ File file = null ; // 接收文件 int result = 0 ; // 接收操作狀態(tài) JFileChooser fileChooser = new JFileChooser() ; // 文件選擇框 if(e.getSource()==this.open){ // 表示執(zhí)行的是打開(kāi)操作 this.area.setText("") ; // 打開(kāi)將文字區(qū)域的內(nèi)容清空 fileChooser.setApproveButtonText("確定") ; fileChooser.setDialogTitle("打開(kāi)文件") ; result = fileChooser.showOpenDialog(this.frame) ; if(result==JFileChooser.APPROVE_OPTION){ // 選擇的是確定按鈕 file = fileChooser.getSelectedFile() ; // 得到選擇的文件 this.label.setText("打開(kāi)的文件名稱(chēng)為:" + file.getName()) ; }else if(result==JFileChooser.CANCEL_OPTION){ this.label.setText("沒(méi)有選擇任何文件") ; }else{ this.label.setText("操作出現(xiàn)錯(cuò)誤") ; } if(file!=null){ try{ Scanner scan = new Scanner(new FileInputStream(file)) ; scan.useDelimiter("\n") ; while(scan.hasNext()){ this.area.append(scan.next()) ; this.area.append("\n") ; } scan.close() ; }catch(Exception e1){} } } if(e.getSource()==this.save){ // 判斷是否是保存操作 result = fileChooser.showSaveDialog(this.frame) ; // 顯示保存框 if(result==JFileChooser.APPROVE_OPTION){ // 選擇的是確定按鈕 file = fileChooser.getSelectedFile() ; // 得到選擇的文件 this.label.setText("選擇的存儲(chǔ)文件名稱(chēng)為:" + file.getName()) ; }else if(result==JFileChooser.CANCEL_OPTION){ this.label.setText("沒(méi)有選擇任何文件") ; }else{ this.label.setText("操作出現(xiàn)錯(cuò)誤") ; } if(file!=null){ try{ PrintStream out = new PrintStream(new FileOutputStream(file)) ; out.print(this.area.getText()) ; out.close() ; }catch(Exception e1){} } } }}
public class JFileChooserDemo{ public static void main(String args[]){ new Note() ; }}
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.awt.event.WindowAdapter;
import?java.awt.event.WindowEvent;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JTextField;
public?class?XXFrame?extends?JFrame?implements?ActionListener
{
private?JTextField?name?=?new?JTextField("someone");
private?JTextField?text?=?new?JTextField();
private?JButton?button?=?new?JButton("輸入");
public?XXFrame()
{
super("一個(gè)測(cè)試框框");
name.setBounds(40,?40,?200,?20);
button.setBounds(260,?40,?100,?20);
text.setBounds(40,?110,?200,?20);
this.setLayout(null);
this.setBounds(200,?200,?400,?400);
this.add(name);
this.add(button);
this.add(text);
this.addWindowListener(new?WindowAdapter()
{
public?void?windowClosing(WindowEvent?e)
{
System.exit(-1);
}
});
button.addActionListener(this);
this.setVisible(true);
}
@Override
public?void?actionPerformed(ActionEvent?e)
{
if?(e.getSource()?==?button)
{
text.setText(name.getText());
}
}
public?static?void?main(String[]?args)
{
new?XXFrame();
}
}
package test;
import java.sql.*;
import javax.swing.*;
public class selection_all {
public static void main(String[] srg) {
// String driverName = "org.gjt.mm.mysql.Driver"; // 加載JDBC驅(qū)動(dòng)
// String dbURL = "jdbc:mysql://localhost:3306/project"; // 連接服務(wù)器和數(shù)據(jù)庫(kù)test
// String userName = "root";
// String userPwd = "root";
String driverName = "jdbc:mysql://localhost:3306/project"; // 加載JDBC驅(qū)動(dòng)
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Test1"; // 連接服務(wù)器和數(shù)據(jù)庫(kù)test
String userName = "sa";
String userPwd = "123";
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
Statement s = dbConn.createStatement();
ResultSet rs = s.executeQuery("select * from person");
String str = "htmltable";
while (rs.next()) {
System.out.println(rs.getString("name") + "\t" + rs.getString("no") + "\t");
// str += rs.getString("name") + "\t" + rs.getString("no") + "\t\n";
str +="trtd" + rs.getString("name") + "/tdtd" + rs.getString("no") + "/td/tr";
// System.out.println(rs.getString("name") + "\t" + rs.getString("password") + "\t");
// str +="trtd" + rs.getString("name") + "/tdtd" + rs.getString("password") + "/td/tr";
}
str += "/table/html";
s.close();
JDialog dialog = new JDialog();
dialog.setBounds(200, 200, 500, 300);
JLabel jl = new JLabel();
jl.setVerticalAlignment(SwingConstants.TOP);
dialog.add(jl);
jl.setText(str);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}