JOptionPane displaying HTML problems in Java

前端 未结 2 1523
醉话见心
醉话见心 2020-12-06 23:40

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. the HTML fon
2条回答
  •  再見小時候
    2020-12-07 00:19

    Here's a rather stupid idea.

    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 Table

    enter image description here

    String dayNames[] = {"S", "M", "Tu", "W", "Th", "F", "S"};
    result.append("");
    result.append("");
    result.append("");
    for (String dayName : dayNames) {
        result.append("");
    }
    result.append("");
    result.append("");
    for (int i = 0; i < d; i++) {
        result.append("");
    }
    for (int i = 0; i < numOfDays[month]; i++) {
        if (((i + d) % 7 == 0)) {
            result.append("");
        }
        result.append("");
    }
    result.append("");
    result.append("
    ").append(dayName).append("
    ").append(i + 1).append("
    "); result.append("");

    JTable Example

    enter image description here

    MyModel model = new MyModel();
    
    List lstRow = new ArrayList(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(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> lstRowValues;
    
        public MyModel() {
            lstRowValues = new ArrayList>(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 rowData = lstRowValues.get(rowIndex);
            return rowData.get(columnIndex);
        }
    
        public void addRow(List lstValues) {
            lstRowValues.add(lstValues);
    
            fireTableRowsInserted(getRowCount(), getRowCount());
        }
    }
    

    Or you can go have a look at JXMonthView

提交回复
热议问题