Java GUI (homework), javagui
The GUI teacher said that there will be little use in the future, so the notes are not very good, so I will not take them out, so I will share my homework today.
Job 1: Set a tool that can generate random numbers continuously. A button is required to pause or start.
Import java. awt. event .*;
Import javax. swing .*;
// Generate a random number
@ SuppressWarnings ("serial ")
Public class Dome extends JFrame {
Int a = 0, B = 0;
Public Dome (){
This. setLayout (null );
JLabel jlabel3 = new JLabel (); // create a text window
Jlabel3.setBounds (125,100, 50, 30); // fixed position
This. add (jlabel3); // enter it in the dialog box
This. setTitle ("0 ~ 99999 random "); // set the title
This. setBounds (300,300,); // you can specify the dialog box size.
This. setVisible (true); // visible in the dialog box
While (true) {// loop listener button
JButton jbutton = new JButton ();
Jbutton. setText ("start/stop ");
Jbutton. setBounds (100,150,100, 30 );
Jbutton. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
B = B + 1;
}
});
This. add (jbutton );
While (B % 2 = 0) {// a random number is generated cyclically.
A = (int) (Math. random () * 99999 );
Jlabel3.setText ("" + );
}
}
}
Public static void main (String [] args ){
New Dome (); // call the constructor
}
}
After completion: This button can be used to start and pause.
Job 2: Create a logon box
Import javax. swing .*;
// Logon Interface
@ SuppressWarnings ("serial ")
Public class Dome extends JFrame {
Public Dome (){
This. setLayout (null );
// Set the first line of text
JLabel jlabel1 = new JLabel ();
Jlabel1.setText ("Logon interface ");
Jlabel1.setBounds (, 40 );
This. add (jlabel1 );
// Display the account
JLabel jlabel2 = new JLabel ();
Jlabel2.setText ("account :");
Jlabel2.setBounds (60, 60, 50, 20 );
This. add (jlabel2 );
// Display Password
JLabel jlabel3 = new JLabel ();
Jlabel3.setText ("Password :");
Jlabel3.setBounds (60,100, 50, 20 );
This. add (jlabel3 );
// Create a text box for the input account
JTextArea jtext1 = new JTextArea ();
Jtext1.setText ("enter an account ");
Jtext1.setBounds );
This. add (jtext1 );
// Create a password box
JPasswordField jtext2 = new JPasswordField ();
Jtext2.setBounds (110,100,200, 20 );
This. add (jtext2 );
// Set the logon button
JButton jbutton = new JButton ();
Jbutton. setText ("secure logon ");
Jbutton. setBounds (110,160,200, 30 );
This. add (jbutton );
// Set the window title and size
This. setTitle ("Logon interface ");
This. setBounds (0, 0, 445,340 );
This. setVisible (true );
}
Public static void main (String [] args ){
New Dome ();
}
}
After completion: This login box does not currently implement the login function, but it is easy to implement. The Listener button and button are triggered to read the content entered above, then compare the content with the user name and password you set.
If the logon is successful, an incorrect account or password is prompted.
Assignment 3: Create a notebook
Import java. awt. event .*;
Import javax. swing .*;
@ SuppressWarnings ("serial ")
Public class Dome extends JFrame {
Public Dome (){
This. setLayout (null );
JTextArea jtext1 = new JTextArea ();
Jtext1.setBounds (0, 0, 400,400 );
Jtext1.setLineWrap (true );
This. add (jtext1 );
// Main Menu Bar
JMenuBar menuBar = new JMenuBar ();
JMenu menu1 = new JMenu ("file ");
JMenu menu2 = new JMenu ("edit ");
JMenu menu3 = new JMenu ("format ");
JMenu menu4 = new JMenu ("View ");
JMenu menu5 = new JMenu ("help ");
SetJMenuBar (menuBar );
MenuBar. add (menu1 );
MenuBar. add (menu2 );
MenuBar. add (menu3 );
MenuBar. add (menu4 );
MenuBar. add (menu5 );
// File Level 2 menu
JMenuItem menu11 = new JMenuItem ("new ");
JMenuItem menu12 = new JMenuItem ("open ");
JMenuItem menu13 = new JMenuItem ("save ");
JMenuItem menu14 = new JMenuItem ("Save ");
JMenuItem menu15 = new JMenuItem ("exit ");
Menu1.add (menu11 );
Menu1.add (menu12 );
Menu1.add (menu13 );
Menu1.add (menu14 );
Menu1.addSeparator ();
Menu1.addSeparator ();
Menu1.add (menu15 );
// Edit the level-2 menu
JMenuItem men21= new JMenuItem ("undo ");
JMenuItem menu22 = new JMenuItem ("Clipboard ");
JMenuItem menu23 = new JMenuItem ("copy ");
JMenuItem menu24 = new JMenuItem ("Paste ");
JMenuItem menu25 = new JMenuItem ("delete ");
JMenuItem menu26 = new JMenuItem ("all selected ");
Menu2.add (men21 );
Menu2.addSeparator ();
Menu2.add (menu22 );
Menu2.add (menu23 );
Menu2.add (menu24 );
Menu2.add (menu25 );
Menu2.addSeparator ();
Menu2.addSeparator ();
Menu2.add (menu26 );
// Format level-2 menu
JMenuItem menu31 = new JMenuItem (" ");
JMenuItem menu32 = new JMenuItem ("font ");
Menu3.add (menu31 );
Menu3.add (menu32 );
// View the level-2 menu
JMenuItem menu41 = new JMenuItem ("status bar ");
Menu4.add (menu41 );
// Help
JMenuItem menu51 = new JMenuItem ("view help ");
JMenuItem menu52 = new JMenuItem ("about Notepad ");
Menu5.add (menu51 );
Menu5.addSeparator ();
Menu5.add (menu52 );
This. setTitle ("Notepad ");
This. setBounds (0, 0, 400,400 );
This. setVisible (true );
}
Public static void main (String [] args ){
New Dome ();
}
}
The specific functions of this notepad have not yet been implemented. That is to say, there is nothing to do with editing. In fact, it is not difficult to implement the functions. You just need to monitor these buttons and set the items triggered when you click them.
After completion: