62,614
社区成员
发帖
与我相关
我的任务
分享
// 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);
}
}
protected void installDefaults(JPanel p) {
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);
}
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"));
}
}
}
public void paintComponent(Graphics gr)
{
gr.setColor(getBackground());//设置当前颜色为背景色
gr.fillRect(0,0,getWidth(),getHeight());//填充
}