2
JFrameで縦線を1週間に別々の日に描画しようとしています。コードはエラーとしてはうまくないようですが、実行すると下の図のようなフレームが出力されます。私は何か不足していますか?Java、Swing - JFrameに線を描くことができません
public class WeekToView extends JFrame{
public WeekToView(){
setTitle("Sheffield Dental Care"); //set title
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenDimensions = toolkit.getScreenSize();
setLocation(new Point(screenDimensions.width*1/4, screenDimensions.height*1/4)); //set location based on screen size
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
getContentPane().add(scrPane);
double size[][] = {{150, 150, 150, 150, 150}, // Columns
{100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100}}; // Rows
container.setLayout(new TableLayout(size));
String daysInWeek[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
JLabel daysInWeekLabels[] = new JLabel[daysInWeek.length];
for (int i = 0; i < daysInWeek.length; i++) {
daysInWeekLabels[i] = new JLabel(daysInWeek[i],SwingConstants.CENTER);
}
container.add(daysInWeekLabels[0], "0,0");
container.add(daysInWeekLabels[1], "1,0");
container.add(daysInWeekLabels[2], "2,0");
container.add(daysInWeekLabels[3], "3,0");
container.add(daysInWeekLabels[4], "4,0");
setSize(780,600); //set size based on screen size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false); //unresizable
setVisible(true);
}
public void paintComponent(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine(getWidth()/5,0,getWidth()/5,getHeight());
g2.drawLine(getWidth()*2/5,0,getWidth()*2/5,getHeight());
g2.drawLine(getWidth()*3/5,0,getWidth()*3/5,getHeight());
g2.drawLine(getWidth()*4/5,0,getWidth()*5/5,getHeight());
}
}
'container.setLayout(new TableLayout(size));'より早くヘルプをするには、[MCVE]または[ショート、自己完結型、正しい例](http://www.sscce.org/)を投稿してください。 –
'public void paintComponent(Graphics g){' to '@Override public void paintComponent(Graphics g){'答えの開始点です。 –