以下是你需要的代碼,你可以選擇任何你需要打開的文件在文本框下打開
創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比行唐網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式行唐網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋行唐地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。
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("打開文件") ; private JButton save = new JButton("保存文件") ; private JLabel label = new JLabel("現(xiàn)在沒有打開的文件") ; 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í)行的是打開操作 this.area.setText("") ; // 打開將文字區(qū)域的內(nèi)容清空 fileChooser.setApproveButtonText("確定") ; fileChooser.setDialogTitle("打開文件") ; result = fileChooser.showOpenDialog(this.frame) ; if(result==JFileChooser.APPROVE_OPTION){ // 選擇的是確定按鈕 file = fileChooser.getSelectedFile() ; // 得到選擇的文件 this.label.setText("打開的文件名稱為:" + file.getName()) ; }else if(result==JFileChooser.CANCEL_OPTION){ this.label.setText("沒有選擇任何文件") ; }else{ this.label.setText("操作出現(xiàn)錯誤") ; } 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("選擇的存儲文件名稱為:" + file.getName()) ; }else if(result==JFileChooser.CANCEL_OPTION){ this.label.setText("沒有選擇任何文件") ; }else{ this.label.setText("操作出現(xiàn)錯誤") ; } 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("一個測試框框");
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ū)動
// String dbURL = "jdbc:mysql://localhost:3306/project"; // 連接服務(wù)器和數(shù)據(jù)庫test
// String userName = "root";
// String userPwd = "root";
String driverName = "jdbc:mysql://localhost:3306/project"; // 加載JDBC驅(qū)動
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Test1"; // 連接服務(wù)器和數(shù)據(jù)庫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();
}
}
}
標(biāo)題名稱:java輸出框代碼 java輸出窗口
瀏覽路徑:http://chinadenli.net/article8/dogepop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、Google、網(wǎng)站設(shè)計、外貿(mào)建站、定制開發(fā)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)