Java Swing JButton

2018-01-09 19:23 更新

Java Swing教程 - Java Swing JButton


JButton也稱為按鈕。用戶按下或單擊JButton以執(zhí)行操作。

創(chuàng)建JButton

JButton可以顯示文本和圖標。 我們可以使用下表中列出的構(gòu)造函數(shù)創(chuàng)建一個JButton。

構(gòu)造函數(shù) 描述
JButton() 創(chuàng)建一個沒有任何標簽或圖標的JButton。
JButton(String text) 創(chuàng)建JButton并將指定的文本設(shè)置為其標簽。
JButton(圖標圖標) 創(chuàng)建一個帶有圖標且沒有標簽的JButton。
JButton(字符串文字,圖標圖標) JButton(字符串文字,圖標圖標)...
JButton(行動) JButton(行動)...

我們可以創(chuàng)建一個JButton,其文本在以下代碼中為OK。

JButton closeButton  = new JButton("OK");

要創(chuàng)建一個帶有圖標的JButton,我們需要一個圖像文件。

Icon  previousIcon = new ImageIcon("C:/images/previous.gif"); 
Icon  nextIcon = new ImageIcon("C:/images/next.gif");

// Create buttons with  icons
JButton previousButton  = new JButton("Previous", previousIcon); 
JButton nextButton = new JButton("Next", nextIcon);

將圖標添加到JButton

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
//  w w w  .  j  av  a  2s  .c  om
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon warnIcon = new ImageIcon("yourFile.gif");
    JButton button2 = new JButton(warnIcon);
    frame.add(button2);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

以下代碼顯示如何向JButton添加各種圖標。

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Main {
  public static void main(String[] argv) throws Exception {
    JButton button = new JButton();
    // Add rollover icon
    Icon rolloverIcon = new ImageIcon("r.gif");
    button.setRolloverIcon(rolloverIcon);

    // Add pressed icon
    Icon pressedIcon = new ImageIcon("p.gif");
    button.setPressedIcon(pressedIcon);

    // To remove rollover icon, set to null
    button.setRolloverIcon(null);

    // To remove pressed icon, set to null
    button.setPressedIcon(null);

  }
}

將禁用圖標添加到JButton組件

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Main {
  public static void main(String[] argv) throws Exception {
    JButton button = new JButton();
    // Icon will appear gray
    button.setEnabled(false);

    // Set a disabled version of icon
    Icon disabledIcon = new ImageIcon("d.gif");
    button.setDisabledIcon(disabledIcon);

    // To remove the disabled version of the icon, set to null
    button.setDisabledIcon(null);

    button.setDisabledIcon(new ImageIcon("icon.gif"));
  }
}

設(shè)置JButton組件中的標簽和圖標之間的間隙大小

    JButton button = new JButton();
    // Get gap size; default is 4
    int gapSize = button.getIconTextGap();

    // Set gap size
    button.setIconTextGap(8);


事件

下面是我們?nèi)绾螢閏loseButton的ActionEvent使用lambda表達式添加操作處理程序。

closeButton.addActionListener(() ->    {
    // The code  to handle the   action  event goes  here
});

助記鍵

JButton支持鍵盤助記符,也稱為鍵盤快捷鍵或鍵盤指示器。

助記鍵通常與諸如Alt鍵的修改鍵組合地按壓。

要安裝C鍵作為關(guān)閉JButton的助記符。當我們按Alt + C時,單擊關(guān)閉JButton。

如果在JButton文本中找到由助記鍵表示的字符,則它的第一個出現(xiàn)被加下劃線。

以下代碼段將C設(shè)置為關(guān)閉JButton的助記鍵:

closeButton.setMnemonic("C");

我們還可以使用以下代碼設(shè)置助記鍵。

closeButton.setMnemonic(KeyEvent.VK_C);

要將F3鍵設(shè)置為助記鍵,我們可以使用KeyEvent.VK_F3常量。

closeButton.setMnemonic(KeyEvent.VK_F3);

助記符,標簽中的一個字符出現(xiàn)下劃線。

import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
/*  w ww  . j  a  v a  2s  .co m*/
public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("DefaultButton");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button1 = new JButton("Text Button");
    button1.setMnemonic(KeyEvent.VK_B);
    frame.add(button1);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}


助記鍵...

下表顯示了JButton類的常用方法。

ID 方法/說明
1 Action getAction()返回與JButton關(guān)聯(lián)的Action對象。
2 void setAction(Action a)設(shè)置JButton的Action對象,并從指定的Action對象刷新JButton的所有屬性。
3 圖標getIcon()返回與JButton相關(guān)聯(lián)的Icon對象。
4 void setIcon(圖標圖標)設(shè)置JButton的圖標。
5 int getMnemonic()返回此JButton的鍵盤助記符。
6 int getMnemonic()返回此JButton的鍵盤助記符。...
7 String getText()返回JButton的文本。
8 void setText()設(shè)置JButton的文本。

JButton行動

以下代碼顯示了如何使用 Action 對象創(chuàng)建JButton。

我們可以創(chuàng)建一個Action對象,然后從中創(chuàng)建一個JButton。 操作對象可以重用到創(chuàng)建菜單項。 通過禁用操作對象,我們可以禁用從它創(chuàng)建的所有UI控件。

Action對象封裝了按鈕的狀態(tài)和行為。

我們可以在Action對象中設(shè)置文本,圖標,助記符,工具提示文本,其他屬性和ActionListener,并使用相同的Action對象來創(chuàng)建不同的UI控件。

Action 是一個接口。 AbstractAction 類提供了Action接口的默認實現(xiàn)。

AbstractAction 是一個抽象類。我們需要從它繼承類。

import java.awt.Container;
import java.awt.event.ActionEvent;
//from w w w. j  a va2s.  c  om
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
class CloseAction extends AbstractAction {
  public CloseAction() {
    super("Close");
  }

  @Override
  public void actionPerformed(ActionEvent event) {
    System.exit(0);
  }
}
public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JButton closeButton = new JButton(new CloseAction());
    contentPane.add(closeButton);

    frame.pack();
    frame.setVisible(true);
  }
}

要在使用Action對象時為JButton設(shè)置任何屬性,我們可以通過使用Action接口的putValue(String key,Object value)方法來實現(xiàn)。

要為Action對象設(shè)置工具提示文本closeAction.putValue(Action.SHORT_DESCRIPTION,“關(guān)閉應(yīng)用程序");

要為Action對象設(shè)置助記鍵close Action.putValue(Action.MNEMONIC KEY,KeyEvent.VK_C);

JButton行動...

要使用不同的字體和顏色或多行顯示組件上的文本,我們可以使用HTML字符串作為組件的文本。

Swing組件內(nèi)置支持顯示HTML文本作為其標簽。

我們可以使用HTML格式的字符串作為JButton,JMenuItem,JLabel,JToolTip,JTabbedPane,JTree等的標簽。

當使用HTML字符串時,應(yīng)以< html>開頭和結(jié)尾。 和< / html> 標簽。

例如,要在JButton上顯示文本“關(guān)閉窗口"作為其標簽,請使用以下代碼:

JButton b1  = new JButton("<html><body><b>Close</b>  Window</body></html>");

要顯示包含HTML標簽的字符串一個標簽,使用html.disable組件的客戶端屬性禁用默認的HTML解釋。

以下代碼禁用JButton的HTML屬性,并在其標簽中使用HTML標記:

JButton b3  = new JButton(); 
b3.putClientProperty("html.disable",  Boolean.TRUE); 
b3.setText("<html><body>HTML is disabled</body></html>");

要在JButton中顯示多個文本,請?zhí)砑?lt; br /> 到html串。

JButton b1  = new JButton(); 
b1.setText("<html><body>Line 1  <br/>Line 2</body></html>");
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號