Java Image Interface Development Simple example
JTextArea, JScrollPane, JPanel, JButton application examples, through the ' Insert Text ' button function, write Test text, line-wrapping function can be used for line and no line, the code is as follows: Import java.awt.BorderLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTextArea;
/**
* JTextArea, JScrollPane, JPanel, JButton application examples
* @author Zuojie JDK 5.0
*/
public class Example8frame extends JFrame {
Private static final long serialversionuid = 1L;
Private JTextArea TextArea; Text fields
Private JScrollPane ScrollPane; with scroll bar panel
Private JPanel Buttonpanel; Store button panel
Private JButton Wrapbutton; Line Wrap button
Public Example8frame () {
Settitle ("text editor"); Set the title of a form
SetSize (300, 300); Set the size of a form
Creating JTextArea Components
TextArea = new JTextArea ();
Create the JScrollPane panel and put the JTextArea component in the scroll bar panel
ScrollPane = new JScrollPane (TextArea);
Add a scroll bar panel to a form
Add (ScrollPane, borderlayout.center);
Buttonpanel = new JPanel (); Create a button panel
Create the Insert Text button and add a click event to add the test text
JButton Insertbutton = new JButton ("Insert text"); Create the Insert Text button
Buttonpanel.add (Insertbutton); Add this button to the panel
Add event Monitoring for this button
Insertbutton.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent event) {
Textarea.append ("This is a simple application instance of a TextArea component, this is the test text.") " ); JTextArea Component Add text information
}
});
Add a "line break" button and add a click event to control line break and no wrap
Wrapbutton = new JButton ("newline"); Create a NewLine text button
Buttonpanel.add (Wrapbutton); Add this button to the panel
Add event Monitoring for this button
Wrapbutton.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent event) {
Boolean wrap =! Textarea.getlinewrap (); Gets whether the JTextArea component has a newline state, default to False, and reverse
Textarea.setlinewrap (wrap); Reset whether line wrapping properties
Wrapbutton.settext (Wrap?) "Do not Wrap": "Line Change"); Display text based on the property settings button
}
});
Add a button panel to a form
Add (Buttonpanel, Borderlayout.south);
}
public static void Main (string[] args) {
Example8frame frame = new Example8frame ();
Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Frame.setvisible (TRUE);
}
}