📄 dialogexample.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextArea;
class FontDialog extends Dialog implements ItemListener,ActionListener {
Choice list;
JTextArea text;
Font font;
Button yes,cancel;
Component com;
FontDialog(Frame f,String name,Component com) {
super(f,name);
this.com=com;
//super(f,name,true);
setModal(true); //将对话框设置为有模式
yes=new Button("Yes");
cancel=new Button("Cancel");
yes.addActionListener(this);
cancel.addActionListener(this);
text=new JTextArea(2,25);
list=new Choice();
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[]=ge.getAvailableFontFamilyNames();
for(int i=0;i<fontName.length;i++)
list.add(fontName[i]);
list.addItemListener(this);
setLayout(new FlowLayout());
add(list);
add(text);
add(yes);
add(cancel);
setBounds(100,100,280,170);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
}
);
validate();
}
public void itemStateChanged(ItemEvent e) {
String name=(String)list.getSelectedItem();
font=new Font(name,Font.PLAIN,14);
text.setFont(font);
text.setText("hello,奥运");
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==yes) {
com.setFont(font);
this.setVisible(true); //将对话框设置为可见
}
else if(e.getSource()==cancel) {
this.setVisible(false); //将对话框设置为不可见
}
}
}
class Dwindow extends Frame implements ActionListener {
JTextArea text;
Button buttonFont;
Dwindow() {
buttonFont=new Button("设置字体");
text=new JTextArea("显示内容");
buttonFont.addActionListener(this);
add(buttonFont,BorderLayout.NORTH);
add(text);
setBounds(60,60,300,300);
setVisible(true);
validate();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==buttonFont) {
FontDialog dialog=new FontDialog(new Dwindow(),"字体对话框",text);
//创建对话框,并将当前窗口、字符串“字体对话框”以及text传递给构造方法的参数
dialog.setVisible(true);
//将对话框dialog设置为可见
}
}
}
public class DialogExample {
public static void main(String[] args) {
new Dwindow();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -