java中gui编程ActionListener和itemListener
2015-06-23 17:15
423 查看
1、根据运行结果,可以定义一个字体设置类实现ActionListener接口和ItemListener接口以进行动作事件处理和选项变化事件处理。使用List构造字形选择列表,使用JComboBox构造字体选择组合框,使用JRadioButton构造字体颜色选择单选按钮,使用JCheckBox构造字体效果选择复选框。使用JLabel构造4个标签分别进行字体、字号、字型、字色的说明,使用JButton构造2个按钮分别进行编辑文本和退出,使用JTextField构造测试文本输入区域。
第一题代码:
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
public
classFronChange extends JFrame
implements ActionListener,ItemListener {
JPanelpnlMain;
JLabellblSize,lblType,lblstyle,lblColor;
JTextFieldtxtTest;
JRadioButton rbtnRed,rbtnGreen;
JCheckBox chkBold,chkItalic;
JButton btnExit,btnEdit;
ButtonGroup grpColor;
List lstSize;
JComboBox cmbType;
String[] strType={"宋体","隶书","楷体_GB2312","仿宋_GB2312"};
publicFronChange() {
super("字体设置器");
lblType=newJLabel("请选择字体");
lblSize=newJLabel("请选择字号");
lblstyle=newJLabel("请选择字形");
lblColor=newJLabel("请选择字色");
lstSize=newList();
lstSize.addItemListener(this);
for (inti=10;i<30;i+=2)
lstSize.add(String.valueOf(i));
lstSize.select(0);
cmbType=newJComboBox(strType);
cmbType.addItemListener(this);
cmbType.setSelectedIndex(0);
grpColor=newButtonGroup();
rbtnRed=newJRadioButton("红色");
grpColor.add(rbtnRed);
rbtnRed.setSelected(true);
rbtnRed.addItemListener(this);
rbtnGreen=newJRadioButton("绿色");
grpColor.add(rbtnGreen);
rbtnGreen.addItemListener(this);
chkBold=newJCheckBox("加粗");
chkBold.addItemListener(this);
chkItalic=newJCheckBox("倾钭");
chkItalic.addItemListener(this);
txtTest=newJTextField("HellowWord");
txtTest.setEditable(false);
btnExit=newJButton("退出");
btnExit.addActionListener(this);
btnEdit=newJButton("编辑");
btnEdit.addActionListener(this);
pnlMain=newJPanel(null);
lblType.setBounds(5,0,80,30);
cmbType.setBounds(5,30,80,30);
lblSize.setBounds(100,0,80,30);
lstSize.setBounds(100,30,80,60);
lblstyle.setBounds(195,0,80,30);
chkBold.setBounds(195,30,60,30);
chkItalic.setBounds(195,50,60,30);
lblColor.setBounds(290,0,80,30);
rbtnRed.setBounds(290,30,60,30);
rbtnGreen.setBounds(290,50,60,30);
txtTest.setBounds(10,100,270,60);
btnExit.setBounds(290,100,80,30);
btnEdit.setBounds(290,135,80,30);
pnlMain.add(lblType);
pnlMain.add(cmbType);
pnlMain.add(lblSize);
pnlMain.add(lstSize);
pnlMain.add(lblstyle);
pnlMain.add(chkBold);
pnlMain.add(chkItalic);
pnlMain.add(lblColor);
pnlMain.add(rbtnRed);
pnlMain.add(rbtnGreen);
pnlMain.add(txtTest);
pnlMain.add(btnExit);
pnlMain.add(btnEdit);
this.setContentPane(pnlMain);
setSize(400,200);
setVisible(true);
}
publicvoidactionPerformed(ActionEvent evt) {
if (evt.getSource()==btnEdit) txtTest.setEditable(
true );
if (evt.getSource()==btnExit)
System.exit(0);
}
publicvoiditemStateChanged(ItemEvent evt) {
if (evt.getSource()==rbtnRed)
txtTest.setForeground(Color.RED);
if (evt.getSource()==rbtnGreen)
txtTest.setForeground(Color.GREEN);
int intBold=(chkBold.isSelected()?Font.BOLD:Font.PLAIN);
int intItalic=(chkItalic.isSelected()?Font.ITALIC:Font.PLAIN);
int intSize=Integer.parseInt((String)lstSize.getSelectedItem());
String strType=(String)cmbType.getSelectedItem();
txtTest.setFont(new Font(strType,intBold+intItalic,intSize));
}
publicstaticvoidmain(String args[]) {
new FronChange();
}
}
第一题代码:
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
public
classFronChange extends JFrame
implements ActionListener,ItemListener {
JPanelpnlMain;
JLabellblSize,lblType,lblstyle,lblColor;
JTextFieldtxtTest;
JRadioButton rbtnRed,rbtnGreen;
JCheckBox chkBold,chkItalic;
JButton btnExit,btnEdit;
ButtonGroup grpColor;
List lstSize;
JComboBox cmbType;
String[] strType={"宋体","隶书","楷体_GB2312","仿宋_GB2312"};
publicFronChange() {
super("字体设置器");
lblType=newJLabel("请选择字体");
lblSize=newJLabel("请选择字号");
lblstyle=newJLabel("请选择字形");
lblColor=newJLabel("请选择字色");
lstSize=newList();
lstSize.addItemListener(this);
for (inti=10;i<30;i+=2)
lstSize.add(String.valueOf(i));
lstSize.select(0);
cmbType=newJComboBox(strType);
cmbType.addItemListener(this);
cmbType.setSelectedIndex(0);
grpColor=newButtonGroup();
rbtnRed=newJRadioButton("红色");
grpColor.add(rbtnRed);
rbtnRed.setSelected(true);
rbtnRed.addItemListener(this);
rbtnGreen=newJRadioButton("绿色");
grpColor.add(rbtnGreen);
rbtnGreen.addItemListener(this);
chkBold=newJCheckBox("加粗");
chkBold.addItemListener(this);
chkItalic=newJCheckBox("倾钭");
chkItalic.addItemListener(this);
txtTest=newJTextField("HellowWord");
txtTest.setEditable(false);
btnExit=newJButton("退出");
btnExit.addActionListener(this);
btnEdit=newJButton("编辑");
btnEdit.addActionListener(this);
pnlMain=newJPanel(null);
lblType.setBounds(5,0,80,30);
cmbType.setBounds(5,30,80,30);
lblSize.setBounds(100,0,80,30);
lstSize.setBounds(100,30,80,60);
lblstyle.setBounds(195,0,80,30);
chkBold.setBounds(195,30,60,30);
chkItalic.setBounds(195,50,60,30);
lblColor.setBounds(290,0,80,30);
rbtnRed.setBounds(290,30,60,30);
rbtnGreen.setBounds(290,50,60,30);
txtTest.setBounds(10,100,270,60);
btnExit.setBounds(290,100,80,30);
btnEdit.setBounds(290,135,80,30);
pnlMain.add(lblType);
pnlMain.add(cmbType);
pnlMain.add(lblSize);
pnlMain.add(lstSize);
pnlMain.add(lblstyle);
pnlMain.add(chkBold);
pnlMain.add(chkItalic);
pnlMain.add(lblColor);
pnlMain.add(rbtnRed);
pnlMain.add(rbtnGreen);
pnlMain.add(txtTest);
pnlMain.add(btnExit);
pnlMain.add(btnEdit);
this.setContentPane(pnlMain);
setSize(400,200);
setVisible(true);
}
publicvoidactionPerformed(ActionEvent evt) {
if (evt.getSource()==btnEdit) txtTest.setEditable(
true );
if (evt.getSource()==btnExit)
System.exit(0);
}
publicvoiditemStateChanged(ItemEvent evt) {
if (evt.getSource()==rbtnRed)
txtTest.setForeground(Color.RED);
if (evt.getSource()==rbtnGreen)
txtTest.setForeground(Color.GREEN);
int intBold=(chkBold.isSelected()?Font.BOLD:Font.PLAIN);
int intItalic=(chkItalic.isSelected()?Font.ITALIC:Font.PLAIN);
int intSize=Integer.parseInt((String)lstSize.getSelectedItem());
String strType=(String)cmbType.getSelectedItem();
txtTest.setFont(new Font(strType,intBold+intItalic,intSize));
}
publicstaticvoidmain(String args[]) {
new FronChange();
}
}
相关文章推荐
- java中的gui实现事件监听
- hbulider mui框架
- Azure Queue队列存储(2)
- QuickFIX/N入门:二、发送消息及接收消息
- IOS-- UIView中的坐标转换
- QuickFIX/N入门:一、如何创建一个QuickFIX/N的应用程序
- QuickFIX/N入门:使用消息循环分组
- QuickFIX/N入门:如何自定义FIX
- mysql sequence
- UIScroll View 和 AutoLayout
- 8_Ueditor编辑器和Echarts的使用
- UE4 AI入门
- mysql:Variable 'character_set_client' can't be set to the value of 'NULL'解决
- Azure Queue队列存储(1)
- 使用SoapUI 对WebService压力测试
- easyui 本地js数据的加载
- iOS-给手势UITapGestureRecognizer添加"tag"标志
- easyui combotree的使用示例
- EasyUi细节
- How to fix Column 'InvariantName' is constrained to be unique 解决办法!