java中的Jlabel怎么设置显示字体大小

pengheng521 2010-11-16 02:22:07
如题
...全文
4747 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
huntor 2010-11-16
  • 打赏
  • 举报
回复


Font font = ...;
jlabel.setFont(font); // 更改某个JLabel字体

UIManager.put("Label.font",font); // 改变所以 JLabel 的字体
sunyiz 2010-11-16
  • 打赏
  • 举报
回复
或者使用html语法
lab.setText("<html><font face=\"宋体\";style=font:15pt>" + "text" + "</font>");
sunyiz 2010-11-16
  • 打赏
  • 举报
回复 4
比如这样
lab.setFont(new Font("宋体", Font.PLAIN, 16));
sunyiz 2010-11-16
  • 打赏
  • 举报
回复
setFont(new Font("字体", FONT.PLANT, xx));

xx就是字体大小
package com.sxt.studentsystem; import java.awt.Dimension; import java.awt.Font; import java.awt.Toolkit; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.text.SimpleDateFormat; import java.util.Date; import javax.swing.JPanel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class LoginFrame extends JFrame { private LoginActionListener myActionListener; private JTextField txtName; private JPasswordField txtPassword; public JTextField getTxtName() { return txtName; } public void setTxtName(JTextField txtName) { this.txtName = txtName; } public JPasswordField getTxtPassword() { return txtPassword; } public void setTxtPassword(JPasswordField txtPassword) { this.txtPassword = txtPassword; } public LoginFrame() { myActionListener = new LoginActionListener(this);// 创建侦听器 this.setLayout(null);// 默认的布局样式 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置位置,居 Dimension SreenSize = Toolkit.getDefaultToolkit().getScreenSize(); int x = (SreenSize.width - 400) / 2; int y = (SreenSize.height - 300) / 2; this.setBounds(x, y, 400, 300);// 设置大小,位置 // 设置主标题 JLabel labMain = new JLabel("欢迎进入学生管理系统"); labMain.setBounds(120, 20, 200, 25); labMain.setFont(new Font("宋体", Font.BOLD, 15));// 字体大小格式 this.getContentPane().add(labMain); // 设置背景图片 JLabel labBackgImage = new JLabel(new ImageIcon("images/back.jpg")); labBackgImage.setBounds(0, 0, 400, 300); this.getLayeredPane() .add(labBackgImage, new Integer(Integer.MIN_VALUE));// 设置深度 ((JPanel) getContentPane()).setOpaque(false); // JLabel labName = new JLabel("姓名:"); labName.setBounds(120, 60, 50, 25); this.getContentPane().add(labName); JLabel labPassword = new JLabel("密码:"); labPassword.setBounds(120, 100, 50, 25); this.getContentPane().add(labPassword); // 输入用户名 txtName = new JTextField("皮锋"); txtName.setBounds(160, 60, 100, 25); this.getContentPane().add(txtName); // 输入密码 txtPassword = new JPasswordField("123456"); txtPassword.setBounds(160, 100, 100, 25); this.getContentPane().add(txtPassword); // 按钮 JButton btnLogin = new JButton("登录"); btnLogin.setBounds(120, 160, 60, 25); // 添加一个标签 btnLogin.setActionCommand("login"); this.add(btnLogin); // 处理事件 btnLogin.addActionListener(this.myActionListener); JButton btnCantel = new JButton("取消"); btnCantel.setBounds(220, 160, 60, 25); // 添加一个标签 btnCantel.setActionCommand("exit"); this.add(btnCantel); btnCantel.addActionListener(this.myActionListener); // 窗体事件 this.addWindowListener(new WindowAdapter() { @Override public void windowOpened(WindowEvent e) { Date date = new Date(); SimpleDateFormat d = new SimpleDateFormat("yyy年MM月dd日hh:mm:ss"); String sDate = d.format(date); LoginFrame.this.setTitle(sDate); } }); // 鼠标事件 this.addMouseListener(new MouseAdapter() { @Override public void mouseEntered(MouseEvent e) { int x = e.getX(); int y = e.getY(); String s = new String(x + "," + y); // txtName.setText(s); } }); this.setVisible(true); } }
整理了Java GUI编程基础知识! public class AllFrame { /** * 这里的代码涉及到 GUI 编程基本面板、基本组件、事件(ActionListener、ChangeListener、MouseListener) */ public AllFrame() { // new 一个主窗体 也可以这样写 new MainFrame(); MainFrame mf = new MainFrame(); } public static void main(String[] args) { new AllFrame(); } // 主窗体 继承 窗体基本格式 类 Jframe 以下弹出窗口均继承 Jframe class MainFrame extends Jframe implements ActionListener { JMenuBar menubar = new JMenuBar();;// 菜单条 JMenu jmenu[] = new JMenu[5];// panelMenu,usedMenu1,usedMenu2,layoutMenu,helpMenu; // 菜单项 JButton closebtn;// 关闭窗体 // 具体的选项 JMenuItem panelitem[] = new JMenuItem[4]; JMenuItem useditem1[] = new JMenuItem[6]; JMenuItem useditem2[] = new JMenuItem[6]; JMenuItem layoutitem[] = new JMenuItem[3]; JMenuItem helpitem = new JMenuItem("关于"); // 菜单名称 String menu[] = { "面板使用", "常用组件一", "常用组件二", "布局设置", "帮助" }; String pane[] = { "普通面板", "滚动面板", "分割面板", "选项面板" }; String uesd1[] = { "标签", "按钮", "文本框", "文本域", "单选按钮" ,"微调"}; String uesd2[] = { "滑块","复选框", "下拉菜单", "列表", "表格", "树状" }; String layout[] = { "流式布局", "边界布局", "网格布局" }; // Jpanelitem,Jscrollpaneitem,Splitpaneitem,JTabbedpanelitem, // Jlabelitem,Jbuttonitem,JTextfielditem,JTextareaitem,JRadiobuttonitem, // JCheckboxitem,JCombomoxitem,Jlistitem,Jtableitem,Jtreeitem, // Flowlayoutitem,Borderlayoutitem,Girdlayoutitem, // Helpitem}; public MainFrame() { this.setTitle("JAVA图型界面学习(GUI)"); // 加载菜单条到窗体 this.setJMenuBar(menubar); // 重新设置大小 this.setSize(380, 290); // 简单的居方法 在窗体基本格式类 Jframe 有详细的居方法 this.setLocationRelativeTo(null); // 设置关闭窗口 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 调用方法 menu(); // 设置容器 Container con = this.getContentPane(); // 定义一个 标签 “JAVA图型界面学习” JLabel jl = new JLabel("JAVA图型界面学习"); // 组件 设置字体大小, 其他组件用法相同 jl.setFont(new Font("隶书", Font.BOLD, 24)); // 具体定位 组件名.setBounds(x, y, width, height) jl.setBounds(30, 30, 280, 50); // 把标签加到 con 容器 con.add(jl); /

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧