以下代碼分別在4個java文件中
java程序,編寫一個JDialog的子類FontDialog,該類為FontFamily對象維護的數據視圖,要求FontDialog對象使用下拉列表顯示FontFamily對象維護的全部字體的名稱,當選擇下拉列表中某個字體名稱後,FongDialog對象使用標簽顯示字體的效果。要求對話框提供返回下拉列表所選擇的字體名稱的方法
編寫一個窗口,該窗口有“設置字體”按鈕和一個文本區對象,當單擊按鈕時,彈出一個FontDialog對話框,然後根據用戶在下拉列表中選擇的字體來顯示文本區中的文本。
package test;
public class E9_2 {
public static void main(String args[]){
window1 window = new window1();
}
}
package test;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fontdialog extends JDialog implements ActionListener,ItemListener{
JComboBox name,size;
JPanel north,center,south;
JButton confirm,cancel;
Label label;
String allnames[];
int sizes[];
String font_name = "Adobe Arabic";
int font_size = 24;
Font f,font;
int tag;
Fontdialog(){
setModal(true);
setBounds(400,400,700,400);
setLayout(new BorderLayout());
setTitle("字體對話框");
f = new Font(font_name,1,font_size);
name = new JComboBox();
size = new JComboBox();
name.setMaximumRowCount(5);
size.setMaximumRowCount(5);
createitem();
north = new JPanel();
south = new JPanel();
center = new JPanel();
confirm = new JButton("Yes");
cancel = new JButton("Cancel");
label = new Label("hello,Java程序");
label.setFont(f);
north.setLayout(new FlowLayout());
center.setLayout(new BorderLayout());
south.setLayout(new FlowLayout());
north.add(name);
north.add(size);
center.add(label,BorderLayout.CENTER);
south.add(confirm);
south.add(cancel);
confirm.addActionListener(this);
cancel.addActionListener(this);
name.addItemListener(this);/*new ItemListener(){
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
font_name = name.getSelectedItem().toString();
font = new Font(font_name,1,font_size);
label.setFont(font);
label.repaint();
validate();
}
}
});*/
size.addItemListener(this);/*new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(e.getStateChange() == ItemEvent.SELECTED){
font_size = Integer.parseInt(size.getSelectedItem().toString());
font = new Font(font_name,1,font_size);
label.setFont(font);
label.repaint();
validate();
}
}
});*/
name.addActionListener(this);
size.addActionListener(this);
add(north,BorderLayout.NORTH);
add(south,BorderLayout.SOUTH);
add(center,BorderLayout.CENTER);
}
void createitem(){
allnames = new FontFamilyNames().getFontName();
for(int i=0;i<allnames.length;i++){
name.addItem(allnames[i]);
}
sizes = new int[80];
for(int i=0;i<sizes.length;i++){
sizes[i]=i+1;
}
for(int i:sizes){
size.addItem(String.valueOf(i));
}
}
public String get_fontname(){
return font_name;
}
public int get_fontsize(){
return font_size;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == cancel){
this.dispose();
}
if(e.getSource()== confirm){
tag = 1;
this.setVisible(false);
}
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==name){
font_name = (String)name.getSelectedItem();
font = new Font(font_name,1,font_size);
label.setFont(font);
label.repaint();
}
if(e.getSource()==size){
font_size = Integer.parseInt(size.getSelectedItem().toString());
font = new Font(font_name,1,font_size);
label.setFont(font);
label.repaint();
}
}
}
package test;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class window1 extends JFrame implements ActionListener {
JTextArea text;
JButton button;
String fontname;
int fontsize;
Font font;
window1(){
text = new JTextArea();
button.addActionListener(this);
init();
setVisible(true);
setBounds(200,200,800,400);
validate();
}
void init(){
Font font = text.getFont();
text.setText("Java 2實用教程(第四版)");
text.setFont(new Font(font.getName(), font.getStyle(), 60));
button = new JButton("設置字體");
setLayout(new BorderLayout());
add(button,BorderLayout.NORTH);
add(text,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()== button){
Fontdialog fontdialog = new Fontdialog();
fontdialog.setVisible(true);
if(fontdialog.tag == 1){
fontsize = fontdialog.get_fontsize();
fontname = fontdialog.get_fontname();
font = new Font(fontname,1,fontsize);
text.setFont(font);
text.repaint();
validate();
}
}
}
}
package test;
import java.awt.GraphicsEnvironment;
public class FontFamilyNames {
String allFontNames[];
public String [] getFontName() {
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
allFontNames=ge.getAvailableFontFamilyNames();
return allFontNames;
}
}
你的程序拋出異常了,在window的構造函數中,button沒有new操作,就添加事件監聽了。
修正,把所有成員變量的初始化放在init函數中完成,修正代碼如下:
public window1() {
init();
setVisible(true);
setBounds(200, 200, 800, 400);
validate();
}
void init() {
text = new JTextArea();
Font font = text.getFont();
text.setText("Java 2實用教程(第四版)");
text.setFont(new Font(font.getName(), font.getStyle(), 60));
button = new JButton("設置字體");
button.addActionListener(this);
setLayout(new BorderLayout());
add(button, BorderLayout.NORTH);
add(text, BorderLayout.CENTER);
System.out.println("init ok.");
}
測試了下,你的代碼基本上完成了上述功能了。從java編碼規范上來說,建議類名稱定義為首字母大寫,而且類名稱要有實際意義。
即你的window1的類名不合規范,可以修正為有意義的名稱,例如:MainWindow。