【发布时间】:2016-07-30 06:21:31
【问题描述】:
我有这个代码
package com.company;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Alarm extends JFrame {
JFrame frame = new JFrame("Java Alarm Clock");
JMenuBar menuBar = new JMenuBar();
JMenu clock = new JMenu("Clock");
JMenu alarm = new JMenu("Alarm");
JMenu help = new JMenu("Help");
public Alarm() {
super("Java Alarm Clock");
getContentPane().setBackground(new Color(204,204,255));
setLayout(new BorderLayout());
setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
setSize(770,470);
setVisible(true);
add(new pclock() , BorderLayout.WEST);
add(menuBar , BorderLayout.NORTH);
menuBar.setBackground(new Color(204,204,255));
menuBar.add(clock);
menuBar.add(alarm);
menuBar.add(help);
}
class pclock extends JPanel {
public void paintComponent(Graphics g) {
setBackground(new Color(204,204,255));
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillOval(40, 100, 180, 180);
}
}
public static void main(String[] args) {
new Alarm();
}
}
但是当我运行代码时,圆圈没有出现 只有当我更改此代码时才会出现
add(new pclock() , BorderLayout.WEST);
到
add(new pclock());
那么如何让它像那张图片一样出现在左侧? 谢谢
【问题讨论】:
-
你能告诉我们它目前的样子,即你看到的问题吗?
-
public void paintComponent(Graphics g) { setBackground(new Color(204,204,255)); super.paintComponent(g);对setBackground(new Color(204,204,255));的调用应该在构造函数中。它只需要完成一次,我们不应该在paint方法中更改组件的状态,因为这会触发repaint()! -
setVisible(true);应该是Alarm构造函数中的最后一个方法调用。
标签: java swing components layout-manager border-layout