0
我有这样的代码组件没有出现在西部边界布局
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);
改变
它只会出现0add(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'构造函数中的最后一个方法调用。 –