This Java source code is an example of a Graphical User Interface (GUI) with different GUI elements such as TextBox, Buttons, TextArea, Text Area and Labels. It is implemented using Java Swing API which provides a set of “lightweight” (all-Java language) components. This code example extends the functionality of JFrame class to show a JFrame window and Implements ActionListener on buttons to catch the click event on those buttons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | package com.company; import javax.swing.*; import java.awt.*; import java.io.*; import java.awt.event.*; class Main extends JFrame implements ActionListener { JComboBox help = new JComboBox(); JLabel txtlabel = new JLabel("Coded by system error at weebly"); JButton button1 = new JButton("First button"); JButton button2 = new JButton("Second button"); JButton button3 = new JButton("Third button"); //text area JTextArea output = new JTextArea("output results..., " + " you can also write some text in here \n "+ " and look at the 1337 colors", 14, 45); public Main() { super("System error's program "); //create window setSize(600, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); //create conent container Container contentArea = getContentPane(); contentArea.setBackground(Color.gray); //creae layout manager FlowLayout flowManager = new FlowLayout(); contentArea.setLayout(flowManager); JComboBox help = new JComboBox(); help.setForeground(Color.black); help.setBackground(Color.lightGray); setContentPane(contentArea); //combo box help help.addItem("Help"); help.addItem(""); help.addItem("This is where i'll explain"); help.addItem("What the program is and exactly"); help.addItem("how to use it, I haven't got"); help.addItem("a clue what I want to do yet though."); contentArea.add(help); //text label txtlabel.setBackground(Color.darkGray); contentArea.add(txtlabel); setContentPane(contentArea); output.setForeground(Color.green); output.setBackground(Color.black); contentArea.add(output); setContentPane(contentArea); //scroll pane JScrollPane scroller = new JScrollPane(output, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scroller.setForeground(Color.darkGray); contentArea.add(scroller); setContentPane(contentArea); //buttons button1.addActionListener(this); button1.setBackground(Color.lightGray); button1.setForeground(Color.black); contentArea.add(button1); setContentPane(contentArea); button2.addActionListener(this); button2.setBackground(Color.lightGray); button2.setForeground(Color.black); contentArea.add(button2); setContentPane(contentArea); button3.addActionListener(this); button3.setBackground(Color.lightGray); button3.setForeground(Color.black); contentArea.add(button3); setContentPane(contentArea); //text label txtlabel2.setBackground(Color.darkGray); contentArea.add(txtlabel2); setContentPane(contentArea); } public void actionPerformed (ActionEvent event) { if(event.getSource() == button1) output.setText("You pressed button1"); if(event.getSource() == button2) output.setText("You pressed button2"); if(event.getSource() == button3) output.setText("You pressed button3"); } public static void main (String [] args) { Main eg = new Main(); } } |