大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
创新互联专注于平遥网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供平遥营销型网站建设,平遥网站制作、平遥网页设计、平遥网站官网定制、小程序制作服务,打造平遥网络公司原创品牌,更为您提供平遥网站排名全网营销落地服务。
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ReadBook extends JFrame {
JTextArea jta;
JTextField jtf;
JButton jb;
public ReadBook() {
jta = new JTextArea();
jtf = new JTextField(30);
jtf.setText("文件保存路径如c:\\ab.txt");
jb = new JButton("保存文字");
JPanel jp = new JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp, BorderLayout.SOUTH);
setBounds(500, 100, 500, 380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//-------------核心代码---------
String path = jtf.getText();
File f = new File(path);
String txt = jta.getText().replaceAll("\n", "\r\n");
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(txt);//写入文件中
bw.close();
} catch (Exception e1) {
e1.printStackTrace();
}
//-------------核心代码---------
}
});
}
public static void main(String[] args) {
new ReadBook();
}
}
运行如图
参考代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RegDemo extends JFrame implements ActionListener{
JTextField jtf;
JPasswordField jpf;
public RegDemo() {
//组件的创建, 和布局安排
JPanel jpc = new JPanel();//默认流式布局
JPanel jp1 = new JPanel(new GridLayout(2, 2,5,10));//网格布局
jp1.setBorder(BorderFactory.createTitledBorder("用户注册"));
JLabel jl1 = new JLabel("用户名:");
jtf = new JTextField(10);
JLabel jl2 = new JLabel("密码:");
jpf = new JPasswordField(10);
jpf.setEchoChar('*');//用*号来隐藏密码的显示
jp1.add(jl1);jp1.add(jtf);
jp1.add(jl2);jp1.add(jpf);
jpc.add(jp1);
add(jpc);
JButton jb1 = new JButton("提交");
jb1.addActionListener(this);
jb1.setActionCommand("yes");
JButton jb2 = new JButton("取消");
jb2.addActionListener(this);
jb2.setActionCommand("no");
JPanel jp2 = new JPanel();
jp2.add(jb1);jp2.add(jb2);
add(jp2,BorderLayout.SOUTH);
setTitle("用户注册界面");
setSize(280, 280);
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//
setVisible(true);
}
public static void main(String[] args) {
new RegDemo();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("yes")){
String name = jtf.getText().trim();
String pwd = new String(jpf.getPassword());
if(name.equals("")||pwd.equals("")){
JOptionPane.showMessageDialog(this, "你还没有输入用户名或者密码");
}else{
JOptionPane.showMessageDialog(this, "注册成功!用户名"+name+",密码"+pwd);
}
}else{
jtf.setText("");
jpf.setText("");
}
}
}
我觉得还是我的好些。。。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileOutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class TestSwing extends JFrame{
private JLabel label1, label2, label3, label4,label5,label6,label7;
private JPanel panel,panel1,panel2;
private JTextField tField1,tField2,tField3,tField4,tField5,tField6,tField7,tField8,tField9;
private JButton button;
private String filepath="c:\\info.dat";
public TestSwing(){
init();
this.setSize(300, 280);
this.setLocation(
(Toolkit.getDefaultToolkit().getScreenSize().width - this
.getSize().width) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height - this
.getSize().height) / 2);
this.setResizable(false);
this.setVisible(true);
this.setTitle("info");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void init(){
initPanel();
initLabel();
initTextField();
initButton();
initContent();
}
private void initContent(){
panel1.add(label1);
panel1.add(tField1);
panel1.add(label2);
panel1.add(tField2);
panel1.add(label3);
panel1.add(tField3);
panel1.add(label4);
panel1.add(tField4);
panel1.add(label5);
panel1.add(tField5);
panel1.add(label6);
panel1.add(tField6);
panel1.add(label7);
panel1.add(tField7);
panel1.add(tField8);
panel1.add(tField9);
panel2.add(button);
panel.add(panel1,BorderLayout.CENTER);
panel.add(panel2,BorderLayout.SOUTH);
getContentPane().add(panel);
}
private void initPanel(){
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setBackground(Color.WHITE);
panel1 = new JPanel();
GridLayout upLayout = new GridLayout(8, 2);
panel1.setLayout(upLayout);
panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
}
private void initLabel(){
label1 = new JLabel("IP地址", SwingConstants.LEFT);
label2 = new JLabel("端口", SwingConstants.LEFT);
label3 = new JLabel("访问地址", SwingConstants.LEFT);
label4 = new JLabel("XXXXX", SwingConstants.LEFT);
label5 = new JLabel("XXXXX", SwingConstants.LEFT);
label6 = new JLabel("XXXXX", SwingConstants.LEFT);
label7 = new JLabel("XXXXX", SwingConstants.LEFT);
}
private void initTextField(){
tField1 = new JTextField(10);
tField2 = new JTextField(10);
tField3 = new JTextField(10);
tField4 = new JTextField(10);
tField5 = new JTextField(10);
tField6 = new JTextField(10);
tField7 = new JTextField(10);
tField8 = new JTextField(10);
tField9 = new JTextField(10);
}
private void initButton(){
button = new JButton("完成");
button.addMouseListener(new MouseAdapter(){
//左键点击
public void mouseClicked(MouseEvent e) {
String ipv = tField1.getText().trim();
String portv = tField2.getText().trim();
String addrv = tField3.getText().trim();
String xx1v = tField4.getText().trim();
String xx2v = tField5.getText().trim();
String xx3v = tField6.getText().trim();
String xx4v = tField7.getText().trim();
String inputx = tField8.getText().trim();
String inputxv = tField9.getText().trim();
createFile(ipv, portv, addrv, xx1v, xx2v, xx3v, xx4v, inputx, inputxv);
JOptionPane.showMessageDialog(null, "保存成功");
tField1.setText("");
tField2.setText("");
tField3.setText("");
tField4.setText("");
tField5.setText("");
tField6.setText("");
tField7.setText("");
tField8.setText("");
tField9.setText("");
}
});
}
public static void main(String[]args){
new TestSwing();
}
private void createFile(String ipv,String portv ,String addrv,String xx1v,String xx2v,String xx3v,String xx4v,String inputx,String inputxv){
StringBuffer context = new StringBuffer();
context.append("ip=").append(ipv).append("\n");
context.append("port=").append(portv).append("\n");
context.append("addr=").append(addrv).append("\n");
context.append("xx1=").append(xx1v).append("\n");
context.append("xx2=").append(xx2v).append("\n");
context.append("xx3=").append(xx3v).append("\n");
context.append("xx4=").append(xx4v).append("\n");
context.append(inputx).append("=").append(inputxv).append("");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filepath);
fos.write(context.toString().getBytes());
} catch (Exception e) {
} finally {
if (fos != null)
try {
fos.close();
} catch (Exception e) {
}
}
}
}
你用的是TextField只能改变长度不能改变宽度的
你可以使用TextArea这个是可以改变宽度的
P.S.用Awt编程不好看 建议用Swing 可能在视觉上要好看一些 不过无所谓
一般来说.比较常使用的是 JTextArea文本框的无参数构造器进行构造
然后可以使用setText("文字")来设置文本,或者getText()方法来获得文本
JTextArea jta = new JTextArea();
jta.setText("你好啊");
String txt = jta.getText();
具体例子
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class ReadBook extends JFrame{
JTextArea jta;
JTextField jtf;
JButton jb;
public ReadBook(){
jta = new JTextArea();
jtf = new JTextField(30);
jtf.setText("文件路径如c:\\ab.txt");
jb = new JButton("显示文字");
JPanel jp = new JPanel();
jp.add(jtf);
jp.add(jb);
add(jta);
add(jp,BorderLayout.SOUTH);
setBounds(500, 100, 500, 380);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String path = jtf.getText();
File f= new File(path);
if(f.exists()!f.isDirectory()){//存在该文件且不是文件夹
try {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(f));
String temp;
while((temp=br.readLine())!=null){
sb.append(temp+"\n");
}
br.close();
jta.setText(sb.toString());//setText方法可以给jta添加文字
} catch (Exception e1) {
e1.printStackTrace();
}
}else{
jtf.setText("路径错误.重新填写");
}
}
});
}
public static void main(String[] args) {
new ReadBook();
}
}
效果
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class vv extends JDialog {
private static final long serialVersionUID = 1L;
private JLabel l_Id = new JLabel("登陆账户", JLabel.CENTER);
private JLabel l_pw = new JLabel("登陆密码", JLabel.CENTER);
private JTextField t_Id = new JTextField(10);
private JPasswordField t_pw = new JPasswordField(10);
private JButton btnLogin;
private JButton btnClose;
public vv() {
super();
setResizable(false);
getContentPane().setBackground(new Color(225, 225, 225));
getContentPane().setLayout(null);
initialize();
}
protected void initialize() {
setTitle("系统登录");
l_Id.setBounds(48, 43, 53, 25);
t_Id.setBounds(110, 43, 150, 25);
l_pw.setBounds(48, 93, 53, 25);
t_pw.setBounds(110, 93, 150, 25);
getContentPane().add(l_Id);
getContentPane().add(l_pw);
getContentPane().add(t_Id);
getContentPane().add(t_pw);
btnLogin = new JButton();
btnLogin.setText("登 录");
btnLogin.setBounds(70, 142, 85, 28);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addBtnLoginActionListener();
}
});
getContentPane().add(btnLogin);
btnClose = new JButton();
btnClose.setText("关 闭");
btnClose.setBounds(175, 142, 85, 28);
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(-1);
}
});
getContentPane().add(btnClose);
}
private void addBtnLoginActionListener() {
String user = t_Id.getText();
String password = new String(t_pw.getPassword());
if (user.equals("")) {
JOptionPane.showMessageDialog(this, "帐号不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {
JOptionPane.showMessageDialog(this, "密码不可为空", "Caution",
JOptionPane.WARNING_MESSAGE);
return;
}
String sql = "select * FROM login WHERE id = '" + user + "' and pw = '"
+ password + "'";
boolean success = false;
// TODO:数据校验 success = executeQuery(sql);
if (success) {
// TODO: 如果数据校验成功 显示主界面 并关闭登录界面
JOptionPane.showMessageDialog(this, "成功登录", "提示",
JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else {
JOptionPane.showMessageDialog(this, "帐号或密码错误!", "警告",
JOptionPane.WARNING_MESSAGE);
t_pw.requestFocus(); // 密码框选中
}
}
public Dimension getPreferredSize() {
return new Dimension(320, 170);
}
public void show() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
Dimension d = getSize();
this.setLocation((screen.width - d.width) / 2,
(screen.height - d.height) / 2);
// 输入密码后回车相当于点击了登录按钮
getRootPane().setDefaultButton(btnLogin);
t_pw.requestFocus();
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 220);
super.show();
}
public static void main(String[] args) {
vv loginFrame = new vv();
loginFrame.setVisible(true);
}
}
希望对你有帮助