Как создавать кнопки java

Class Button

This class creates a labeled button. The application can cause some action to happen when the button is pushed. This image depicts three views of a » Quit » button as it appears under the Solaris operating system:

The first view shows the button as it appears normally. The second view shows the button when it has input focus. Its outline is darkened to let the user know that it is an active object. The third view shows the button when the user clicks the mouse over the button, and thus requests that an action be performed.

The gesture of clicking on a button with the mouse is associated with one instance of ActionEvent , which is sent out when the mouse is both pressed and released over the button. If an application is interested in knowing when the button has been pressed but not released, as a separate gesture, it can specialize processMouseEvent , or it can register itself as a listener for mouse events by calling addMouseListener . Both of these methods are defined by Component , the abstract superclass of all components.

When a button is pressed and released, AWT sends an instance of ActionEvent to the button, by calling processEvent on the button. The button’s processEvent method receives all events for the button; it passes an action event along by calling its own processActionEvent method. The latter method passes the action event on to any action listeners that have registered an interest in action events generated by this button.

Читайте также:  Vs code unminify css

If an application wants to perform some action based on a button being pressed and released, it should implement ActionListener and register the new listener to receive events from this button, by calling the button’s addActionListener method. The application can make use of the button’s action command as a messaging protocol.

Источник

JButton basic tutorial and examples

In this Java Swing tutorial, you will learn how to use button that allows the user to perform action in a desktop application.

You know, JButton is a fundamental Swing component that renders a button on screen and responds to user’s clicking event for performing a specific task. This article summarizes common programming practices for using JButton in Swing.

Table of content:

1. Creating a JButton object

JButton button = new JButton("Edit");

Image:
Create a button with only an icon in the file system:

JButton button = new JButton(new ImageIcon("images/start.gif"));

Here the icon file start.gif is placed under images directory which is relative to the program.
Image:
Create a button with only icon inside a jar file or in classpath:

String iconPath = "/net/codejava/swing/jbutton/stop.jpg"; Icon icon = new ImageIcon(getClass().getResource(iconPath)); JButton button = new JButton(icon);

Here the icon file stop.jpg is placed under a specific package in the classpath.
Image:
Create a button with a caption and an icon:

JButton button = new JButton("Start", new ImageIcon("images/start.gif"));

Image:

2. Adding the button to a container

frame.add(button); dialog.add(button); panel.add(button); applet.getContentPane().add(button);
frame.add(button, BorderLayout.CENTER); panel.add(button, gridbagConstraints);

3. Adding event listener for JButton

button.addActionListener(new ActionListener() < @Override public void actionPerformed(ActionEvent evt) < // do everything here. >>);
button.addActionListener(new ActionListener() < @Override public void actionPerformed(ActionEvent evt) < // delegate to event handler method buttonActionPerformed(evt); >>);
private void buttonActionPerformed(ActionEvent evt) < // do something here. >
public class App extends JFrame implements ActionListener < public App() < // creates the button. // adds event listener: button.addActionListener(this); >@Override public void actionPerformed(ActionEvent evt) < // do something here. >>

Here the container ( JFrame ) must implement the ActionListener interface and override the method actionPerformed() .

4. Setting mnemonic and hotkey for JButton

button.setMnemonic(KeyEvent.VK_E);

The letter E is underlined: so user can invoke that button’s action by pressing Alt + Einstead of clicking mouse.

  • Set F2 as the hot key to invoke the button’s action:
  • String mapKey = "KEY_F2"; InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(KeyStroke.getKeyStroke("F2"), mapKey); button.getActionMap().put(mapKey, new AbstractAction() < public void actionPerformed(ActionEvent evt) < buttonActionPerformed(evt); >>);

    5. Setting a JButton as the default button

    A window can have a default button whose action will be invoked when the user hits Enter key. Here is the code to set the button as default button in the frame window:

    getRootPane().setDefaultButton(button);

    The default button is bold in the window like the 3 rd button in this screenshot:

    button set as default6. Customizing JButton’s appearance

    button.setFont(new java.awt.Font("Arial", Font.BOLD, 14)); button.setBackground(Color.YELLOW); button.setForeground(Color.BLUE);

    Image:

  • Change font style using HTML code:
  • 7. JButton demo program

    For reference, we created a demo program which incorporates all the practices mentioned above. The program looks like this:

    JButton demo program

    You can download source code of this program in the attachment section.

    Other Java Swing Tutorials:

    About the Author:

    Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.

    Add comment

    Comments

    merci
    I want to add to the button text using a variable. how do I get this to work? jpFormatPanel.add(new JButton(«Format Citation: «
    + strTemplateName

    I want to add to the button text using a variable. how do I get this to work? jpFormatPanel.add(new JButton(«Format Citation: »
    + strTemplateName
    + » — »
    + strTemplateCode),
    BorderLayout.NORTH);

    CodeJava.net shares Java tutorials, code examples and sample projects for programmers at all levels.
    CodeJava.net is created and managed by Nam Ha Minh — a passionate programmer.

    Copyright © 2012 — 2023 CodeJava.net, all rights reserved.

    Источник

    Оцените статью