我要单击颜色按钮是改变super("字体设置的")颜色,怎么办呢?
import java.awt.*;
import java.awt.event.*; import javax.swing.*; import java.io.*;
class FontFrame extends JFrame {
Color color=Color.red; FontFrame(){
super("字体设置");
initAboutDialog();
initButton();
initMenu();
} void initButton(){
pane.add(b1);
pane.add(b2);
pane.add(b3);
pane.add(b4);
b3.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
font2 =new Font("黑体",Font.ITALIC|Font.BOLD,14);
///////我把super(“字体设置”)的字体进行改变,怎么办?
}
});
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);}} };
void initAboutDialog(){
about.getContentPane().add(new JLabel("我的程序"));
about.setModal(true);
about.setSize(200,100);
}
} 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.setSize(800,600);
f.show();
}
}
import java.awt.*;
import java.awt.event.*; import javax.swing.*; import java.io.*;
class FontFrame extends JFrame {
Color color=Color.red; FontFrame(){
super("字体设置");
initAboutDialog();
initButton();
initMenu();
} void initButton(){
pane.add(b1);
pane.add(b2);
pane.add(b3);
pane.add(b4);
b3.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{
font2 =new Font("黑体",Font.ITALIC|Font.BOLD,14);
///////我把super(“字体设置”)的字体进行改变,怎么办?
}
});
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);}} };
void initAboutDialog(){
about.getContentPane().add(new JLabel("我的程序"));
about.setModal(true);
about.setSize(200,100);
}
} 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.setSize(800,600);
f.show();
}
}
如果项目要求必须改Title字体,你就得自己模拟一个Title,写一个setFont(Font font)方法
你自己做个panel,放到原来的标题栏位置,然后用自定义的字体