⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buttonguithree.java

📁 《A first book of java》by Gary J.Bronson 北大出版社
💻 JAVA
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;  // need this to add controls
public class ButtonGuiThree extends JFrame
{
  private JFrame mainFrame;
  private JButton firstButton;              // Step 1 - first statement
 
  public ButtonGuiThree() // a constructor
  {
    mainFrame = new JFrame("Example of a Gui with a Button");

      // create a button object
    firstButton = new JButton("Press me"); 
    
      // get the content pane
    Container c = mainFrame.getContentPane();  

      // add the button to the ContentPane 
    c.add(firstButton);                       

    firstButton.setToolTipText("This is a button");    
    firstButton.setMnemonic('P');                  

    mainFrame.setSize(300,150);

       // define and register window event handler
    mainFrame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    
      // create and register the button event handler
    ButEventHandler bhandler = new ButEventHandler();  // instantiate a handler
    firstButton.addActionListener(bhandler);  // register the handler    

    mainFrame.show();
  } 
  
    // inner class for the button event handler
  class ButEventHandler implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      JOptionPane.showMessageDialog(null, "Button was Pressed",
        "Event Handler Message",JOptionPane.INFORMATION_MESSAGE);
    }
  }  // end of inner class

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

}  // end of ButtonGuiThree class

⌨️ 快捷键说明

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