首页 > 编程语言 > java基础语法API之GUI和时间类综合练习

java基础语法API之GUI和时间类综合练习

时间:2024-01-04 21:32:00浏览次数:40  
标签:java Color void private public API new jf GUI

一:概述

在这里将以两个小案例,去再次说明一个GUI和时间类的应用。

二:具体案例

<1>案例1:员工出勤查询

在这里面需要最先写入一个DateChooser类,练习的时候可以直接复制这个代码,这个代码比较复杂。这个类是第一个代码块

/*
日期选择器,可以选择日期的显示格式
 */

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*;

public class DateChooser extends JPanel {
    private static final long serialVersionUID = 4529266044762990227L;

    private Date initDate;
    private Calendar now = Calendar.getInstance();
    private Calendar select;
    private JPanel monthPanel;// 月历
    private JP1 jp1;// 四块面板,组成
    private JP2 jp2;
    private JP3 jp3;
    private JP4 jp4;
    private Font font = new Font("宋体", Font.PLAIN, 12);
    private final LabelManager lm = new LabelManager();
    private SimpleDateFormat sdf;
    private boolean isShow = false;
    private Popup pop;

    private JComponent showDate;

    public static DateChooser getInstance() {
        return new DateChooser();
    }

    public static DateChooser getInstance(Date date) {
        return new DateChooser(date);
    }

    public static DateChooser getInstance(String format) {
        return new DateChooser(format);
    }

    public static DateChooser getInstance(Date date, String format) {
        return new DateChooser(date, format);
    }

    /**
     * Creates a new instance of DateChooser
     */
    private DateChooser() {
        this(new Date());
    }

    private DateChooser(Date date) {
        this(date, "yyyy年MM月dd日");
    }

    private DateChooser(String format) {
        this(new Date(), format);
    }

    private DateChooser(Date date, String format) {
        initDate = date;
        sdf = new SimpleDateFormat(format);
        select = Calendar.getInstance();
        select.setTime(initDate);
        initPanel();
    }

    /**
     * 是否允许用户选择
     */
    public void setEnabled(boolean b) {
        super.setEnabled(b);
        showDate.setEnabled(b);
    }

    /**
     * 得到当前选择框的日期
     */
    public Date getDate() {
        return select.getTime();
    }

    public String getStrDate() {
        return sdf.format(select.getTime());
    }

    public String getStrDate(String format) {
        sdf = new SimpleDateFormat(format);
        return sdf.format(select.getTime());
    }

    // 根据初始化的日期,初始化面板
    private void initPanel() {
        monthPanel = new JPanel(new BorderLayout());
        monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        JPanel up = new JPanel(new BorderLayout());
        up.add(jp1 = new JP1(), BorderLayout.NORTH);
        up.add(jp2 = new JP2(), BorderLayout.CENTER);
        monthPanel.add(jp3 = new JP3(), BorderLayout.CENTER);
        monthPanel.add(up, BorderLayout.NORTH);
        monthPanel.add(jp4 = new JP4(), BorderLayout.SOUTH);
        this.addAncestorListener(new AncestorListener() {
            public void ancestorAdded(AncestorEvent event) {

            }

            public void ancestorRemoved(AncestorEvent event) {

            }

            // 只要祖先组件一移动,马上就让popup消失
            public void ancestorMoved(AncestorEvent event) {
                hidePanel();
            }
        });
    }

    public void register(final JComponent showDate) {
        this.showDate = showDate;

        showDate.setRequestFocusEnabled(true);
        showDate.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                showDate.requestFocusInWindow();
            }
        });
        this.setBackground(Color.WHITE);
        this.add(showDate, BorderLayout.CENTER);
        this.setPreferredSize(new Dimension(90, 25));
        this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        showDate.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent me) {
                if (showDate.isEnabled()) {
                    showDate.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    showDate.setForeground(Color.RED);
                }
            }

            public void mouseExited(MouseEvent me) {
                if (showDate.isEnabled()) {
                    showDate.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    showDate.setForeground(Color.BLACK);
                }
            }

            public void mousePressed(MouseEvent me) {
                if (showDate.isEnabled()) {
                    showDate.setForeground(Color.CYAN);
                    if (isShow) {
                        hidePanel();
                    } else {
                        showPanel(showDate);
                    }
                }
            }

            public void mouseReleased(MouseEvent me) {
                if (showDate.isEnabled()) {
                    showDate.setForeground(Color.BLACK);
                }
            }
        });
        showDate.addFocusListener(new FocusListener() {
            public void focusLost(FocusEvent e) {
                hidePanel();
            }

            public void focusGained(FocusEvent e) {

            }
        });
    }

    // 根据新的日期刷新
    private void refresh() {
        jp1.updateDate();
        jp2.updateDate();
        jp3.updateDate();
        jp4.updateDate();
        SwingUtilities.updateComponentTreeUI(this);
    }

    // 提交日期
    private void commit() {
        // TODO add other components here
        if (showDate instanceof JTextField) {
            ((JTextField) showDate).setText(sdf.format(select.getTime()));
        } else if (showDate instanceof JLabel) {
            ((JLabel) showDate).setText(sdf.format(select.getTime()));
        }

        hidePanel();
    }

    // 隐藏日期选择面板
    private void hidePanel() {
        if (pop != null) {
            isShow = false;
            pop.hide();
            pop = null;
        }
    }

    // 显示日期选择面板
    private void showPanel(Component owner) {
        if (pop != null) {
            pop.hide();
        }
        Point show = new Point(0, showDate.getHeight());
        SwingUtilities.convertPointToScreen(show, showDate);
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        int x = show.x;
        int y = show.y;
        if (x < 0) {
            x = 0;
        }
        if (x > size.width - 295) {
            x = size.width - 295;
        }
        if (y < size.height - 170) {
        } else {
            y -= 188;
        }
        pop = PopupFactory.getSharedInstance()
                .getPopup(owner, monthPanel, x, y);
        pop.show();
        isShow = true;
    }

    /**
     * 最上面的面板用来显示月份的增减
     */
    private class JP1 extends JPanel {
        private static final long serialVersionUID = -5638853772805561174L;
        JLabel yearleft, yearright, monthleft, monthright, center,
                centercontainer;

        public JP1() {
            super(new BorderLayout());
            this.setBackground(new Color(160, 185, 215));
            initJP1();
        }

        private void initJP1() {
            yearleft = new JLabel("  <<", JLabel.CENTER);
            yearleft.setToolTipText("上一年");
            yearright = new JLabel(">>  ", JLabel.CENTER);
            yearright.setToolTipText("下一年");
            yearleft.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
            yearright.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));

            monthleft = new JLabel("  <", JLabel.RIGHT);
            monthleft.setToolTipText("上一月");
            monthright = new JLabel(">  ", JLabel.LEFT);
            monthright.setToolTipText("下一月");
            monthleft.setBorder(BorderFactory.createEmptyBorder(2, 30, 0, 0));
            monthright.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 30));

            centercontainer = new JLabel("", JLabel.CENTER);
            centercontainer.setLayout(new BorderLayout());
            center = new JLabel("", JLabel.CENTER);

            centercontainer.add(monthleft, BorderLayout.WEST);
            centercontainer.add(center, BorderLayout.CENTER);
            centercontainer.add(monthright, BorderLayout.EAST);

            this.add(yearleft, BorderLayout.WEST);
            this.add(centercontainer, BorderLayout.CENTER);
            this.add(yearright, BorderLayout.EAST);
            this.setPreferredSize(new Dimension(295, 25));

            updateDate();

            yearleft.addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent me) {
                    yearleft.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    yearleft.setForeground(Color.RED);
                }

                public void mouseExited(MouseEvent me) {
                    yearleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    yearleft.setForeground(Color.BLACK);
                }

                public void mousePressed(MouseEvent me) {
                    select.add(Calendar.YEAR, -1);
                    yearleft.setForeground(Color.WHITE);
                    refresh();
                }

                public void mouseReleased(MouseEvent me) {
                    yearleft.setForeground(Color.BLACK);
                }
            });
            yearright.addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent me) {
                    yearright.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    yearright.setForeground(Color.RED);
                }

                public void mouseExited(MouseEvent me) {
                    yearright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    yearright.setForeground(Color.BLACK);
                }

                public void mousePressed(MouseEvent me) {
                    select.add(Calendar.YEAR, 1);
                    yearright.setForeground(Color.WHITE);
                    refresh();
                }

                public void mouseReleased(MouseEvent me) {
                    yearright.setForeground(Color.BLACK);
                }
            });
            monthleft.addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent me) {
                    monthleft.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    monthleft.setForeground(Color.RED);
                }

                public void mouseExited(MouseEvent me) {
                    monthleft.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    monthleft.setForeground(Color.BLACK);
                }

                public void mousePressed(MouseEvent me) {
                    select.add(Calendar.MONTH, -1);
                    monthleft.setForeground(Color.WHITE);
                    refresh();
                }

                public void mouseReleased(MouseEvent me) {
                    monthleft.setForeground(Color.BLACK);
                }
            });
            monthright.addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent me) {
                    monthright.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    monthright.setForeground(Color.RED);
                }

                public void mouseExited(MouseEvent me) {
                    monthright.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    monthright.setForeground(Color.BLACK);
                }

                public void mousePressed(MouseEvent me) {
                    select.add(Calendar.MONTH, 1);
                    monthright.setForeground(Color.WHITE);
                    refresh();
                }

                public void mouseReleased(MouseEvent me) {
                    monthright.setForeground(Color.BLACK);
                }
            });
        }

        private void updateDate() {
            center.setText(select.get(Calendar.YEAR) + "年"
                    + (select.get(Calendar.MONTH) + 1) + "月");
        }
    }

    private class JP2 extends JPanel {
        private static final long serialVersionUID = -8176264838786175724L;

        public JP2() {
            this.setPreferredSize(new Dimension(295, 20));
        }

        protected void paintComponent(Graphics g) {
            g.setFont(font);
            g.drawString("星期日 星期一 星期二 星期三 星期四 星期五 星期六", 5, 10);
            g.drawLine(0, 15, getWidth(), 15);
        }

        private void updateDate() {

        }
    }

    private class JP3 extends JPanel {
        private static final long serialVersionUID = 43157272447522985L;

        public JP3() {
            super(new GridLayout(6, 7));
            this.setPreferredSize(new Dimension(295, 100));
            initJP3();
        }

        private void initJP3() {
            updateDate();
        }

        public void updateDate() {
            this.removeAll();
            lm.clear();
            Date temp = select.getTime();
            Calendar select = Calendar.getInstance();
            select.setTime(temp);
            select.set(Calendar.DAY_OF_MONTH, 1);
            int index = select.get(Calendar.DAY_OF_WEEK);
            int sum = (index == 1 ? 8 : index);
            select.add(Calendar.DAY_OF_MONTH, 0 - sum);
            for (int i = 0; i < 42; i++) {
                select.add(Calendar.DAY_OF_MONTH, 1);
                lm.addLabel(new MyLabel(select.get(Calendar.YEAR), select
                        .get(Calendar.MONTH), select.get(Calendar.DAY_OF_MONTH)));
            }
            for (MyLabel my : lm.getLabels()) {
                this.add(my);
            }
            select.setTime(temp);
        }
    }

    private class MyLabel extends JLabel implements Comparator<MyLabel>,
            MouseListener, MouseMotionListener {
        private static final long serialVersionUID = 3668734399227577214L;
        private int year, month, day;
        private boolean isSelected;

        public MyLabel(int year, int month, int day) {
            super("" + day, JLabel.CENTER);
            this.year = year;
            this.day = day;
            this.month = month;
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
            this.setFont(font);
            if (month == select.get(Calendar.MONTH)) {
                this.setForeground(Color.BLACK);
            } else {
                this.setForeground(Color.LIGHT_GRAY);
            }
            if (day == select.get(Calendar.DAY_OF_MONTH)) {
                this.setBackground(new Color(160, 185, 215));
            } else {
                this.setBackground(Color.WHITE);
            }
        }

        public boolean getIsSelected() {
            return isSelected;
        }

        public void setSelected(boolean b, boolean isDrag) {
            isSelected = b;
            if (b && !isDrag) {
                int temp = select.get(Calendar.MONTH);
                select.set(year, month, day);
                if (temp == month) {
                    SwingUtilities.updateComponentTreeUI(jp3);
                } else {
                    refresh();
                }
            }
            this.repaint();
        }

        protected void paintComponent(Graphics g) {
            if (day == select.get(Calendar.DAY_OF_MONTH)
                    && month == select.get(Calendar.MONTH)) {
                // 如果当前日期是选择日期,则高亮显示
                g.setColor(new Color(160, 185, 215));
                g.fillRect(0, 0, getWidth(), getHeight());
            }
            if (year == now.get(Calendar.YEAR)
                    && month == now.get(Calendar.MONTH)
                    && day == now.get(Calendar.DAY_OF_MONTH)) {
                // 如果日期和当前日期一样,则用红框
                Graphics2D gd = (Graphics2D) g;
                gd.setColor(Color.RED);
                Polygon p = new Polygon();
                p.addPoint(0, 0);
                p.addPoint(getWidth() - 1, 0);
                p.addPoint(getWidth() - 1, getHeight() - 1);
                p.addPoint(0, getHeight() - 1);
                gd.drawPolygon(p);
            }
            if (isSelected) {// 如果被选中了就画出一个虚线框出来
                Stroke s = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE,
                        BasicStroke.JOIN_BEVEL, 1.0f,
                        new float[]{2.0f, 2.0f}, 1.0f);
                Graphics2D gd = (Graphics2D) g;
                gd.setStroke(s);
                gd.setColor(Color.BLACK);
                Polygon p = new Polygon();
                p.addPoint(0, 0);
                p.addPoint(getWidth() - 1, 0);
                p.addPoint(getWidth() - 1, getHeight() - 1);
                p.addPoint(0, getHeight() - 1);
                gd.drawPolygon(p);
            }
            super.paintComponent(g);
        }

        public boolean contains(Point p) {
            return this.getBounds().contains(p);
        }

        private void update() {
            repaint();
        }

        public void mouseClicked(MouseEvent e) {
        }

        public void mousePressed(MouseEvent e) {
            isSelected = true;
            update();
        }

        public void mouseReleased(MouseEvent e) {
            Point p = SwingUtilities.convertPoint(this, e.getPoint(), jp3);
            lm.setSelect(p, false);
            commit();
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }

        public void mouseDragged(MouseEvent e) {
            Point p = SwingUtilities.convertPoint(this, e.getPoint(), jp3);
            lm.setSelect(p, true);
        }

        public void mouseMoved(MouseEvent e) {
        }

        public int compare(MyLabel o1, MyLabel o2) {
            Calendar c1 = Calendar.getInstance();
            c1.set(o1.year, o2.month, o1.day);
            Calendar c2 = Calendar.getInstance();
            c2.set(o2.year, o2.month, o2.day);
            return c1.compareTo(c2);
        }
    }

    private class LabelManager {
        private List<MyLabel> list;

        public LabelManager() {
            list = new ArrayList<MyLabel>();
        }

        public List<MyLabel> getLabels() {
            return list;
        }

        public void addLabel(MyLabel my) {
            list.add(my);
        }

        public void clear() {
            list.clear();
        }

        @SuppressWarnings("unused")
        public void setSelect(MyLabel my, boolean b) {
            for (MyLabel m : list) {
                if (m.equals(my)) {
                    m.setSelected(true, b);
                } else {
                    m.setSelected(false, b);
                }
            }
        }

        public void setSelect(Point p, boolean b) {
            // 如果是拖动,则要优化一下,以提高效率
            if (b) {
                // 表示是否能返回,不用比较完所有的标签,能返回的标志就是把上一个标签和
                // 将要显示的标签找到了就可以了
                boolean findPrevious = false, findNext = false;
                for (MyLabel m : list) {
                    if (m.contains(p)) {
                        findNext = true;
                        if (m.getIsSelected()) {
                            findPrevious = true;
                        } else {
                            m.setSelected(true, b);
                        }
                    } else if (m.getIsSelected()) {
                        findPrevious = true;
                        m.setSelected(false, b);
                    }
                    if (findPrevious && findNext) {
                        return;
                    }
                }
            } else {
                MyLabel temp = null;
                for (MyLabel m : list) {
                    if (m.contains(p)) {
                        temp = m;
                    } else if (m.getIsSelected()) {
                        m.setSelected(false, b);
                    }
                }
                if (temp != null) {
                    temp.setSelected(true, b);
                }
            }
        }

    }

    private class JP4 extends JPanel {
        private static final long serialVersionUID = -6391305687575714469L;

        public JP4() {
            super(new BorderLayout());
            this.setPreferredSize(new Dimension(295, 20));
            this.setBackground(new Color(160, 185, 215));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            final JLabel jl = new JLabel("今天: " + sdf.format(new Date()));
            jl.setToolTipText("点击选择今天日期");
            this.add(jl, BorderLayout.CENTER);
            jl.addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent me) {
                    jl.setCursor(new Cursor(Cursor.HAND_CURSOR));
                    jl.setForeground(Color.RED);
                }

                public void mouseExited(MouseEvent me) {
                    jl.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                    jl.setForeground(Color.BLACK);
                }

                public void mousePressed(MouseEvent me) {
                    jl.setForeground(Color.WHITE);
                    select.setTime(new Date());
                    refresh();
                    commit();
                }

                public void mouseReleased(MouseEvent me) {
                    jl.setForeground(Color.BLACK);
                }
            });
        }

        private void updateDate() {

        }
    }

    public static void main(String[] args) {
        //创建窗体
        JFrame jf = new JFrame();

        //设置窗体大小
        jf.setSize(400, 300);
        //设置窗体标题
        jf.setTitle("日历控件");
        //设置位置,值为null,则窗体位于屏幕中央
        jf.setLocationRelativeTo(null);
        //设置窗体关闭时默认操作,窗口关闭时退出应用程序
        jf.setDefaultCloseOperation(3);
        //设置此窗口是否应始终位于其他窗口之上
        jf.setAlwaysOnTop(true);
        //取消窗体默认布局:窗体对象.setLayout(null);
        jf.setLayout(null);

        //创建日期选择器对象,指定日期字符串格式
        DateChooser dateChooser = DateChooser.getInstance("yyyy-MM-dd");

        JTextField showDateField = new JTextField("单击选择日期");
        showDateField.setBounds(50, 50, 100, 20);

        //把日历控件和文本框进行绑定
        dateChooser.register(showDateField);

        jf.add(showDateField);

        jf.setVisible(true);
    }
}

继承上面这个类:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AttendanceQuery {
       public static void main(String[] args) {
           // 创建窗体对象
           JFrame jf = new JFrame();
           jf.setTitle("考勤查询");
           jf.setSize(400,300);
           jf.setDefaultCloseOperation(3);
           jf.setLocationRelativeTo(null);
           jf.setAlwaysOnTop(true);
           jf.setLayout(null);



           // 创建考勤日期这个JLabel标签
           JLabel attendanceDateJLabel = new JLabel("考勤日期");
           attendanceDateJLabel.setBounds(50,20,100,20);
           jf.add(attendanceDateJLabel);

           // 创建开始时间这个JLabel标签
           JLabel startTimeJLabel = new JLabel("开始日期");
           startTimeJLabel.setBounds(50,70,100,20);
           jf.add(startTimeJLabel);


           DateChooser dateChooser1  = DateChooser.getInstance("yyyy/MM/dd");
           DateChooser dateChooser2 = DateChooser.getInstance("yyyy/MM/dd");


           // 创建JTextField开始时间的输入框
           JTextField inputStartDate = new JTextField();
           inputStartDate.setBounds(50,100,100,20);
           dateChooser1.register(inputStartDate);
           jf.add(inputStartDate);

           // 创建结束时间的JLabel标签
           JLabel endTimeJLabel = new JLabel("结束时间");
           endTimeJLabel.setBounds(250,70,100,20);
           jf.add(endTimeJLabel);

           // 创建结束时间的JTextField输入框
           JTextField inputEndDate = new JTextField();
           inputEndDate.setBounds(250,100,100,20);
           dateChooser2.register(inputEndDate);
           jf.add(inputEndDate);

           // 创建确定按钮
//      JButton enterJButton = new JButton("确定");
           JButton enterJButton = new JButton("确定");
           enterJButton.setBounds(250,180,60,20);
           jf.add(enterJButton);

           enterJButton.addActionListener(new ActionListener() {
               @Override
               public void actionPerformed(ActionEvent e) {
                   SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

                   // 获取开始时间和结束时间的字符串
                   String startDateString = inputStartDate.getText();
                   String endDateString = inputEndDate.getText();
                   try {
                       Date startDate = sdf.parse(startDateString);
                       Date endDate = sdf.parse(endDateString);

                      JOptionPane.showMessageDialog(jf,startDate + "\n" + endDate);
                   } catch (ParseException ex) {
                       throw new RuntimeException(ex);
                   }


               }

           });

           // 设置窗体可见
           jf.setVisible(true);

                               java基础语法API之GUI和时间类综合练习_java

                               java基础语法API之GUI和时间类综合练习_java_02

<2>案例2:时间日期显示

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ShowDateTime {
           public static void main(String[] args) {
               // 创建窗体对象
               JFrame jf = new JFrame();
               jf.setTitle("手机日期和时间显示");
               jf.setSize(400,300);
               jf.setDefaultCloseOperation(3);
               jf.setLocationRelativeTo(null);
               jf.setAlwaysOnTop(true);
               jf.setLayout(null);




               // 创建JLabel对象显示日期和时间
               JLabel dateLabel = new JLabel("日期");
               dateLabel.setBounds(50,50,100,20);
               jf.add(dateLabel);

               Date d = new Date();
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
               String dateString = sdf.format(d);


               //按照格式显示日期的字符串
               JLabel showDateLabel = new JLabel(dateString);
               showDateLabel.setBounds(50,80,200,20);
               jf.add(showDateLabel);

               SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
               String timeString = sdf2.format(d);

               // 提示时间
               JLabel timeLabel = new JLabel("时间");
               timeLabel.setBounds(50,150,100,20);
               jf.add(timeLabel);

               // 按照格式显示时间的字符串
               JLabel showTimeLabel = new JLabel(timeString);
               showTimeLabel.setBounds(50,180,200,20);
               jf.add(showTimeLabel);



               // 设置窗体可见
               jf.setVisible(true);

           }

                               java基础语法API之GUI和时间类综合练习_sed_03

标签:java,Color,void,private,public,API,new,jf,GUI
From: https://blog.51cto.com/u_15912723/9105310

相关文章

  • Java之字符流的详细解析
     3.字符流当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储。所以Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文件。3.1字符输入流【Reader】java.io.Reader抽象类是表示用于......
  • Java项目管理工具 - Maven的使用
    Maven介绍什么是MavenMaven的正确发音是[ˈmevən],而不是“马瘟”以及其他什么瘟。Maven在美国是一个口语化的词语,代表专家、内行的意思。一个对Maven比较正式的定义是这么说的:Maven是一个项目管理工具,它包含了一个项目对象模型(POM:ProjectObjectModel),一组标准集合,一个......
  • 关于JavaScript的技巧一
    1.滚动到页面顶部我们可以使用window.scrollTo()平滑滚动到页面顶部。constscrollToTop=()=>{window.scrollTo({top:0,left:0,behavior:"smooth"});};2.滚动到页面底部当然,如果知道文档的高度,也可以平滑滚动到页面底部。constscrollToBottom=()=>{wind......
  • 揭秘阿里巴巴:如何通过API实时捕获中国市场商品数据
    一、引言随着电子商务的迅猛发展,实时数据获取在商业决策中扮演着越来越重要的角色。阿里巴巴中国站作为国内领先的B2B平台,提供了丰富的API接口供开发者使用。本文将重点介绍如何通过阿里巴巴中国站的按关键字搜索商品API实现实时数据获取,并给出相应的代码示例。二、按关键字搜索商......
  • 阿里巴巴中国站1688商品详情API实时数据获取:从零基础到精通的全程指南
    一、引言随着电子商务的快速发展,实时数据获取在商业决策中扮演着越来越重要的角色。阿里巴巴中国站作为国内领先的B2B平台,提供了丰富的API接口供开发者使用。本文将重点介绍如何通过阿里巴巴中国站的1688商品详情API实现实时数据获取,并给出相应的代码示例。二、1688商品详情API介绍......
  • 使用Go语言的HTTP客户端库进行API调用
    随着微服务架构和RESTfulAPI的普及,API调用成为了日常开发中的常见任务。Go语言提供了多种工具和库来帮助开发者轻松地与API进行交互。本文将介绍如何使用Go语言的HTTP客户端库进行API调用。在Go语言中,标准库中的net/http包提供了基本的HTTP客户端功能。要使用net/http包进行API调......
  • 使用Go语言实现RESTful API
    RESTful架构是一种设计风格,用于构建网络应用程序的API。它基于HTTP协议,并使用不同的HTTP方法(如GET、POST、PUT、DELETE等)来处理不同的操作。在Go语言中,我们可以使用标准库中的net/http包来实现RESTfulAPI。下面是一个简单的示例,演示如何使用Go语言实现一个基本的RESTfulAPI:go复制......
  • 安装APISIX服务
    1、先安装依赖服务#安装epel,luarocks需要它wgethttp://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpmrpm-ivhepel-release-latest-7.noarch.rpm#添加OpenResty源yuminstallyum-utilsyum-config-manager--add-repo\https://openresty.org/packag......
  • Python实现post请求虾皮shopee商品列表API
    Shopee平台商品列表数据接口是一种用于获取Shopee平台上商品列表信息的API接口。通过调用该接口,用户可以获取到商品的基本信息,如商品ID、商品名称、商品价格等。以下是一个示例的Shopee平台商品列表数据接口的请求和返回示例:Shopee.item_search-获取虾皮商品列表数据接口返回值说明......
  • 深入分析 Java、Kotlin、Go 的线程和协程
    文章目录前言协程是什么协程的好处进程进程是什么进程组成进程特征线程线程是什么线程组成任务调度进程与线程的区别线程的实现模型一对一模型多对一模型多对多模型线程的“并发”协程协程的目的协程的特点协程的原理Java、Kotlin、Go的线程与协程Kotlin的协程使用「线程」的代......