1个回答
展开全部
如果认可代码,请加分到100,
顺带学习下方法覆盖,AbstractAction的用法,简练的程序代码风格,完全值你所付出的100分
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
final public class ColorSelectDemo extends JFrame {
private static final Dimension preferredSize = new Dimension(210,50);
public ColorSelectDemo() throws HeadlessException {
getContentPane().add(
new JButton(new ChooseColorAction("Choose Color ...")),
BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
static private class ChooseColorAction extends AbstractAction{
public ChooseColorAction(String name) {
super(name);
putValue(AbstractAction.SHORT_DESCRIPTION,"select color as foreground");
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof Component) {
Component cmp = (Component)source;
cmp.setForeground(JColorChooser.showDialog(
cmp,"select foreground color",
cmp.getForeground()) );
}
}
}
public String getTitle() {return "Color chooser Demo";}
public Dimension getSize() {return preferredSize;}
public Dimension getPreferredSize() {return preferredSize;}
public Dimension getMinimumSize() {return preferredSize;}
public static void main(String[] args) {new ColorSelectDemo();}
}
顺带学习下方法覆盖,AbstractAction的用法,简练的程序代码风格,完全值你所付出的100分
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
final public class ColorSelectDemo extends JFrame {
private static final Dimension preferredSize = new Dimension(210,50);
public ColorSelectDemo() throws HeadlessException {
getContentPane().add(
new JButton(new ChooseColorAction("Choose Color ...")),
BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
static private class ChooseColorAction extends AbstractAction{
public ChooseColorAction(String name) {
super(name);
putValue(AbstractAction.SHORT_DESCRIPTION,"select color as foreground");
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source instanceof Component) {
Component cmp = (Component)source;
cmp.setForeground(JColorChooser.showDialog(
cmp,"select foreground color",
cmp.getForeground()) );
}
}
}
public String getTitle() {return "Color chooser Demo";}
public Dimension getSize() {return preferredSize;}
public Dimension getPreferredSize() {return preferredSize;}
public Dimension getMinimumSize() {return preferredSize;}
public static void main(String[] args) {new ColorSelectDemo();}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询