JAVA 记事本 设置字体 事件处理
classSetFontextendsJDialogimplementsItemListener,ActionListener{Containercon;JPanelj1...
class SetFont extends JDialog implements ItemListener, ActionListener {
Container con;
JPanel j1, j2, j3, j4, j5, j6;
JComboBox bx1, bx2, bx3;
JButton jb1, jb2;
JLabel jl1, jl2, jl3, jl4, jl5;
String fontt1="黑体",str="12",str2;
int i=Font.PLAIN;
public SetFont() {
String[] font1 = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
String[] font2 = { "常规", "斜体", "粗体", "粗斜体" };
String[] font3 = { "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "22", "24", "26", "28", "30",
"36", "42", "50" };
bx1 = new JComboBox(font1);
bx2 = new JComboBox(font2);
bx3 = new JComboBox(font3);
bx1.addActionListener(this);
bx2.addActionListener(this);
bx3.addActionListener(this);
jb1 = new JButton("确定");
jb2 = new JButton("取消");
jb1.addActionListener(this);
jb2.addActionListener(this);
jl1 = new JLabel("字体:");
jl2 = new JLabel("字形:");
jl3 = new JLabel("字号:");
jl4 = new JLabel("AaBbCcDdEeYyZz", JLabel.CENTER);// 标签(设置标签位置剧中)
jl4.setBorder(BorderFactory.createTitledBorder("事例"));// 设置对象边框(边框提示!)
jl4.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
jl4.setFont(new Font(fontt1,i,Integer.parseInt(str)));
j4.setLayout(new GridLayout(1, 1));
j4.add(jl4);
j4.setBorder(BorderFactory.createTitledBorder("示例"));// 设置对象边框(边框提示!)
j4.setBounds(50, 50, 200, 70);
}
public void itemStateChanged(ItemEvent e1) {
if (e1.getSource() == bx1) {
fontt1 = (String) bx1.getSelectedItem();
}
if (e1.getSource() == bx2) {
str2=(String) bx2.getSelectedItem();
if (str2.equals("常规")) {
i = Font.PLAIN;
}
if (str2.equals("斜体")) {
i = Font.ITALIC;
}
if (str2.equals("加粗")) {
i = Font.BOLD;
}
}
if(e1.getSource()==bx3){
str=(String)bx3.getSelectedItem();
}
}
}
由于发的字数有限,中间省去了一些界面设计的代码
我是个新手
请问我设置字体的事件逻辑哪错了? 展开
Container con;
JPanel j1, j2, j3, j4, j5, j6;
JComboBox bx1, bx2, bx3;
JButton jb1, jb2;
JLabel jl1, jl2, jl3, jl4, jl5;
String fontt1="黑体",str="12",str2;
int i=Font.PLAIN;
public SetFont() {
String[] font1 = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
String[] font2 = { "常规", "斜体", "粗体", "粗斜体" };
String[] font3 = { "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "22", "24", "26", "28", "30",
"36", "42", "50" };
bx1 = new JComboBox(font1);
bx2 = new JComboBox(font2);
bx3 = new JComboBox(font3);
bx1.addActionListener(this);
bx2.addActionListener(this);
bx3.addActionListener(this);
jb1 = new JButton("确定");
jb2 = new JButton("取消");
jb1.addActionListener(this);
jb2.addActionListener(this);
jl1 = new JLabel("字体:");
jl2 = new JLabel("字形:");
jl3 = new JLabel("字号:");
jl4 = new JLabel("AaBbCcDdEeYyZz", JLabel.CENTER);// 标签(设置标签位置剧中)
jl4.setBorder(BorderFactory.createTitledBorder("事例"));// 设置对象边框(边框提示!)
jl4.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
jl4.setFont(new Font(fontt1,i,Integer.parseInt(str)));
j4.setLayout(new GridLayout(1, 1));
j4.add(jl4);
j4.setBorder(BorderFactory.createTitledBorder("示例"));// 设置对象边框(边框提示!)
j4.setBounds(50, 50, 200, 70);
}
public void itemStateChanged(ItemEvent e1) {
if (e1.getSource() == bx1) {
fontt1 = (String) bx1.getSelectedItem();
}
if (e1.getSource() == bx2) {
str2=(String) bx2.getSelectedItem();
if (str2.equals("常规")) {
i = Font.PLAIN;
}
if (str2.equals("斜体")) {
i = Font.ITALIC;
}
if (str2.equals("加粗")) {
i = Font.BOLD;
}
}
if(e1.getSource()==bx3){
str=(String)bx3.getSelectedItem();
}
}
}
由于发的字数有限,中间省去了一些界面设计的代码
我是个新手
请问我设置字体的事件逻辑哪错了? 展开
1个回答
展开全部
看看我写的一段关于字体的
注意这里:
******************
mode += Font.BOLD;
******************
不懂hi我吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckBoxTest {
public static void main(String[] args) {
CheckBoxFrame frame = new CheckBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CheckBoxFrame extends JFrame {
private JLabel label;
private JCheckBox bold;
private JCheckBox italic;
private static final int FONTSIZE = 12;
public CheckBoxFrame() {
setTitle("CheckBoxTest");
setSize(300,200);
label = new JLabel("The quick brown fox jumps over the lazy dog.");
label.setFont(new Font("Serif", Font.PLAIN, FONTSIZE));
add(label, BorderLayout.CENTER);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
int mode = 0;
if(bold.isSelected()) mode += Font.BOLD;
if(italic.isSelected()) mode += Font.ITALIC;
label.setFont(new Font("Serif", mode, FONTSIZE));
}
};
JPanel buttonPanel = new JPanel();
bold = new JCheckBox("Bold");
bold.addActionListener(listener);
buttonPanel.add(bold);
italic = new JCheckBox("Italic");
italic.addActionListener(listener);
buttonPanel.add(italic);
add(buttonPanel, BorderLayout.SOUTH);
}
}
注意这里:
******************
mode += Font.BOLD;
******************
不懂hi我吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CheckBoxTest {
public static void main(String[] args) {
CheckBoxFrame frame = new CheckBoxFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class CheckBoxFrame extends JFrame {
private JLabel label;
private JCheckBox bold;
private JCheckBox italic;
private static final int FONTSIZE = 12;
public CheckBoxFrame() {
setTitle("CheckBoxTest");
setSize(300,200);
label = new JLabel("The quick brown fox jumps over the lazy dog.");
label.setFont(new Font("Serif", Font.PLAIN, FONTSIZE));
add(label, BorderLayout.CENTER);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
int mode = 0;
if(bold.isSelected()) mode += Font.BOLD;
if(italic.isSelected()) mode += Font.ITALIC;
label.setFont(new Font("Serif", mode, FONTSIZE));
}
};
JPanel buttonPanel = new JPanel();
bold = new JCheckBox("Bold");
bold.addActionListener(listener);
buttonPanel.add(bold);
italic = new JCheckBox("Italic");
italic.addActionListener(listener);
buttonPanel.add(italic);
add(buttonPanel, BorderLayout.SOUTH);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询