Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process of creating a Java chat window?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge about "what is the creation process of Java chat window". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Swing Components JPanel

The Panel component in JPanel and AWT is used in the same way. It is a borderless panel that cannot be moved, zoomed in, zoomed out, or closed. Its default layout manager is FlowLayout. You can also use JPanel's constructor with parameters JPanel(LayoutManager layout) or its setLayout() method to formulate layout managers for it.

JScrollPane

JScrollPan is a panel container with scroll bars, and this panel can only add one component, if you want to add multiple components in the panel, add the component to JPanel, and then add JPanel to JScrollPan.

JScrollPan ()//Create an empty JScrollPan panel JScrollPan (Component view)//Create a JScrollPan panel that displays specified components, horizontal and vertical scroll bars JScrollPan (Component view,int vsbPolicy,int hsbPolicy)//Create a JScrollPan that displays specified containers and has specified scroll bar policies. The parameters vsbPolicy and hsbPolicy represent vertical and horizontal scrollbar policies, respectively, and are static constants specified as SCrollPaneConstants. void setHorizontalBarPolicy(int policy)//Specifies the horizontal scrollbar policy, i.e. when the horizontal scrollbar is displayed on the scrollpane void setVerticalBarPolicy(int policy)//Specifies the vertical scrollbar policy, i.e. when the vertical scrollbar appears on the scroll panel void setViewportView(Component view)//Sets the components displayed on the scroll panel void setHorizontalBarPolicy(int policy)//Specifies the horizontal scroll bar policy, i.e. when the horizontal scroll bar is displayed on the scroll panel void setVerticalBarPolicy(int policy)//Specifies the vertical scrollbar policy, i.e. when the vertical scrollbar appears on the scroll panel void setViewportView(Component view)//Sets the components displayed in the scroll panel How to add buttons to containers package Swing; import javax.swing.*; import java.awt.*; public class Example02 extends JFrame{ public Example02(){ this.setTitle("PanelDemo"); //Create a scroll panel JScrollPane scrollPane=new JScrollPane(); //Set horizontal scroll bar policy, ---scroll bar always displayed scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); //Set Vertical Scrollbar Policy---Scrollbars are displayed when needed scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); //define a panel JPanel panel=new JPanel(); panel.add(new JButton("Button one")); panel.add(new JButton("Button two")); panel.add(new JButton("Button three")); panel.add(new JButton("Button four")); //Set JPanel panel to display in scroll panel scrollPane.setViewportView(panel); //Add a scroll panel to the center area of the content panel this.add(scrollPane, BorderLayout.CENTER); //Add a button to the SOUTH area of the content panel this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400,250); this.setVisible(true); } public static void main(String[] args) { new Example02(); } }

text component

Text components are used to receive user input information or present information to the user, including text box (JtexField), text field (JtexArea), they have a common parent class JTextComponent. This is an abstract class that provides methods commonly used by text components.

String getText() returns all text content in a text component String getSelectedText () returns selected text content in a text component void selectAll() selects all content in a text component void setEditable() sets the text component to editable or non-editable void setText(String text) sets the content of a text component void replaceSelection(String content) replaces currently selected content with given content Text box (JTextField)

Can only accept single-line text input, text box commonly used construction method

JTextField() Creates an empty text box with an initial string of nullJTextField(int columns) Creates a text box with the specified number of columns with an initial string of nullJTextField(String text) Creates a text box that displays the specified initial string JTextField(String text,intcolumn) Creates a text box with the specified number of columns and displays the specified initial string Text Field (JTextArea)

Can receive multi-line text input, can set the number of rows and columns in the region, commonly used construction methods

JTextArea(String text) creates a text field that displays the specified initial string JTextArea() creates an empty text field JTextArea(int rows,int columns) creates an empty text field with specified rows and columns JTextArea(String text,int rows,int columns) creates a text field that displays the specified initial text and specifies rows and columns Chat Window Example package Swing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LiaoTian extends Frame { JButton sendBt; JTextField inputField; JTextArea chatContent; public LiaoTian(){ this.setLayout(new BorderLayout()); //Create a text field chatContent=new JTextArea(12,34); //Create a scroll panel with text fields as display criteria JScrollPane showPanel=new JScrollPane(chatContent); //Set text field to non-editable chatContent.setEditable(false); //Create a panel JPanel inputPanel=new JPanel(); //Create a text box inputField =new JTextField(20); //Create a Send button sendBt=new JButton("Send"); //Add an event to the button sendBt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Get input information String content=inputField.getText(); //determine whether the input information is empty if(content!= null&&! content.trim().equals("")){ chatContent.append("self: "+content+"\n"); }else{ chatContent.append("Chat message cannot be empty"+"\n"); } inputField.setText(""); } }); //Create a label Label label=new Label("Chat Info"); //Add tags to Jpanel panel inputPanel.add(label); //Add button to Jpanel panel inputPanel.add(inputField); //Add scroll panel and JPanel panel to JFrame window inputPanel.add(sendBt); this.add(showPanel,BorderLayout.CENTER); this.add(inputPanel,BorderLayout.SOUTH); this.setTitle("Chat window"); this.setSize(400,300); this.setVisible(true); } public static void main(String[] args) { new LiaoTian(); } }

operation results

From the above, we can simulate a simple window through JFrame, first divide the window into two areas through BorderLayout layout manager, then put a JScrollpane scroll panel in the center area, add a JTextArea text field in the scroll panel to display chat records, place a JPanel panel in the south area, place three components in the JPanel panel, JLabel label is used for information description, JTextField text box is used to input user chat information, The JButton button is used to send chat messages. The JLabel component used here is a static component used to display a static line of text and icons. Its role is to explain information. It does not accept user input and cannot add events.

"Java chat window creation process is what" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report