JAVA graphical interface Swing Create button, text field

Source: Internet
Author: User
Tags gettext

Package Org.rui.swin;import Java.awt.flowlayout;import Javax.swing.jbutton;import javax.swing.jframe;import Org.rui.utils.swingconsole;public class Button1  extends Jframe{private JButton button1=new JButton ("Button1"), Button2=new JButton ("Button2");p ublic Button1 () {setlayout (New FlowLayout ()); Add (button1); add (button2);} public static void Main (string[] args) {Swingconsole.run (New Button1 (), 200,300);}}


Package Org.rui.swin;import Java.awt.flowlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JTextField; Import org.rui.utils.swingconsole;/** * Capture event * @author PC * */public class Button2 extends JFrame {private JButton BUT1 = New JButton ("Button1"), But2 = new JButton ("Button2"); JTextField TEXTF = new JTextField (10);//Listener Processing class Buttonlistener implements ActionListener {@Overridepublic void actionperformed (ActionEvent arg0) {//system.out.println ((JButton) Arg0.getsource ()). GetText ()); String name = ((JButton) Arg0.getsource ()). GetText (); Textf.settext (name);}} Private Buttonlistener bl = new Buttonlistener ();p ublic Button2 () {but1.addactionlistener (BL); But2.addactionlistener (BL); setlayout (new FlowLayout ()); Add (but1); add (but2); add (TEXTF);} public static void Main (string[] args) {Swingconsole.run (New Button2 (), 300, 200);}}


Package Org.rui.swin;import Java.awt.flowlayout;import Java.awt.event.actionevent;import Java.awt.event.actionlistener;import Java.util.hashmap;import Java.util.map;import Java.util.Map.Entry;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.jscrollpane;import Javax.swing.JTextArea;import Org.rui.utils.swingconsole;public class TextArea extends JFrame {private JButton but1 = new JButton ("Add Data"), But2 = NE W JButton ("Clear Data");p rivate jtextarea t = new JTextArea (+);p rivate map<string, string> m = new Hashmap<s Tring, string> ();p ublic TextArea () {m.put ("CH", "China"), M.put ("EN", "United States"), M.put ("CC", "Kingdom of Heaven"); But1.addactionlistener ( New ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {for (Entry En:m.entryset ()) {T.append ( En.getkey () + ":" +en.getvalue () + "\ n");}}); But2.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {t.settext ("");}}); SetLayout (New FlowLayout ()); Add (new JScrollPane (t)); Add (buT1); add (but2);} public static void Main (string[] args) {Swingconsole.run (New TextArea (), 500, 600);}}


Related Article

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.