multibuttons.java

来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 54 行

JAVA
54
字号
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;  // need this to add controls
import java.awt.*;  // need this for layout manager 

public class MultiButtons extends JFrame
{
  private JFrame mainFrame;
  private JButton messageButton;
  private JButton clearButton;
  private JButton exitButton;
 
  public MultiButtons() // a constructor
  {
    mainFrame = new JFrame("The Hello Application - Ver. 1.0");

      // create the button objects
    messageButton = new JButton("Message");  
    clearButton = new JButton("Clear");
    exitButton = new JButton("Exit");

      // get the content pane & specify layout manager
    Container c = mainFrame.getContentPane();
    c.setLayout(new FlowLayout());
   
       // add the button to the ContentPane 
    c.add(messageButton);
    c.add(clearButton);
    c.add(exitButton);
      
      // create accelerator keys
    messageButton.setMnemonic('M');
    clearButton.setMnemonic('C');
    exitButton.setMnemonic('x');

    mainFrame.setSize(300,100);

       // define and register window event handler
    mainFrame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });
        
    mainFrame.show();
  }

  public static void main(String args[])
  {
    MultiButtons app = new MultiButtons();  // instantiate a GUI object 
  }

}   // end of  class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?