How to change the font in Java a simple instance and the operation effect __java

Source: Internet
Author: User

Package Java; Import Java.awt.FlowLayout; Import Java.awt.Font; Import java.awt.event.ActionEvent; Import Java.awt.event.ActionListener; Import java.awt.event.ItemEvent; Import Java.awt.event.ItemListener; Import javax.swing.*; public class Fontnamedome extends JFrame implements ActionListener {/** * */private static final long Serialversionuid = 1L; JLabel jl1; JTextArea JTA; JButton jb1,jb2; JPanel JP; JScrollPane JS; String name= "Song Body"; Public Fontnamedome () {jl1=new JLabel ("message board"), Jta=new JTextArea (13,45), Jb1=new JButton ("Change Fonts"), Jb2=new JButton ("submit"); Jp=new JPanel (); Js=new JScrollPane (JTA); Jp.add (JL1); Jp.add (JS); Jp.add (JB1); Jp.add (JB2); Jta.settext ("One World, One dream.") "); Jta.setfont (New Font ("Song Body", font.plain,12)); This.add (JP); This.setsize (330, 300); This.setvisible (TRUE); Jb1.addactionlistener (this); Jb2.addactionlistener (this); public void actionperformed (ActionEvent e) {if (E.getsource () ==jb1) {final JDialog jd=new (this, prompt, true); JDialog Etlayout (New FlowLayout ()); BUttongroup bg=new Buttongroup (); Jradiobutton jrb1=new Jradiobutton ("XXFarEastFont-Xingkai"); Jradiobutton jrb2=new Jradiobutton ("Chinese fine black"); JButton jb3=new JButton ("OK"); Bg.add (JRB1); Bg.add (JRB2); Jd.add (JRB1); Jd.add (JRB2); Jrb1.additemlistener (New ItemListener () {public void itemstatechanged (ItemEvent e) {name= "Chinese Xingkai";}}); Jrb2.additemlistener (New ItemListener () {public void itemstatechanged (ItemEvent e) {name= "XXFarEastFont-Black";}}); Jd.add (JB3); Jb3.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {jd.dispose (); Font f=new font (name,font.bold,16); Jta.setfont (f); } }); Jd.setsize (200, 200); Jd.setvisible (TRUE); else if (E.getsource () ==jb2) {int a=joptionpane.showconfirmdialog (NULL), confirm that you want to commit it. "," hint ", joptionpane.ok_option, Joptionpane.information_message); if (a==joptionpane.yes_option) {Dispose ();}} public static void Main (string[] args) {fontnamedome fontname=new fontnamedome ();}}

==========================
This is a simple Java program. Its function is to use SetFont to change the font. The effect can be seen after running. In this Java program, the main use of JLabel, JTextArea, JButton, JPanel, JScrollPane, JDialog, Jradiobutton and so on;
and several commonly used registration monitoring: addActionListener, Additemlistener;
Change font: SetFont

==========================
==========================
The operation effect is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.