云云众生415 2022-12-07 08:36 采纳率: 100%
浏览 63
已结题

Java GUI 小白求助

Java GUI
小白一枚,刚学的,期末作业有个问题 一直至达不到想要的要求,求大佬支援!!!万分感谢!!!
怎么才能把控制台输出的内容,放到GUI的蓝灰色方框里正常显示呢?
问了好多人,试了很多方法都不行,救救孩子吧

主函数在这
public class work implements ActionListener{
    
    JFrame ck = new JFrame("大作业");
    ImageIcon imgIcon=new ImageIcon("3.png");//设置默认图标
    JTextArea xianshi = new JTextArea();
    Font z1 = new Font("宋体",Font.BOLD,22);
    JLabel b1  = new JLabel("年份");
    JLabel b2  = new JLabel("月份");
    JButton a1 = new JButton("确定");    
    static JTextField sr1 = new JTextField("例如:2023",10);
    static JTextField sr2 = new JTextField("例如:9",10);
    public work() {
        
        Container con = ck.getContentPane();    //创建容器,
        con.setBackground(Color.white);         //并设置容器背景颜色
        
        
        xianshi.setBackground(new Color(208,223,230));
        
        b1.setFont(z1);
        
        b2.setFont(z1);
        
        a1.setBounds(470,30,80,30);  //确定按钮的位置,大小
        
        b1.setBounds(120,30,100,30);       //确定按钮的位置
        b2.setBounds(290,30,100,30);//确定的位置
        sr2.setBounds(350,30,100,30);//确定的位置
        xianshi.setBounds(100,100,500,200);//确定的位置
        sr1.setBounds(180,30,100,30);//确定的位置
        ck.add(xianshi);
        ck.add(b1);
        ck.add(b2);
        ck.add(sr1);
        ck.add(a1);                  //添加组件
        ck.add(sr2);
        ck.setLayout(null);       //将容器的布局设为绝对布局
        ck.setSize(700,400);      //  窗口大小
        ck.setLocationRelativeTo(null);   //显示为中
        ck.setVisible(true);   //设置可见模式
        ck.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );  //点击窗体关闭
        ck.setResizable(false);    //窗口不可更改大小
        ck.setIconImage(imgIcon.getImage());
        sr1.addMouseListener(new MouseHandle());
        sr2.addMouseListener(new MouseHandle());
        a1.addActionListener(this);
        
        
        
        
    
        
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        xianshi.setText("");
        int year,month;
        year =Integer.parseInt(sr1.getText()); 
        month =Integer.parseInt(sr2.getText()); 
        //xianshi.append(String.valueOf(new fangfa( year, month)));
        xianshi.setText(String.valueOf(new fangfa( year, month))); 
        
    }
}

public class fangfa {
    public fangfa (int x,int y) {
        int year  =x;
        int month =y;
        int totalDays = getFromNowYearTo1900TotalDays(year);

        //、获得当前年份所经过的天数
        int totalDaysThisYear = getNowYearPassedTotalDays(year,month);

        //、求得给定月份第一天的星期数
        int week = (totalDays + totalDaysThisYear +1) % 7;//星期日是,week = 0

        //、格式化输出日历
        formatCalendarOutput(week,year,month);
    }

    /**
    *格式化输出日历
    *@param week:当前月份第一天的星期数
    *@param year:当前年份
    *@param month:当前月份
    */
    public static void formatCalendarOutput(int week,int year,int month){
        int cnt = 0;//计数器,记录空白数和日期数的和
        //1) 打印表头
        System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
        //2)打印空白(观察星期与之前空个数之间的规律)
        for(int i=1;i<= week;i++){
            System.out.print("\t");
            cnt++;
        }
        //3) 打印日历
        for(int i=1;i<=getNowMonthDays(year,month);i++){
            System.out.print(i+"\t");
            cnt++;
            //若记录空白数和日期数的和是七的倍数,应该换行输出
            if(cnt % 7 == 0){
                System.out.println();
            }
        }    
    }
    /**
    *判断给定的年份是否为闰年
    *@param year:给定的年份
    *@return true:闰年;false:平年
    */
    public static boolean isLeapYear(int year){
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);    
    }

    /**
    *根据参数指定的年份,月份,求出当前月的总天数
    *@param year:年份
    *@param month:月份
    *@return 月的总天数
    */
    public static int getNowMonthDays(int year,int month){//year:设计该参数的原因,2月份根据年份是否是闰年来确定其天数的
        switch(month){
            case 2:
                return isLeapYear(year) ? 29 : 28;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            default:
                return 31;
        }
    }

    /**
    *获得当前年份的上一年距离1900年所经过的总天数
    *@param year 当前年份
    *@return 总天数
    */
    public static int getFromNowYearTo1900TotalDays(int year){
        int totalDays = 0;
        for(int i = 1900; i< year; i++){//i:年份
            totalDays += isLeapYear(i) ? 366 : 365;
        }
        return totalDays;
    }

    /**
    *求出当前年份经过的总天数(从当前年的1月1日到给定月份的上一个月最末一天)
    *@param year:年份
    *@param month:月份
    *@return 总天数
    */
    public static int getNowYearPassedTotalDays(int year,int month){
        int totalDays = 0;
        for(int i=1;i< month;i++){//i:月份
            totalDays += getNowMonthDays(year,i);
        }
        return totalDays;
    }  
        
        
    }
    
引用的大佬的想法

img

  • 写回答

2条回答 默认 最新

  • Huazie 全栈领域优质创作者 2022-12-07 09:44
    关注

    参考我在这篇问答里的回复

    我写了一个小demo,如下:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.PrintStream;
    
    /**
     * @author huazie
     * @version 2.0.0
     * @since 2.0.0
     */
    public class MyJFrame extends JFrame implements ActionListener {
        JButton jButton = new JButton("点我");
    
        JTextArea jTextArea = new JTextArea(10, 10);
    
        public MyJFrame() {
            initJframe();
            FlowLayout layout = new FlowLayout();
            this.setLayout(layout);
    
            jButton.setBounds(0, 0, 100, 50);
            jButton.addActionListener(this);
    
            this.add(jButton);
            this.add(jTextArea);
    
            OutputStream textAreaStream = new OutputStream() {
                public void write(int b) throws IOException {
                    jTextArea.append(String.valueOf((char) b));
                }
    
                public void write(byte b[]) throws IOException {
                    jTextArea.append(new String(b));
                }
    
                public void write(byte b[], int off, int len) throws IOException {
                    jTextArea.append(new String(b, off, len));
                }
            };
            PrintStream myOut = new PrintStream(textAreaStream);
            System.setOut(myOut);
            System.setErr(myOut);
        }
    
        private void initJframe() {
            this.setSize(500, 500);
            this.setAlwaysOnTop(true);
            this.setTitle("done.MyJFrame");
            this.setLocationRelativeTo(null);
            this.setDefaultCloseOperation(3);
            this.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            Object sourse = e.getSource();
            if (sourse == jButton) {
                System.out.println("hello world");
            }
        }
    
        public static void main(String[] args) {
            new MyJFrame();
        }
    }
    
    
    

    img


    如有帮助,欢迎采纳哈!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月15日
  • 已采纳回答 12月7日
  • 创建了问题 12月7日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵