重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先建一个文本文件,比如在D盘根目录下,名字为:int.txt,然后写一个Java类CalcNumber.java。
修水网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。成都创新互联公司自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司。
****************************************************************
int.txt里面的文本如下:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
****************************************************************
CalcNumber.java类代码如下:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
public class CalcNumber extends JFrame {
JTextArea in;
JTextArea suText;
JTextArea heText;
public CalcNumber() {
this.setTitle("计算素数和合数");
// 输入框
in = new JTextArea(4, 1);
in.setAutoscrolls(true);
JScrollPane inScroll = new JScrollPane(in);
in.setBorder(BorderFactory.createBevelBorder(1));
JPanel upper = new JPanel(new BorderLayout());
upper.add(inScroll);
// 按钮
JButton imp = new JButton("导入文本");
imp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
BufferedReader br = new BufferedReader(new FileReader(
"d:\\int.txt"));
String s;
while ((s = br.readLine()) != null) {
in.setText(in.getText() + "\n" + s);
}
br.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
});
JButton start = new JButton("启动两个线程并计算");
start.addActionListener(new CalcActionListener());
JPanel buttonP = new JPanel();
buttonP.add(imp);
buttonP.add(start);
// 结果
JPanel result = new JPanel(new GridLayout(1, 2));
JPanel suPanel = new JPanel();
suPanel.setBorder(BorderFactory.createTitledBorder("以下为素数"));
JPanel hePanel = new JPanel();
hePanel.setBorder(BorderFactory.createTitledBorder("以下为合数"));
result.add(suPanel);
result.add(hePanel);
suText = new JTextArea(4, 25);
JScrollPane suScroll = new JScrollPane(suText);
suText.setBorder(BorderFactory.createBevelBorder(1));
heText = new JTextArea(4, 25);
JScrollPane heScroll = new JScrollPane(heText);
heText.setBorder(BorderFactory.createBevelBorder(1));
suPanel.add(suScroll);
hePanel.add(heScroll);
// 总
JPanel all = new JPanel(new GridLayout(2, 1, 2, 2));
all.setBorder(BorderFactory.createTitledBorder("计算素数和合数"));
all.add(upper);
all.add(result);
this.add(buttonP, BorderLayout.SOUTH);
this.add(all, BorderLayout.CENTER);
// this.setSize(400, 300);
this.pack();
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension screen = tool.getScreenSize();
this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
/ 2 - this.getHeight() / 2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class CalcActionListener implements ActionListener, Runnable {
public void actionPerformed(ActionEvent e) {
Thread suThread = new Thread(new CalcActionListener(), "su");
Thread heThread = new Thread(new CalcActionListener(), "he");
suThread.start();
heThread.start();
}
public void run() {
String s = in.getText();
String[] arr = s.split("\\s");
StringBuffer su = new StringBuffer();
StringBuffer he = new StringBuffer();
for (int i = 0; i arr.length; i++) {
boolean flag = false;
if (!arr[i].equals("") arr[i] != null) {
int n = Integer.valueOf(arr[i]);
for (int j = 2; j n; j++) {
if (n % j == 0) {
flag = true;
}
}
if (Thread.currentThread().getName().equals("su")
flag == false) {
su = su.append(" " + n);
}
if (Thread.currentThread().getName().equals("he")
flag == true) {
he = he.append(" " + n);
}
}
}
if (Thread.currentThread().getName().equals("su")) {
suText.setText(new String(su));
} else {
heText.setText(new String(he));
}
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
CalcNumber c = new CalcNumber();
}
}
****************************************************************
运行结果如下:
看代码:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
public class Main {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("JFrame with JSplitPane");
JLabel label1 = new JLabel("label1");
label1.setBackground(Color.BLUE);
label1.setVerticalAlignment(SwingConstants.TOP);//设置标签内容处于顶端
label1.setHorizontalAlignment(SwingConstants.CENTER);//设置内容居中
// 让组件变成不透明,这样在JLabel上所设置的颜色才能显示出来
label1.setOpaque(true);
JLabel label2 = new JLabel("label2");
label2.setBackground(Color.LIGHT_GRAY);
label2.setVerticalAlignment(SwingConstants.TOP);
label2.setHorizontalAlignment(SwingConstants.CENTER);
label2.setOpaque(true);
JLabel label3 = new JLabel("label3");
label3.setBackground(Color.RED);
label3.setVerticalAlignment(SwingConstants.TOP);
label3.setHorizontalAlignment(SwingConstants.CENTER);
label3.setOpaque(true);
//加入 label1,label2 到 splitPane1 中,并设置 splitPane1 为水平分割
JSplitPane splitPane1 = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT, false, label1, label2);
// 设置 splitPane1 的分隔线位置,0.4 是相对于 splitPane1 的大小而定
splitPane1.setDividerLocation(0.4);
splitPane1.setResizeWeight(0.4);
JSplitPane splitPane2 = new JSplitPane(
JSplitPane.VERTICAL_SPLIT, false, splitPane1, label3);
splitPane2.setDividerLocation(0.5);
splitPane2.setResizeWeight(0.5);
frame.add(splitPane2);
frame.setSize(400, 300);
frame.setLocationRelativeTo(null); // 设置居中
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
运行结果:
我下面写了一个,初始账号和密码是 nihao 5201314 你可以改或设置
import java.awt.*;
import java.awt.event.*;
class MyDow extends Frame implements ActionListener{
private Label lName,lPassword;
private TextField tName,tPassword;
private Button login,rest,exitout;
private Label login_clew;
private int count=0;
private String myName="nihao";
private String myPassword="5201314";
public void setMyNP(String n,String p){
myName=n;
myPassword=p;
}
public MyDow(String str){
super(str);
}
public void init(){
setLayout(null);
setSize(400,300);
setVisible(true);
setResizable(false);
lName=new Label("姓名");
lName.setBounds(50, 70, 60, 30);add(lName);
lPassword=new Label("密码");
lPassword.setBounds(50, 120, 60, 30);add(lPassword);
tName=new TextField("");
tName.setBounds(110, 70, 200, 30);add(tName);
tPassword=new TextField("");
tPassword.setBounds(110, 120, 200, 30);add(tPassword);
login=new Button("登录");
login.setBounds(90, 200, 60, 30);add(login);
rest=new Button("重输");
rest.setBounds(170, 200, 60, 30);add(rest);
exitout=new Button("退出");
exitout.setBounds(250,200,60,30);add(exitout);
login_clew=new Label("");
login.addActionListener(this);
rest.addActionListener(this);
exitout.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==login){
if(count2){
if(tName.getText().equalsIgnoreCase(myName)tPassword.getText().equalsIgnoreCase(myPassword)){
removeAll();
add(login_clew);
add(exitout);
exitout.setLabel("确定");
login_clew.setBounds(160,120,200,30);
login_clew.setText("欢迎XX用户!");
exitout.setBounds(170,220,60,30);
}else{
add(login_clew);
login_clew.setBounds(90,160,200,30);
login_clew.setText("请输入正确的信息!");
count++;
}
}else{
add(login_clew);
login_clew.setBounds(90,160,200,30);
login_clew.setText("你输入的次数达到了3次,不能登录了!");
}
}else if(e.getSource()==exitout){
System.exit(1);
}else{
lName.setText("");
lPassword.setText("");
}
}
}
public class UserLogin {
public static void main(String args[]){
MyDow m=new MyDow("登录窗口");
m.init();
}
}
import java.awt.FlowLayout;
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.JTextArea;
import javax.swing.JTextField;
public class Demo extends JFrame{
String username;
String password;
public Demo() {
username = "java";
password = "123";
setTitle("#####");
setSize(400, 300);
setLocation(400, 200);
setLayout(new FlowLayout());
JTextArea jta = new JTextArea(4,30);
jta.setText("请输入用户名密码:\n用户名:"+username+"\n密码:"+password);
jta.setEditable(false);//设置文本区域不可编辑
add(jta);
JLabel usernameLabel = new JLabel("用户名:");
final JTextField usernameText = new JTextField(10);
add(usernameLabel);
add(usernameText);
JLabel passwordLabel = new JLabel("密码:");
final JTextField passwordText = new JTextField(10);
add(passwordLabel);
add(passwordText);
JButton button = new JButton("登录");
add(button);
final JLabel result = new JLabel();//用来显示登录结果
add(result);
button.addActionListener(new ActionListener() {//登录按钮事件
@Override
public void actionPerformed(ActionEvent e) {
//获取输入的用户名密码
String tempUserName = usernameText.getText();
String tempPassWord = passwordText.getText();
if(tempUserName.equals(username)tempPassWord.equals(password)){
result.setText("登录成功");
}else{
result.setText("登录失败");
}
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Demo();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ClientDemo01 {
public static void main(String[] args){
JFrame f=new JFrame("AA");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(15,30);
ta.setEditable(false); //文本域只读
JScrollPane sp=new JScrollPane(ta); //滚动窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("发送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
socket=new Socket("192.168.0.4",5000);
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread01 mt=new MyThread01(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener01(tf,ta,bos));
}
}
class ButtonActionListener01 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText();
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("AA:"+message+"\n"); //添加到文本域并换行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("发送失败");
}
}
}
}
class MyThread01 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread01(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("BB:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
} import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ServerDemo01{
public static void main(String[] args){
JFrame f=new JFrame("BB");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(12,30); //文本域,第一个参数为行数,第二个参数为列数
ta.setEditable(false); //文本域只读
JScrollPane sp=new JScrollPane(ta); //滚动窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("发送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ServerSocket server=null;
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
server=new ServerSocket(5000);
//ta.append("等待AA连接...\n");
socket=server.accept();
//ta.append("AA已连接\n");
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread1 mt=new MyThread1(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener1(tf,ta,bos));
}
}
class ButtonActionListener1 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText(); //获取文本框中的内容
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("BB:"+message+"\n"); //添加到文本域并换行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("发送失败!");
}
}
}
}
class MyThread1 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread1(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("AA:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
一: 首先弄清题目的意思
A.需要的主要组件列表:
1. 创建一个窗口,窗口标题叫Information
2. 3个标签, 用于显示文字 Name Number Class
3. 3个文本框, 用于填写信息
4. 1个按钮, 文字是确认
5. 1个文本域
B.业务逻辑
1. 当点击按钮确认的时候, 把 文本框的信息显示到文本域
C.设计的主要技术
JLabel , JButton, JTextField ...等, 都是swing的组件 , 所以应该使用swing进行创建
二: 确定使用的布局
swing虽然重写了大部分的组件, 但是布局, 依旧沿袭awt技术
分析图片上的布局:
至少有2种方法可以实现,
方法一: 绝对布局 , 优点: 配合可视化GUI拖曳, 可以完美的实现图上的组件的位置
但是缺点也是致命的, 不同的操作系统平台下, 可能会出现位置的移动,
只适合开发平台, 移植效果差 . 所以不推荐使用
方法二: 灵活的表格布局, 配合流式布局 , 所有操作系统下,显示效果都比较统一.
三: 效果图
四: 参考代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameDemo extends JFrame {
//申明需要的组件
private final JTextField jtf1,jtf2,jtf3;
private final JTextArea jta;
public FrameDemo() {
setTitle("Information");//设置窗口标题
setSize(320, 360);//设置窗口大小
setLocationRelativeTo(null);//设置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭时退出虚拟机
getContentPane().setLayout(new FlowLayout());//设置窗口布局为流式布局
JPanel jp = new JPanel(new GridLayout(4, 2));//设置jp面板为表格布局4行2列
//第一行
JPanel jp01 = new JPanel();
JLabel jl1 = new JLabel("Name:");
jp01.add(jl1);
JPanel jp1 = new JPanel();
jtf1 = new JTextField(8);
jp1.add(jtf1);
//第二行
JPanel jp02 = new JPanel();
JLabel jl2 = new JLabel("Number:");
jp02.add(jl2);
JPanel jp2 = new JPanel();
jtf2 = new JTextField(8);
jp2.add(jtf2);
//第三行
JPanel jp03 = new JPanel();
JLabel jl3 = new JLabel("Class:");
jp03.add(jl3);
JPanel jp3 = new JPanel();
jtf3 = new JTextField(8);
jp3.add(jtf3);
//第四行
JPanel jp04 = new JPanel();
JLabel jl4 = new JLabel("");
jp04.add(jl4);
JPanel jp4 = new JPanel();
JButton jb = new JButton("确认");
jp4.add(jb);
jp.add(jp01);
jp.add(jp1);
jp.add(jp02);
jp.add(jp2);
jp.add(jp03);
jp.add(jp3);
jp.add(jp04);
jp.add(jp4);
getContentPane().add(jp);
jta = new JTextArea();
jta.setColumns(20);//设置文本域的大小
jta.setEditable(false);//设置文本域不可编辑
jta.setBackground(jp.getBackground());//设置文本域的背景色和面板一样
getContentPane().add(jta);
jb.addActionListener(new ActionListener() {//给按钮添加事件
public void actionPerformed(ActionEvent e) {//点击按钮,显示信息到文本域
String name = jtf1.getText();
String number = jtf2.getText();
String clazz = jtf3.getText();
jta.setText("You name is "+name+" number is "+number+" class is "+clazz);
}
});
}
public static void main(String[] args) {
new FrameDemo().setVisible(true);//创建窗口,被设置为可见
}
}
五: 拓展
虽然图形界面的实现方法是多样的, 我们一定要根据具体情况, 选择一个比较优化的 合理的, 符合业务逻辑的实现方法