Home > Back-end >  Java Swing about JTextArea components display Settings
Java Swing about JTextArea components display Settings

Time:10-11

Java tic-tac-toe game, how to draw a fork or circle in JTextArea components, how to set up to monitor events to get the current user click of the mouse which JTextArea

 
Package the swing;

Import the Java. The awt. BorderLayout.
Import the Java. The awt. Color;
Import the Java. The awt. Container;
Import the Java. The awt. The Font;
Import the Java. The awt. GridLayout;
Import the Java. The awt. A Rectangle;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;

The import javax.mail. Swing. BorderFactory;
The import javax.mail. Swing. JButton;
The import javax.mail. Swing. JFrame;
The import javax.mail. Swing. JLabel;
The import javax.mail. Swing. JOptionPane;
The import javax.mail. Swing. JPanel;
The import javax.mail. Swing. JTextArea;
The import javax.mail. Swing. Border. The border;
The import javax.mail. Swing. Border. LineBorder;

Public class MyFrame extends JFrame {

//create all kinds of control objects
JLabel labelOne=new JLabel (" & lt; Html> Please select a mode & lt;/html>" );
JLabel labelTwo=new JLabel (" ");
JButton buttonOne=new JButton (" everyone against ");
JButton buttonTwo=new JButton (" man-machine against ");
JButton buttonThree=new JButton (" exit game!" );
JTextArea area1=new JTextArea (12, 14);
JTextArea area2=new JTextArea (12, 14);
JTextArea area3=new JTextArea (12, 14);
JTextArea area4=new JTextArea (12, 14);
JTextArea area5=new JTextArea (12, 14);
JTextArea area6=new JTextArea (12, 14);
JTextArea area7=new JTextArea (12, 14);
JTextArea area8=new JTextArea (12, 14);
JTextArea area9=new JTextArea (12, 14);
Border Border=BorderFactory. CreateLineBorder (Color BLACK);

Public MyFrame (String title) {

//set the window title
Super (title);

//create the content pane object (ContentPane)
The Container contentPane=this. GetContentPane ();

ContentPane. SetLayout (new BorderLayout ());

//create a JPanel container object

//panalOne is used to display the above layout
JPanel panalOne=new JPanel ();

//panalTwo is used to display the layout of the board
JPanel panalTwo=new JPanel ();

//cancel the default layout manager
//contentPane. SetLayout (null);

////set the content pane for: flow layout FlowLayout
//LayoutManager layout=new FlowLayout (FlowLayout. CENTER);
//contentPane. SetLayout (layout);

//set panalTwo, there are 3 * 3 board, and implement frame 0
PanalTwo. SetLayout (new GridLayout (3, 3, 0, 0));
PanalTwo. SetBorder (new LineBorder (Color black));
//add JPanal container control into the content pane
ContentPane. Add (panalOne, BorderLayout. NORTH);
ContentPane. Add (panalTwo, BorderLayout. CENTER);

//set the border around the text field
Area1. SetBorder (border);
Area1. SetEditable (false);
Area2. SetBorder (border);
Area2. SetEditable (false);
Area3. SetBorder (border);
Area3. SetEditable (false);
Area4. SetBorder (border);
Area4. SetEditable (false);
Area5. SetBorder (border);
Area5. SetEditable (false);
Area6. SetBorder (border);
Area6. SetEditable (false);
Area7. SetBorder (border);
Area7. SetEditable (false);
Area8. SetBorder (border);
Area8. SetEditable (false);
Area9. SetBorder (border);
Area9. SetEditable (false);

//components is added to a container control
PanalOne. Add (labelOne);
PanalOne. Add (labelTwo);
PanalOne. Add (buttonOne);
PanalOne. Add (buttonTwo);
PanalOne. Add (buttonThree);

PanalTwo. Add (area1);
PanalTwo. Add (area2);
PanalTwo. Add (area3);
PanalTwo. Add (area4);
PanalTwo. Add (area5);
PanalTwo. Add (area6);
PanalTwo. Add (area7);
PanalTwo. Add (area8);
PanalTwo. Add (area9);

//the following four parts to set the format of the component
LabelOne. SetFont (new Font (" tahoma ", the Font. PLAIN, 25));
LabelOne. SetForeground (new Color (47174250));
LabelOne. SetBounds (new Rectangle (50,50,200,50));


ButtonOne. SetFont (new Font (" tahoma ", the Font. PLAIN, 16));
ButtonOne. SetForeground (new Color (47174250));
ButtonOne. SetBounds (new Rectangle (280,50,150,50));

ButtonTwo. SetFont (new Font (" tahoma ", the Font. PLAIN, 16));
ButtonTwo. SetForeground (new Color (47174250));
ButtonTwo. SetBounds (new Rectangle (430,50,150,50));

ButtonThree. SetFont (new Font (" tahoma ", the Font. PLAIN, 16));
ButtonThree. SetForeground (new Color (47174250));
ButtonThree. SetBounds (new Rectangle (580,50,150,50));

//create a listener object, add the listener object to the buttons 1,
ButtonOne. AddActionListener (new ActionListener () {
Public void actionPerformed (an ActionEvent e) {

//click on the button, after 1 button 1 and 2 are not optional condition, the content of the object and refresh JLable labelOne
ChangeFrameOne ();
MyFrame. DoublePerson. Play ();

}
});

//create a listener object, to add the listener object button 2,
ButtonTwo. AddActionListener (new ActionListener () {
Public void actionPerformed (an ActionEvent e) {
//click on the button, after 2 button 1 and 2 are not optional, and refresh the content of the object JLable labelTwo
ChangeFrameTwo ();
}
});

//create a listener object, add the listener object to the button 3,
ButtonThree. AddActionListener (new ActionListener () {
Public void actionPerformed (an ActionEvent e) {
//close the window, shows a paragraph and directly to close the window
JOptionPane. ShowMessageDialog (buttonThree, "game has quit, welcome to you next time!" );
System.exit(0);
}
});

}

Public void changeFrameOne () {
ButtonOne. SetEnabled (false);
ButtonTwo. SetEnabled (false);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related