- 论坛徽章:
- 0
|
这个是的代码,但是b2的action改变不了颜色,怎么办?急啊。。。。。。。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.*;
class FontFrame extends JFrame {
Color color=Color.red;
String title="";
TitledBorder inputTitledBorder;
FontFrame(){
initButton();
initMenu();
}
void initButton(){
pane.add(b1);
pane.add(b2);
pane.add(b3);
pane.add(b4);
b1.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
}
});
b2.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
//这么写改变不了标题的颜色
FontFrame f=new FontFrame();
f.setForeground(Color.red);
}
});
b3.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
}
});
b4.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
setContentPane(pane);
pane.add(label);
setVisible(true);
}
JLabel label=new JLabel("Welcome to JAVA World!",JLabel.CENTER);
JPanel pane = new JPanel();
Font font1 ;
Font font2 ;
JButton b1 = new JButton("字型");
JButton b2 = new JButton("颜色");
JButton b3 = new JButton("背景颜色");
JButton b4 = new JButton("退出");
JTextPane text=new JTextPane(); //
JDialog about=new JDialog(this); //关于对话框
JMenuBar menubar=new JMenuBar();//菜单
Graphics g;
JMenu[] menus=new JMenu[]{
new JMenu("File"),
new JMenu("Font")
};
JMenuItem menuitems[][]=new JMenuItem[][]{{
new JMenuItem("exit")
},
{
new JMenuItem("Fond"),
new JMenuItem("CFG"),
new JMenuItem("CBG")
}
};
void initMenu(){
for(int i=0;i<menus.length;i++){
menubar.add(menus[i]);
for(int j=0;j<menuitems[i].length;j++){
menus[i].add(menuitems[i][j]);
menuitems[i][j].addActionListener( action );
}
}
this.setJMenuBar(menubar);
}
ActionListener action=new ActionListener(){ //when here have not wrong:
public void actionPerformed(ActionEvent e){
JMenuItem mi=(JMenuItem)e.getSource();
String id=mi.getText();
if(id.equals("exit")){
FontFrame f=new FontFrame();
int s=JOptionPane.showConfirmDialog(f,"你真的要结束吗","结束程序",JOptionPane.YES_NO_CANCEL_OPTION);
if(s==JOptionPane.YES_OPTION)
System.exit(0);
}else if(id.equals("Fond")){
font1 =new Font("黑体",Font.ITALIC|Font.BOLD,14);
label.setFont(font1) ;
}else if(id.equals("CFG")){
label.setForeground(Color.YELLOW);
}
else if(id.equals("CBG")){
label.setForeground(Color.BLUE);
}
}
};
}
public class SetFont{
public static void main(String args[]){
FontFrame f=new FontFrame();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
FontFrame f=new FontFrame();
int s=JOptionPane.showConfirmDialog(f,"你真的要结束吗","结束程序",JOptionPane.YES_NO_OPTION);
if(s==JOptionPane.YES_OPTION)
System.exit(0);}
});
f.setTitle("字体设置");
f.setSize(800,600);
f.show();
}
} |
|