JOptionPane 在 Java 中显示 HTML 问题

JOptionPane displaying HTML problems in Java(JOptionPane 在 Java 中显示 HTML 问题)
本文介绍了JOptionPane 在 Java 中显示 HTML 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

好的,我有这段代码,它会提示用户一个月和一年,并打印该月的日历.不过我遇到了一些问题.

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.

而不是使用空格来格式化您的结果,这可能会受到可变宽度字体的各个字体宽度变化的影响...改用 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

这篇关于JOptionPane 在 Java 中显示 HTML 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)