本文目录一览:
Java编程(写出程序代码)
写了一个代码,代码如下,可以进行参考
public class sum {
public static void main(String[] args) {
//创建一个Scanner的对象input
Scanner input = new Scanner(System.in);
//提示用户输入数据
System.out.print("请输入一个整数");
//将输入的值赋给n
int n = input.nextInt();
//定义变量接收计算后的和
int sum = 0;
//利用循环进行求和
for (int i = 0; i = n; i++) {
sum+=i;
}
//输出最后的和
System.out.println("从0一直到"+n+"的所有整数的和是:"+sum);
}
}
java编程代码!请仔细看我的说明. . .
我来帮您,代码如下:
import java.awt.Container;
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.JOptionPane;
import javax.swing.JProgressBar;
public class A extends JFrame {
private static final long serialVersionUID = 1L;
JProgressBar pb;
A() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocation(400, 100);
getContentPane().setLayout(null);
pb = new JProgressBar(); // 改动1
pb.setBounds(10, 201, 212, 40);
getContentPane().add(pb);
setTitle("滚动条测试!");
pb.setStringPainted(true);
final JLabel label = new JLabel();
label.setText("正在登陆验证,请稍候。。。");
label.setBounds(10, 179, 212, 18);
getContentPane().add(label);
final JLabel label03 = new JLabel();
label03.setBounds(49, 247, 13, 18);
getContentPane().add(label03);
final JLabel label04 = new JLabel();
label04.setBounds(68, 247, 13, 18);
getContentPane().add(label04);
final JLabel label05 = new JLabel();
label05.setBounds(86, 247, 13, 18);
getContentPane().add(label05);
final JLabel label06 = new JLabel();
label06.setBounds(105, 247, 13, 18);
getContentPane().add(label06);
final JLabel label07 = new JLabel();
label07.setBounds(124, 247, 13, 18);
getContentPane().add(label07);
final JLabel label02 = new JLabel();
label02.setBounds(30, 247, 13, 18);
getContentPane().add(label02);
final JLabel label01 = new JLabel();
label01.setBounds(10, 247, 13, 18);
getContentPane().add(label01);
final JLabel label08 = new JLabel();
label08.setBounds(143, 247, 13, 18);
getContentPane().add(label08);
final JLabel label09 = new JLabel();
label09.setBounds(162, 247, 13, 18);
getContentPane().add(label09);
final JLabel label10 = new JLabel();
label10.setBounds(177, 247, 13, 18);
getContentPane().add(label10);
JButton btn1 = new JButton("登陆");
btn1.addActionListener(new ActionListener() {
private int i;
public synchronized void actionPerformed(final ActionEvent e) {
new Thread() {
public synchronized void run() {
for (i = 0; i = 100; i++) {
pb.setValue(i);
try {
Thread.currentThread();
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
switch (i) {
case 0:
label01.setText("*");
break;
case 10:
label01.setText("*");
label02.setText("*");
break;
case 20:
label01.setText("*");
label02.setText("*");
label03.setText("*");
break;
case 30:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
break;
case 40:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
break;
case 50:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
break;
case 60:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
label07.setText("*");
break;
case 70:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
label07.setText("*");
label08.setText("*");
break;
case 80:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
label07.setText("*");
label08.setText("*");
label09.setText("*");
break;
case 90:
label01.setText("*");
label02.setText("*");
label03.setText("*");
label04.setText("*");
label05.setText("*");
label06.setText("*");
label07.setText("*");
label08.setText("*");
label09.setText("*");
label10.setText("*");
break;
case 100:
JOptionPane.showMessageDialog((new A()
.getContentPane()), "登陆成功", "恭喜您!",
JOptionPane.WARNING_MESSAGE);
label01.setText("");
label02.setText("");
label03.setText("");
label04.setText("");
label05.setText("");
label06.setText("");
label07.setText("");
label08.setText("");
label09.setText("");
label10.setText("");
pb.setValue(0);
}
}
}
}.start();
}
});
btn1.setBounds(86, 92, 90, 39);
Container con = getContentPane();
con.add(btn1);
}
public static void main(String[] args) {
new A().setVisible(true);
}
}
JAVA编程代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class LoginFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 2899557721654693611L;
final JTextField jt1 = new JTextField ();
final JTextField jt2 = new JTextField ();
public LoginFrame() {
this.setTitle("学生登陆");
this.setLayout(null);
this.setBounds(200, 200, 400, 300);
JButton jb1 = new JButton("登陆");
JButton jb2 = new JButton("退出");
jb1.setBounds(50, 230, 80, 20);
jb2.setBounds(270,230,80,20);
jt1.setBounds(170,80,100,20);
jt2.setBounds(170,170,100,20);
JLabel jl1 = new JLabel("用户名");
JLabel jl2 = new JLabel("密码");
jl1.setBounds(80, 80, 50, 20);
jl2.setBounds(80, 170, 50, 20);
add(jb1);
add(jb2);
add(jt1);
add(jt2);
add(jl1);
add(jl2);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
setVisible(false);
System.exit(0);
}
});
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new IntoTxt().intotex(jt1.getText(), jt2.getText());
}
});
jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
setVisible(false);
System.exit(0);
}
});
}
public static void main(String[] args) {
LoginFrame lf = new LoginFrame();
}
}
class IntoTxt {
public void intotex(String name,String password) {
BufferedWriter bw;
File file = new File("D:/write.txt");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
bw = new BufferedWriter(new FileWriter(file,true));
bw.newLine();
bw.write("用户名是 ");
bw.write(name);
bw.write(" 密码是 ");
bw.write(password);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}