Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.BorderFactory;
- import javax.swing.Box;
- import javax.swing.BoxLayout;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.ScrollPaneConstants;
- import javax.swing.SwingUtilities;
- import javax.swing.border.EtchedBorder;
- /**
- * a simple class that uses box layouts to make a resizable JFrame with some components
- * @author MarkW
- *
- */
- public class GuiTest extends JFrame {
- public static void main(String[] args) {
- //using an annoynmous class here of type runnable
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- GuiTest gt = new GuiTest();
- gt.setVisible(true);
- }
- });
- }
- //our graphical components
- private JPanel mainPanel;
- private JTextArea textArea;
- private JTextField textField;
- private JButton okButton;
- private JButton clearButton;
- private JButton exitButton;
- //our button listener
- private ButtonListener buttonListener;
- //some dimensions
- private final Dimension TEXTAREASIZE = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
- private final Dimension TEXTFIELDSIZE = new Dimension(100, 20);
- private final Dimension BUTTONSIZE = new Dimension(75,35);
- /**
- * GuiTest constructor
- */
- public GuiTest() {
- //set some basic stuff
- this.setBounds(100, 100, 600, 450);
- this.setMinimumSize(new Dimension(550, 400));
- //close the app when the form is closed
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.setTitle("Guit Examples");
- //this is where we will set up our components
- init();
- }
- public void init() {
- //build our main panel which will be our content pane
- mainPanel = new JPanel();
- //this is a box layout with a Y axis... adds components top down
- mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
- //this looks complicated,
- //but its just making an empty outer border thats 10 pixels on the top bottom left and right
- //and making a lowered etched border with a break in it for text as the inner border
- mainPanel.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createEmptyBorder(10, 10, 10, 10),
- BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Box Layouts!")));
- //I like to use a quick and dirty local JPanel to help arrange stuff
- JPanel tPanel;
- //add our text area first
- textArea = new JTextArea();
- //give our text area a scroll bar
- JScrollPane scrollPane = new JScrollPane(textArea);
- scrollPane.setMaximumSize(TEXTAREASIZE);
- scrollPane.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createEmptyBorder(5, 5, 5, 5),
- BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Output Window")));
- scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
- mainPanel.add(scrollPane);
- //this is an invisible component that grows to fill space vertically
- //its below the textArea scroll pane
- mainPanel.add(Box.createVerticalGlue());
- //do the text field stuff
- textField = new JTextField();
- textField.setMinimumSize(TEXTFIELDSIZE);
- textField.setMaximumSize(TEXTFIELDSIZE);
- textField.setPreferredSize(TEXTFIELDSIZE);
- //time to use our temp panel
- tPanel = new JPanel();
- //x axis this time for left to right
- tPanel.setLayout(new BoxLayout(tPanel, BoxLayout.X_AXIS));
- //add a fixed width invisible component on the left
- tPanel.add(Box.createHorizontalStrut(15));
- //create a label for the text field
- tPanel.add(new JLabel("Enter Text:"));
- //add a small spacer
- tPanel.add(Box.createHorizontalStrut(3));
- //add the text field
- tPanel.add(textField);
- //make our button listener now so we have it
- buttonListener = new ButtonListener();
- //lets push the next items to the far right side of the form
- tPanel.add(Box.createHorizontalGlue());
- //add the buttons
- okButton = new JButton("Ok");
- okButton.addActionListener(buttonListener);
- okButton.setMinimumSize(BUTTONSIZE);
- okButton.setMaximumSize(BUTTONSIZE);
- okButton.setPreferredSize(BUTTONSIZE);
- tPanel.add(okButton);
- //a small spacer
- tPanel.add(Box.createHorizontalStrut(5));
- clearButton = new JButton("Clear");
- clearButton.addActionListener(buttonListener);
- clearButton.setMinimumSize(BUTTONSIZE);
- clearButton.setMaximumSize(BUTTONSIZE);
- clearButton.setPreferredSize(BUTTONSIZE);
- tPanel.add(clearButton);
- //a small spacer
- tPanel.add(Box.createHorizontalStrut(5));
- exitButton = new JButton("Exit");
- exitButton.addActionListener(buttonListener);
- exitButton.setMinimumSize(BUTTONSIZE);
- exitButton.setMaximumSize(BUTTONSIZE);
- exitButton.setPreferredSize(BUTTONSIZE);
- tPanel.add(exitButton);
- //lets put some space between the buttons and the right edge
- tPanel.add(Box.createHorizontalStrut(15));
- //add the temp panel to the main panel below the text area
- mainPanel.add(tPanel);
- //this is where the main panel gets
- //added to the JFrames content pane
- this.getContentPane().add(mainPanel);
- }
- /**
- * this class will listen for click events on the buttons
- * and do what should be done from there
- *
- * @author MarkW
- *
- */
- public class ButtonListener implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == okButton) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- textArea.append(textField.getText() + "\r\n");
- textArea.setCaretPosition(textArea.getText().length());
- textField.setText("");
- }
- });
- }
- else if (e.getSource() == clearButton) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- textArea.setText("");
- }
- });
- }
- else if (e.getSource() == exitButton) {
- setVisible(false);
- dispose();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement