久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

JOptionPane 在 Java 中顯示 HTML 問題

JOptionPane displaying HTML problems in Java(JOptionPane 在 Java 中顯示 HTML 問題)
本文介紹了JOptionPane 在 Java 中顯示 HTML 問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

好的,我有這段代碼,它會提示用戶一個月和一年,并打印該月的日歷.不過我遇到了一些問題.

Okay, so I have this code, it prompts a month and a year from the user and prints the calendar for that month. I'm having some problems though.

  1. HTML 字體編輯只影響月份.
  2. 星期幾在列中未正確對齊.

謝謝!

package calendar_program;

import javax.swing.JOptionPane;

public class Calendar {

public static void main(String[] args) {
    StringBuilder result=new StringBuilder();

    // read input from user
    int year=getYear();
    int month=getMonth();

    String[] allMonths={
            "", "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December"
    };

    int[] numOfDays= {0,31,28,31,30,31,30,31,31,30,31,30,31};

    if (month == 2 && isLeapYear(year)) numOfDays[month]=29;

    result.append("    "+allMonths[month]+ "  "+ year+"
"+" S  M  Tu  W Th  F  S"+"
");

    int d= getstartday(month, 1, year);

    for (int i=0; i<d; i++){
        result.append("    ");
                // prints spaces until the start day
    }
    for (int i=1; i<=numOfDays[month];i++){
        String daysSpace=String.format("%4d", i);
        result.append(daysSpace);
        if (((i+d) % 7==0) || (i==numOfDays[month])) result.append("
");
    }
    //format the final result string
    String finalresult= "<html><font face='Arial'>"+result; 
    JOptionPane.showMessageDialog(null, finalresult);
}

//prompts the user for a year
public static int getYear(){
    int year=0;
    int option=0;
    while(option==JOptionPane.YES_OPTION){
        //Read the next data String data
        String aString = JOptionPane.showInputDialog("Enter the year (YYYY) :");
        year=Integer.parseInt(aString);
        option=JOptionPane.NO_OPTION;
    }
    return year;
}

//prompts the user for a month
public static int getMonth(){
    int month=0;
    int option=0;
    while(option==JOptionPane.YES_OPTION){
        //Read the next data String data
        String aString = JOptionPane.showInputDialog("Enter the month (MM) :");
        month=Integer.parseInt(aString);
        option=JOptionPane.NO_OPTION;
    }
    return month;
}

//This is an equation I found that gives you the start day of each month
public static int getstartday(int m, int d, int y){
    int year = y - (14 - m) / 12;
    int x = year + year/4 - year/100 + year/400;
    int month = m + 12 * ( (14 - m) / 12 ) - 2;
    int num = ( d + x + (31*month)/12) % 7;
    return num;
}

//sees if the year entered is a leap year, false if not, true if yes
public static boolean isLeapYear (int year){
    if ((year % 4 == 0) && (year % 100 != 0)) return true;
    if (year % 400 == 0) return true;
    return false;
}
}

推薦答案

這是一個相當愚蠢的想法.

Here's a rather stupid idea.

而不是使用空格來格式化您的結(jié)果,這可能會受到可變寬度字體的各個字體寬度變化的影響...改用 HTML 表格,或 JTableJXMonthView 來自 SwingX 項目

Rather then formatting your results using spaces, which may be affected by variance in the individual font widths of a variable width font...use a HTML table instead, or a JTable, or JXMonthView from the SwingX project

HTML 表格

String dayNames[] = {"S", "M", "Tu", "W", "Th", "F", "S"};
result.append("<html><font face='Arial'>");
result.append("<table>");
result.append("<tr>");
for (String dayName : dayNames) {
    result.append("<td align='right'>").append(dayName).append("</td>");
}
result.append("</tr>");
result.append("<tr>");
for (int i = 0; i < d; i++) {
    result.append("<td></td>");
}
for (int i = 0; i < numOfDays[month]; i++) {
    if (((i + d) % 7 == 0)) {
        result.append("</tr><tr>");
    }
    result.append("<td align='right'>").append(i + 1).append("</td>");
}
result.append("</tr>");
result.append("</table>");

result.append("</html>");

JTable 示例

MyModel model = new MyModel();

List<String> lstRow = new ArrayList<String>(7);
for (int i = 0; i < d; i++) {
    lstRow.add("");
}
for (int i = 0; i < numOfDays[month]; i++) {
    if (((i + d) % 7 == 0)) {
        model.addRow(lstRow);
        lstRow = new ArrayList<String>(7);
    }
    lstRow.add(Integer.toString(i + 1));
}

if (lstRow.size() > 0) {
    while (lstRow.size() < 7) {
        lstRow.add("");
    }
    model.addRow(lstRow);
}

JTable table = new JTable(model);
// Kleopatra is so going to kill me for this :(
Dimension size = table.getPreferredScrollableViewportSize();
size.height = table.getRowCount() * table.getRowHeight();
table.setPreferredScrollableViewportSize(size);

JOptionPane.showMessageDialog(null, new JScrollPane(table));

public static class MyModel extends AbstractTableModel {

    public static final String[] DAY_NAMES = {"S", "M", "Tu", "W", "Th", "F", "S"};
    private List<List<String>> lstRowValues;

    public MyModel() {
        lstRowValues = new ArrayList<List<String>>(25);
    }

    @Override
    public int getRowCount() {
        return lstRowValues.size();
    }

    @Override
    public String getColumnName(int column) {
        return DAY_NAMES[column];
    }

    @Override
    public int getColumnCount() {
        return 7;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        List<String> rowData = lstRowValues.get(rowIndex);
        return rowData.get(columnIndex);
    }

    public void addRow(List<String> lstValues) {
        lstRowValues.add(lstValues);

        fireTableRowsInserted(getRowCount(), getRowCount());
    }
}

或者你可以去看看JXMonthView

這篇關(guān)于JOptionPane 在 Java 中顯示 HTML 問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 欧美又大又硬又粗bbbbb | 国产免费一区二区三区在线观看 | 亚洲成人a v | 天天操网站 | 国产区在线| 欧美福利在线 | 亚洲av毛片成人精品 | 欧美一区二区在线观看 | 久久久久网 | 国产一级黄色录像 | 九九精品在线观看 | 国产激情视频在线 | 色综合色综合色综合 | 免费色片 | 日本三级香港三级 | 精东影业一区二区三区 | 国产精品毛片一区视频播 | 亚洲天堂国产 | 成人国产网站 | 成人一区二区三区四区 | 日韩精品一区二区三区四区 | 黄色av大全 | 这里只有精品视频在线观看 | 教室脔到她哭h粗话h好爽视频 | av在线播放观看 | 91三级视频 | 国产亚洲欧美日韩高清 | 日韩欧美一区在线 | 欧美精品在线免费观看 | 日韩在线精品 | 日韩一区二区三区四区 | 亚洲一级黄色片 | 国产涩涩| av免费在线观看网站 | 中文字幕第7页 | 国产伦精品一区二区三区免费视频 | av网站在线免费观看 | 国产精品久久久久久久久借妻 | 国产成人精品一区二 | 三级久久久 | 久久精品一区二区 |