First attempt at a simple GUI

I'm trying to teach myself how to create a GUI using Java swing and Window Builder Pro, after watching several YouTube videos and reading some of the tutorials that I completed, follow these steps.

import javax.swing.JFrame; import javax.swing.JTabbedPane; import javax.swing.JPanel; import javax.swing.SpringLayout; import javax.swing.JTextPane; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class JetstreamFrame extends JFrame { private static final long serialVersionUID = 1L; JTabbedPane tabPane; JPanel buttonOnePanel; JPanel buttonTwoPanel; JPanel textDisplayPanel; JTextPane textPane; SpringLayout sl_textDisplayPanel; SpringLayout springLayout; SpringLayout sl_buttonTwoPanel; SpringLayout sl_buttonOnePanel; JButton ButtonTwo; JButton ButtonOne; public JetstreamFrame() { springLayout = new SpringLayout(); getContentPane().setLayout(springLayout); tabPane = new JTabbedPane(JTabbedPane.TOP); setupTabPane(); buttonOnePanel = new JPanel(); sl_buttonOnePanel = new SpringLayout(); setupButtonOnePanel(); ButtonOne = new JButton("Click Here!"); setupButtonOne(); buttonTwoPanel = new JPanel(); sl_buttonTwoPanel = new SpringLayout(); setupButtonTwoPanel(); ButtonTwo = new JButton("Click Here!"); setupButtonTwo(); textDisplayPanel = new JPanel(); sl_textDisplayPanel = new SpringLayout(); setupTextDisplayPanel(); textPane = new JTextPane(); setupTextPane(); } private void setupTabPane() { springLayout.putConstraint(SpringLayout.NORTH, tabPane, 0, SpringLayout.NORTH, getContentPane()); springLayout.putConstraint(SpringLayout.WEST, tabPane, 0, SpringLayout.WEST, getContentPane()); springLayout.putConstraint(SpringLayout.SOUTH, tabPane, -198, SpringLayout.SOUTH, getContentPane()); springLayout.putConstraint(SpringLayout.EAST, tabPane, 484, SpringLayout.WEST, getContentPane()); getContentPane().add(tabPane); } private void setupButtonOnePanel() { tabPane.addTab("Tab One", null, buttonOnePanel, null); buttonOnePanel.setLayout(sl_buttonOnePanel); } private void setupButtonOne() { ButtonOne.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); sl_buttonOnePanel.putConstraint(SpringLayout.NORTH, ButtonOne, 99, SpringLayout.NORTH, buttonOnePanel); sl_buttonOnePanel.putConstraint(SpringLayout.WEST, ButtonOne, 187, SpringLayout.WEST, buttonOnePanel); buttonOnePanel.add(ButtonOne); } private void setupButtonTwoPanel() { tabPane.addTab("Tab Two", null, buttonTwoPanel, null); buttonTwoPanel.setLayout(sl_buttonTwoPanel); } private void setupButtonTwo() { ButtonTwo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { } }); sl_buttonTwoPanel.putConstraint(SpringLayout.NORTH, ButtonTwo, 99, SpringLayout.NORTH, buttonTwoPanel); sl_buttonTwoPanel.putConstraint(SpringLayout.WEST, ButtonTwo, 187, SpringLayout.WEST, buttonTwoPanel); buttonTwoPanel.add(ButtonTwo); } private void setupTextDisplayPanel() { springLayout.putConstraint(SpringLayout.NORTH, textDisplayPanel, 6, SpringLayout.SOUTH, tabPane); springLayout.putConstraint(SpringLayout.WEST, textDisplayPanel, 0, SpringLayout.WEST, getContentPane()); springLayout.putConstraint(SpringLayout.SOUTH, textDisplayPanel, -10, SpringLayout.SOUTH, getContentPane()); springLayout.putConstraint(SpringLayout.EAST, textDisplayPanel, 0, SpringLayout.EAST, getContentPane()); getContentPane().add(textDisplayPanel); textDisplayPanel.setLayout(sl_textDisplayPanel); } private void setupTextPane() { sl_textDisplayPanel.putConstraint(SpringLayout.NORTH, textPane, 0, SpringLayout.NORTH, textDisplayPanel); sl_textDisplayPanel.putConstraint(SpringLayout.WEST, textPane, 0, SpringLayout.WEST, textDisplayPanel); sl_textDisplayPanel.putConstraint(SpringLayout.SOUTH, textPane, 182, SpringLayout.NORTH, textDisplayPanel); sl_textDisplayPanel.putConstraint(SpringLayout.EAST, textPane, 0, SpringLayout.EAST, textDisplayPanel); textDisplayPanel.add(textPane); } public void start() { this.setSize(500, 500); this.setVisible(true); } } 

The premise of this code is to create a window with several tabs and a text area at the bottom, which is visible regardless of which tab the user is on. I created a text area, tabs, buttons, as well as button event listeners for this GUI.

Unfortunately, not one of the tutorials that I read showed me how to type anything in the text area. I would like to do something in accordance with

 System.out.println(); 

attached to each of the buttons, and printing this text appears in the text area, but I don’t know how to do it.

I would also like to know if there was an area of ​​text that I created using the scroll bar along the side if the printed text goes beyond the visible frame.

+4
source share
2 answers
 textpane.append("TEXTHERE"); 

Any text will be added to the event based text area. When we append() text, we can also add new text to the existing passage. If you want to clear JTextArea , I recommend that you call the setText() method.

A JScrollPane needed to add a scrollbar to a JTextArea , which can be done as follows:

 import javax.swing.JScrollPane; JScrollPane scrollPane = new JScrollPane(TEXTAREAHERE); 

Personally, I like to set the scroll bar for this property so that the JScrollBar can scroll up and down and delete the scroll left and right:

scrollPane.setHorizonalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

+4
source

If you understand correctly, you can simply use the append() method.

Learn more about JTextArea

To test this, add a call to it in one of your events with buttons (they don't seem to be doing anything right now). So you can do jTextArea.append("Did it print?"); and click this button to see how it works.

Edit It looks like you are using JTextPane, in this case, maybe use setText() . More here

+2
source

Source: https://habr.com/ru/post/1489142/


All Articles