QA@IT

java(swing)でカレンダーアプリの作成 土日祝の文字色を変更するには?

12523 PV
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Calendar;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class debugTest {
 public static void main(String[] args) {
  JFrame frame = new JFrame("Swing Calender");
  frame.add(new SwingCalendar());
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
  frame.setVisible(true);
 }
}

class SwingCalendar extends JPanel {
 private JLabel  monthLabel = new JLabel();
 private JLabel  yearLabel = new JLabel();
 Calendar cal = Calendar.getInstance();

 //曜日、日付ラベル作成
 private JLabel[]    weekLabels = new JLabel[7];
 private JLabel[][]  dayLabels = new JLabel[6][7];

 //各パーツのColorの作成
 private static final Color WEEK_BG = new Color(102,102,102);
 //private static final LineBorder WEEK_BORDER = new LineBorder(new  Color(160,160,230), 2, false);

 //日付パネルの色
 private static final Color DAY_BG = new Color(102,102,102);
 private static final Color DAY_FG = Color.BLACK;
 private static final LineBorder DAY_BORDER = new LineBorder(new Color(102,102,102), 2, false);

 //日付のLabel
 private static final Dimension DAY_LABEL_SIZE = new Dimension(50,50);
 private static final String[] WEEK_NAMES = new String[] {"日", "月", "火", "水", "木", "金", "土"};
 private static final String[] MONTH_NAMES = new String[] {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};

 public SwingCalendar() {
 try {
 UIManager.setLookAndFeel(
 "javax.swing.plaf.nimbus.NimbusLookAndFeel"
 );
 } catch (ClassNotFoundException | InstantiationException
 | IllegalAccessException | UnsupportedLookAndFeelException e) {
 e.printStackTrace();
 }

 /* MONTH Label */
 Dimension dm = monthLabel.getPreferredSize();
 dm.width += 10; //dm.height += 1;
 monthLabel.setPreferredSize(dm);
 monthLabel.setForeground(DAY_FG);

 /* YEAR Label */
 Dimension dy = yearLabel.getPreferredSize();
 dy.width += 10; //dy.height += 1;
 yearLabel.setPreferredSize(dy);
 yearLabel.setForeground(DAY_FG);

 /* DAY PANEL */
 JPanel dayPanel = new JPanel();
 GridLayout layout = new GridLayout(7, 7);
 layout.setHgap(0);
 layout.setVgap(0);
 dayPanel.setLayout(layout);
 dayPanel.setBackground(DAY_BG);

 //曜日の配列
 for( int i=0; i<7; i++) {
  weekLabels[i] = new JLabel(WEEK_NAMES[i]);
  weekLabels[i].setHorizontalAlignment(SwingConstants.CENTER);
  weekLabels[i].setVerticalAlignment(SwingConstants.CENTER);
  weekLabels[i].setPreferredSize(DAY_LABEL_SIZE);
  weekLabels[i].setOpaque(true);
  weekLabels[i].setBackground(WEEK_BG);
 
  if( i == 0 ) {
    weekLabels[i].setForeground(Color.RED);
  } else if ( i == 6 ) {
    weekLabels[i].setForeground(Color.BLUE);
  }

  System.out.println("kakunin" + i);
  dayPanel.add(weekLabels[i]);
 }

 //日付の配列
 for( int i=0; i<6; i++) {
  for( int j=0; j<7; j++) {
   dayLabels[i][j] = new JLabel(); //String.valueOf(i),(j)
   dayLabels[i][j].setHorizontalAlignment(SwingConstants.CENTER);
   dayLabels[i][j].setVerticalAlignment(SwingConstants.CENTER);
   dayLabels[i][j].setOpaque(true);
   dayLabels[i][j].setBackground(DAY_BG);
   dayLabels[i][j].setPreferredSize(DAY_LABEL_SIZE);
   System.out.println("i" + i); //確認
   System.out.println("j" + j); //確認


    if( i == 0 && j == 0 ) {
      dayLabels[i][j].setForeground(Color.RED);
    } else if ( i == 6 && j == 0 ) {
      dayLabels[i][j].setForeground(Color.BLUE);
    }

      dayPanel.add(dayLabels[i][j]);
   }

  }

  JPanel monthPanel = new JPanel();
  monthPanel.setLayout(new GridLayout());
  monthPanel.add(yearLabel);
  monthPanel.add(monthLabel);
  monthPanel.setBackground(DAY_BG);

  setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  GridLayout gl = new GridLayout(1,2);
  gl.setHgap(10); gl.setVgap(10);

  //年月ボタン表示パネル、日にちパネルの追加
  add(monthPanel);
  add(dayPanel);

 }

}

回答

ウォッチ

この質問への回答やコメントをメールでお知らせします。