Java图形化实现登录

· 代码实现 

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login {
    public static void main(String[] args) {
        //创建窗体
        JFrame jf = new JFrame("登录");
        //创建顶部展示标签
        JLabel jl3 = new JLabel("学生信息管理系统 V1.0");
        // 创建用户名标签
        JLabel jl1 = new JLabel("用户名:");
        // 创建文本框
        final JTextField jtf1 = new JTextField();
        // 创建密码标签
        JLabel jl2 = new JLabel("密码:");
        // 创建密码框
        final JPasswordField jpf1 = new JPasswordField();
        // 创建"提交"按钮
        JButton jb1 = new JButton("登录");
        // 创建"重置"按钮
        JButton jb2 = new JButton("注册");
        //按钮1的动作
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if ((jtf1.getText().trim().equals("123") && new String(jpf1.getPassword()).trim().equals("666"))) {
                    JOptionPane.showMessageDialog(null, "登陆成功!");
                } else if((jtf1.getText().trim().equals("111") && new String(jpf1.getPassword()).trim().equals("666"))){
                    JOptionPane.showMessageDialog(null, "登陆成功!");
                } else if((jtf1.getText().trim().equals("321") && new String(jpf1.getPassword()).trim().equals("666"))){
                    JOptionPane.showMessageDialog(null, "登陆成功!");
                } else if (jtf1.getText().trim().length() == 0 || new String(jpf1.getPassword()).trim().length() == 0) {
                    JOptionPane.showMessageDialog(null, "不能为空!");
                } else {
                    JOptionPane.showMessageDialog(null, "错误");
                    // 清零
                    jtf1.setText("");
                    jpf1.setText("");
                }
            }
        });

        //按钮2的动作9
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "小样,注册不了");
            }
        });

        //绝对布局
        jf.setLayout(null);
        // 定义一个容器
        Container c = jf.getContentPane();
        // 将各组件添加到容器中
        c.add(jl3);
        c.add(jl1);
        c.add(jtf1);
        c.add(jl2);
        c.add(jpf1);
        c.add(jb1);
        c.add(jb2);
        // 设置各组件的位置以及大小
        jl3.setBounds(85,5,300,20);
        jl1.setBounds(10, 40, 90, 30);
        jtf1.setBounds(60, 40, 210, 30);
        jl2.setBounds(23, 80, 90, 30);
        jpf1.setBounds(60, 80, 210, 30);
        jb1.setBounds(80, 130, 70, 30);
        jb2.setBounds(150, 130, 70, 30);
        //背景色
        jb1.setBackground(Color.orange);
        jb2.setBackground(Color.orange);
        // 设置窗体大小、关闭方式、不可拉伸
        jf.setLocation(600,300);
        jf.setSize(320, 220);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }
}

· 运行结果 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQWxhbuacqOWtkOadjg==,size_20,color_FFFFFF,t_70,g_se,x_16

 

 

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Java图形化实现进程调度需要借助GUI库,如Swing或JavaFX。下面以JavaFX为例,介绍如何实现进程调度的图形化界面。 1. 定义进程类:与上面介绍的一样,定义进程类,包括进程的名称、状态、优先级等信息。 ```java public class Process { private String name; private int priority; private int state; // 其它属性和方法 } ``` 2. 创建UI界面:使用JavaFX创建UI界面,包括进程列表、调度算法选择、开始、暂停、继续、停止等操作。 ```java public class ProcessScheduler extends Application { private ProcessQueue processQueue; private Scheduler scheduler; private TableView<Process> processTable; private ComboBox<String> algorithmComboBox; private Button startButton; private Button pauseButton; private Button resumeButton; private Button stopButton; @Override public void start(Stage primaryStage) throws Exception { // 创建进程队列和调度器 processQueue = new ProcessQueue(); scheduler = new Scheduler(processQueue); // 创建UI界面 BorderPane root = new BorderPane(); root.setPadding(new Insets(10)); // 进程列表 processTable = new TableView<>(); TableColumn<Process, String> nameColumn = new TableColumn<>("进程名称"); nameColumn.setCellValueFactory(new PropertyValueFactory<>("name")); TableColumn<Process, Integer> priorityColumn = new TableColumn<>("优先级"); priorityColumn.setCellValueFactory(new PropertyValueFactory<>("priority")); TableColumn<Process, String> stateColumn = new TableColumn<>("状态"); stateColumn.setCellValueFactory(new PropertyValueFactory<>("state")); processTable.getColumns().addAll(nameColumn, priorityColumn, stateColumn); root.setCenter(processTable); // 调度算法选择 algorithmComboBox = new ComboBox<>(); algorithmComboBox.getItems().addAll("先来先服务", "短作业优先", "时间片轮转"); algorithmComboBox.setValue("先来先服务"); root.setTop(algorithmComboBox); // 开始、暂停、继续、停止按钮 HBox buttonBox = new HBox(10); startButton = new Button("开始"); startButton.setOnAction(event -> { scheduler.setAlgorithm(algorithmComboBox.getValue()); scheduler.start(); }); pauseButton = new Button("暂停"); pauseButton.setOnAction(event -> scheduler.pause()); resumeButton = new Button("继续"); resumeButton.setOnAction(event -> scheduler.resume()); stopButton = new Button("停止"); stopButton.setOnAction(event -> scheduler.stop()); buttonBox.getChildren().addAll(startButton, pauseButton, resumeButton, stopButton); root.setBottom(buttonBox); // 显示界面 Scene scene = new Scene(root, 400, 300); primaryStage.setScene(scene); primaryStage.show(); } } ``` 3. 实现调度算法:根据不同的调度算法,实现对应的进程调度逻辑,如先来先服务、短作业优先、时间片轮转等算法。 ```java public class Scheduler { private ProcessQueue processQueue; private String algorithm; private boolean running; private boolean paused; private Thread thread; public Scheduler(ProcessQueue processQueue) { this.processQueue = processQueue; algorithm = "先来先服务"; running = false; paused = false; } public void setAlgorithm(String algorithm) { this.algorithm = algorithm; } public void start() { if (!running) { running = true; thread = new Thread(() -> { while (running) { if (!paused) { switch (algorithm) { case "先来先服务": fcfs(); break; case "短作业优先": sjf(); break; case "时间片轮转": rr(); break; } } // 等待一段时间,模拟进程调度 try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }); thread.start(); } } public void pause() { paused = true; } public void resume() { paused = false; } public void stop() { running = false; } // 先来先服务调度算法 public void fcfs() { Process process = processQueue.getFirstArrivedProcess(); if (process != null) { processQueue.removeProcess(process); process.setState(Process.RUNNING); runProcess(process); process.setState(Process.TERMINATED); } } // 短作业优先调度算法 public void sjf() { Process process = processQueue.getShortestJobProcess(); if (process != null) { processQueue.removeProcess(process); process.setState(Process.RUNNING); runProcess(process); process.setState(Process.TERMINATED); } } // 时间片轮转调度算法 public void rr() { Process process = processQueue.getHighestPriorityProcess(); if (process != null) { processQueue.removeProcess(process); process.setState(Process.RUNNING); runProcess(process); if (!process.isCompleted()) { process.setState(Process.READY); processQueue.addProcess(process); } } } // 模拟进程运行 private void runProcess(Process process) { // 模拟进程运行,更新进程的运行时间和状态等信息 // ... } } ``` 以上代码仅为示例,实际应用中还需要考虑更多的因素,如进程的同步、互斥等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan木子李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值