dbq时隔这么久我总算又来更新了
这次说说一个很好玩的类,JLabel的如何改里面字的属性
label.setFont(new Font("宋体", 0 , 15)); 这个指的是里面字体是宋体,正常(无样式),15是字号。再来看一个
label.setFont(new Font("宋体", Font.ITALIC , 15)); ITALIC这个是指斜体
package experiment; import java.awt.BorderLayout; import java.awt.Container; 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 java.util.Calendar; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class ShenYue extends JFrame implements ActionListener { JButton button1, button2, button3; JLabel label; ShenYue() { super("JButtonDemo"); label = new JLabel("这里是文字", JLabel.CENTER); button1 = new JButton("普通"); button1.setActionCommand("putong"); // 设置动作命令为putong button1.addActionListener(this); // 注册事件监听器 button2 = new JButton("黑体"); button2.setActionCommand("heiti"); // 设置动作命令为heiti button2.addActionListener(this); // 注册事件监听器 button3 = new JButton("xieti"); button3.setActionCommand("quit"); // 设置动作命令为xieti button3.addActionListener(this); // 注册事件监听器 getContentPane().add(label, BorderLayout.NORTH); getContentPane().add(button1, BorderLayout.WEST); getContentPane().add(button2, BorderLayout.CENTER); getContentPane().add(button3, BorderLayout.EAST); } public void actionPerformed(ActionEvent e) { Calendar c = Calendar.getInstance(); // 得到系统日历类的对象 if (e.getActionCommand().equals("putong")) { label.setText("这里是文字"); label.setFont(new Font("宋体", 0 , 15)); label.setHorizontalAlignment(JLabel.CENTER);// 设置标签的文本居中 } else if (e.getActionCommand().equals("heiti")) { label.setText("这里是文字"); label.setFont(new Font("黑体", 0 , 15)); label.setHorizontalAlignment(JLabel.CENTER);// 设置标签的文本居中 } else { label.setText("这里是文字"); label.setHorizontalAlignment(JLabel.CENTER); label.setFont(new Font("宋体", Font.ITALIC , 15)); } } public static void main(String args[]) { JFrame frame = new ShenYue(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }