java:求一个用swing来做小程序,我是用来修改配置文件用的,求代码谢谢
布局最好做成比如一个名字,后面是个输入框,换行再个名字,再个输入框那种,最下面一个确定按钮和一个推出按钮...
布局最好做成比如一个名字,后面是个输入框,换行再个名字,再个输入框那种,最下面一个确定按钮和一个推出按钮
展开
3个回答
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Test02 extends JFrame {
private JPanel jp = new JPanel();
private JButton jb01 = new JButton("按钮一");
private JButton jb02 = new JButton("按钮二");
private JButton jb03 = new JButton("按钮三");
private JButton[] jb = new JButton[] { jb01, jb02, jb03 };
private JLabel jl = new JLabel("请单击按钮!");
private int count = 0;
public Test02() {
for (int i = 0; i < jb.length; i++) {
jp.add(jb[i]);
}
jp.add(jl);
this.add(jp);
this.setTitle("点按钮,记录单击按钮的次数和名字!");
jb01.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Test02.this.jl.setText(jl.getText());
}
});
for (int i = 0; i < jb.length; i++) {
jb[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jb01) {
Test02.this.jl.setText("您单击的是按钮一,您总共单机了" + (++count)
+ "次按钮");
} else if (e.getSource() == jb02) {
Test02.this.jl.setText("您单击的是按钮二,您总共单机了" + (++count)
+ "次按钮");
} else if (e.getSource() == jb03) {
Test02.this.jl.setText("您单击的是按钮三,您总共单机了" + (++count)
+ "次按钮");
}
}
});
this.setBounds(100, 100, 480, 130);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
new Test02();
}
}
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Test02 extends JFrame {
private JPanel jp = new JPanel();
private JButton jb01 = new JButton("按钮一");
private JButton jb02 = new JButton("按钮二");
private JButton jb03 = new JButton("按钮三");
private JButton[] jb = new JButton[] { jb01, jb02, jb03 };
private JLabel jl = new JLabel("请单击按钮!");
private int count = 0;
public Test02() {
for (int i = 0; i < jb.length; i++) {
jp.add(jb[i]);
}
jp.add(jl);
this.add(jp);
this.setTitle("点按钮,记录单击按钮的次数和名字!");
jb01.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Test02.this.jl.setText(jl.getText());
}
});
for (int i = 0; i < jb.length; i++) {
jb[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == jb01) {
Test02.this.jl.setText("您单击的是按钮一,您总共单机了" + (++count)
+ "次按钮");
} else if (e.getSource() == jb02) {
Test02.this.jl.setText("您单击的是按钮二,您总共单机了" + (++count)
+ "次按钮");
} else if (e.getSource() == jb03) {
Test02.this.jl.setText("您单击的是按钮三,您总共单机了" + (++count)
+ "次按钮");
}
}
});
this.setBounds(100, 100, 480, 130);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public static void main(String[] args) {
new Test02();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
XML文件(e:\data\dbconfigure.xml):
<?xml version="1.0" encoding="UTF-8"?>
<CONFIGUREDATA>
<CONFIGURE TYPE="SQL Server 2005">
<DRIVER>com.microsoft.sqlserver.jdbc.SQLServerDriver</DRIVER>
<URL>jdbc:sqlserver://localhost:1433;DatabaseName=DBName</URL>
<USERID>sa</USERID>
<PASSWORD>123</PASSWORD>
</CONFIGURE>
</CONFIGUREDATA>
Java程序(Test.java):
import java.io.File;
import java.io.FileOutputStream;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Test{
public static void main(String[] args){
try{
String filepath = "E:\\data\\dbconfigure.xml";
File file = new File(filepath);
SAXBuilder builder = new SAXBuilder();
Document document = (Document) builder.build(file);
Element root = document.getRootElement();
Element elem1 = root.getChild("CONFIGURE");
//修改密码
elem1.getChild("PASSWORD").setText("123456");
//写回XML文件
Format format=Format.getRawFormat();
format.setEncoding("UTF-8");
XMLOutputter output=new XMLOutputter(format);
output.output(document, new FileOutputStream(filepath));
}
catch(Exception e){
e.printStackTrace();
}
}
}
Swing省略。
<?xml version="1.0" encoding="UTF-8"?>
<CONFIGUREDATA>
<CONFIGURE TYPE="SQL Server 2005">
<DRIVER>com.microsoft.sqlserver.jdbc.SQLServerDriver</DRIVER>
<URL>jdbc:sqlserver://localhost:1433;DatabaseName=DBName</URL>
<USERID>sa</USERID>
<PASSWORD>123</PASSWORD>
</CONFIGURE>
</CONFIGUREDATA>
Java程序(Test.java):
import java.io.File;
import java.io.FileOutputStream;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Test{
public static void main(String[] args){
try{
String filepath = "E:\\data\\dbconfigure.xml";
File file = new File(filepath);
SAXBuilder builder = new SAXBuilder();
Document document = (Document) builder.build(file);
Element root = document.getRootElement();
Element elem1 = root.getChild("CONFIGURE");
//修改密码
elem1.getChild("PASSWORD").setText("123456");
//写回XML文件
Format format=Format.getRawFormat();
format.setEncoding("UTF-8");
XMLOutputter output=new XMLOutputter(format);
output.output(document, new FileOutputStream(filepath));
}
catch(Exception e){
e.printStackTrace();
}
}
}
Swing省略。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class SwingTest extends JFrame{
JButton b;
JTextField tf;
public SwingTest() {
b = new JButton(" 测试 ");
this.add(b);
tf = new JTextField(20);
this.add(tf);
this.setLayout(new FlowLayout());
this.setBounds(200, 100, 300, 150);
this.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = JOptionPane.showInputDialog("请输入:", "1");
tf.setText(s);
}
});
}
public static void main(String[] args) {
SwingTest st = new SwingTest();
}
}
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class SwingTest extends JFrame{
JButton b;
JTextField tf;
public SwingTest() {
b = new JButton(" 测试 ");
this.add(b);
tf = new JTextField(20);
this.add(tf);
this.setLayout(new FlowLayout());
this.setBounds(200, 100, 300, 150);
this.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = JOptionPane.showInputDialog("请输入:", "1");
tf.setText(s);
}
});
}
public static void main(String[] args) {
SwingTest st = new SwingTest();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询