java doc里面是这么写的setBackground
public void setBackground(Color bg)
Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.
It is up to the look and feel to honor this property, some may choose to ignore it. 代码:
// the main() method is ignored.public MyFrame extends JFrame
{ //set ...
MyComponent cmp=new MyComponent();
// '1'
add(cmp);
}class MyComponent extends JComponent
{ public void paintComponent(Graphics gr)
{ setBackground(Color.RED);
}
}然后背景颜色还是没有变在代码中 ‘1’处加上 cmp.setOpaque(true); 也没有用 (貌似要求都符合了--opaque;override paintComponent() )如果不是JComponet 而是JPanel 就没有问题那么如果我想用JComponent 怎么设置背景颜色呢?
public void setBackground(Color bg)
Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.
It is up to the look and feel to honor this property, some may choose to ignore it. 代码:
// the main() method is ignored.public MyFrame extends JFrame
{ //set ...
MyComponent cmp=new MyComponent();
// '1'
add(cmp);
}class MyComponent extends JComponent
{ public void paintComponent(Graphics gr)
{ setBackground(Color.RED);
}
}然后背景颜色还是没有变在代码中 ‘1’处加上 cmp.setOpaque(true); 也没有用 (貌似要求都符合了--opaque;override paintComponent() )如果不是JComponet 而是JPanel 就没有问题那么如果我想用JComponent 怎么设置背景颜色呢?
而是说,你调用此变量,绘制背景.自己来实现背景色的填充eg:public void paintComponent(Graphics gr)
{
gr.setColor(getBackground());//设置当前颜色为背景色
gr.fillRect(0,0,getWidth(),getHeight());//填充
}
不用fillRect (填充矩阵 而且不是Java 2D的)
想用setBackground method
难道你认为,这个方法在任何时候,任何类中都会执行?
我想,应该不会有人这样告诉你吧。
JFrame 里面的 add() method 啊 Java SE 5 里面说可以直接用 JFrame.add()
1L 中的 java doc里面 “Direct subclasses of JComponent must override paintComponent to honor this property.” 那应该怎么用setBackground()能够使之改变背景颜色
http://apicode.gicp.net/class.do?api=selectByfatherIndex&father=255
http://apicodecn.gicp.net/class.do?api=selectByfatherIndex&father=255
要想设置背景其实不用重写,下面这样就可以了
MyComponent cmp=new MyComponent();
cmp.setBackground(......
add(cmp);如果需要实现其他界面效果才需要下面重写,比如swing都是长方形边框,
那么可以重写paint方法或者paintComponent方法重画边框样子
class MyComponent extends JComponent
{ public void paintComponent(Graphics gr)
{ gr.setColor(Color.RED);
gr.draw.......
}
}
(想用JComponent 以及 setBackground() )
怎么实现呢?
{ //set ...
MyComponent cmp=new MyComponent();
// '1'
add(cmp);
}
我是没有看明白,这个是声明了一个类?还是定义了一个函数?当然这也许是你粘贴时候没有注意的错误。
我现在才看明白你究竟想要什么。
首先可以明确,你给你的继承自JComponent的有类设置背景色,前景色和字体,就必须完成它的UI。因为每一种组件都对应自己的ui。例如,JPanel,有PanelUI,这是一个抽象类。而实际使用的是它的子类,如BasicPanelUI。还有其他的请自己查阅文档。
而颜色字体都是在ui里面去展现的。
但是,JComponent有一些不同之处。它有ComponentUI,一个抽象类,却没有什么BasicComponentUI。
而且在处理字体颜色的
public void installUI(JComponent c) {
}
这个方法,你可以看到,是空实现。所以,如果你希望JComponent能够处理颜色字体,就需要额外处理。
而且,你可以看到ButtonUI,PanelUI,LabelUI等等都是ComponentUI的子类,虽然它们也是抽象的。
因此,简便些的做法是你去使用这些对于颜色的处理实现过的类,也推荐这样,如果你不是很了解内部机制。当然如果你很牛,也无妨。。
好像,另外有一个关于InputMap的帖子也是你的吧。我写了一小段代码,很简单。但是,应该理解的是原理。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.LookAndFeel;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicPanelUI;public class JbuttonInputMap extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private TComponent comp = new TComponent();
private JLabel l = new JLabel(); public JbuttonInputMap() {
super();
Container c = getContentPane(); JPanel p = new JPanel();
p.setBackground(Color.green);
p.setLayout(new FlowLayout());
JButton bt1 = new JButton("Button1"); InputMap map1 = bt1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
KeyStroke ks1 = KeyStroke.getKeyStroke(KeyEvent.VK_F,
KeyEvent.CTRL_DOWN_MASK + KeyEvent.SHIFT_DOWN_MASK);
map1.put(ks1, "ks1");
ActionMap actionMap1 = bt1.getActionMap();
Action action1 = new AbstractAction() { /**
*
*/
private static final long serialVersionUID = 1L; @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("1");
comp.repaint();
}
};
actionMap1.put("ks1", action1); bt1.addActionListener(action1);
p.add(bt1); JButton bt2 = new JButton("Button2");
InputMap map2 = bt2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
KeyStroke ks2 = KeyStroke.getKeyStroke(KeyEvent.VK_L,
KeyEvent.CTRL_DOWN_MASK + KeyEvent.SHIFT_DOWN_MASK);
map2.put(ks2, "ks2");
ActionMap actionMap2 = bt2.getActionMap();
Action action2 = new AbstractAction() { /**
*
*/
private static final long serialVersionUID = 1L; @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("2");
l.setBackground(Color.blue);
}
};
actionMap2.put("ks2", action2);
bt2.addActionListener(action2);
p.add(bt2); l.setBackground(Color.red);
l.setText("ABC");
l.setOpaque(true);
p.add(l); c.add(p, BorderLayout.SOUTH);
c.add(comp); setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
} public static void main(String[] args) {
// new JbuttonInputMap().requestFocus();
new JbuttonInputMap();
} private class TComponent extends JComponent {
public TComponent() {
super();
setUI(new ComponentUI() {
@Override
public void installUI(JComponent c) {
// TODO Auto-generated method stub
super.installUI(c);
LookAndFeel.installColors(c, "Panel.background",
"Panel.foreground");
}
});
this.setOpaque(true);
this.setBackground(Color.blue); this.setLayout(new FlowLayout());
add(new JLabel("OK"));
}
}
}
LookAndFeel.installColorsAndFont(p,
"Panel.background",
"Panel.foreground",
"Panel.font");
LookAndFeel.installBorder(p,"Panel.border");
LookAndFeel.installProperty(p, "opaque", Boolean.TRUE);
} protected void uninstallDefaults(JPanel p) {
LookAndFeel.uninstallBorder(p);
}
应该是缺少ui。仅此而已。那个installUI是做初始化设定的。
所以,如果你想开始的时候默认有一个颜色,可以在这里设置。
如果你想就如其他一样设置背景,只要
XXX.setUI(new ComponentUI(){});
ComponentUI是抽象类,但没有抽象方法。