Skip to content

Instantly share code, notes, and snippets.

@memish
Created February 13, 2019 13:54
Show Gist options
  • Save memish/ac9aa5aa8ddb60f7bc7ed3e70f0fec7f to your computer and use it in GitHub Desktop.
Save memish/ac9aa5aa8ddb60f7bc7ed3e70f0fec7f to your computer and use it in GitHub Desktop.
//Usually you will require both swing and awt packages
// even if you are working with just swings.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class gui {
public static void main(String args[]) {
//Creating the Frame
JFrame frame = new JFrame("Chat Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
//Creating the MenuBar and adding components
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("FILE");
JMenu m2 = new JMenu("Help");
mb.add(m1);
mb.add(m2);
JMenuItem m11 = new JMenuItem("Open");
JMenuItem m22 = new JMenuItem("Save as");
m1.add(m11);
m1.add(m22);
//Creating the panel at bottom and adding components
JPanel panel = new JPanel(); // the panel is not visible in output
JLabel label = new JLabel("Enter Text");
JTextField tf = new JTextField(10); // accepts upto 10 characters
JButton send = new JButton("Send");
JButton reset = new JButton("Reset");
panel.add(label); // Components Added using Flow Layout
panel.add(label); // Components Added using Flow Layout
panel.add(tf);
panel.add(send);
panel.add(reset);
// Text Area at the Center
JTextArea ta = new JTextArea("Hello, World");
//JTextPane ta = new JTextPane();
//Adding Components to the frame.
frame.getContentPane().add(BorderLayout.SOUTH, panel);
frame.getContentPane().add(BorderLayout.NORTH, mb);
frame.getContentPane().add(BorderLayout.CENTER, ta);
frame.setVisible(true);
// add the listener to the jbutton to handle the "pressed" event
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String appendText = "jumps over the lazy dog.";
String mySt = tf.getText();
ta.setText(mySt);
// ta.append(appendText);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment