阅读(2679) (0)

Java Swing JButton

2017-01-09 19:23:21 更新

Java Swing教程 - Java Swing JButton


JButton也称为按钮。用户按下或单击JButton以执行操作。

创建JButton

JButton可以显示文本和图标。 我们可以使用下表中列出的构造函数创建一个JButton。

构造函数 描述
JButton() 创建一个没有任何标签或图标的JButton。
JButton(String text) 创建JButton并将指定的文本设置为其标签。
JButton(图标图标) 创建一个带有图标且没有标签的JButton。
JButton(字符串文字,图标图标) JButton(字符串文字,图标图标)...
JButton(行动) JButton(行动)...

我们可以创建一个JButton,其文本在以下代码中为OK。

JButton closeButton  = new JButton("OK");

要创建一个带有图标的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"));
  }
}

设置JButton组件中的标签和图标之间的间隙大小

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

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


事件

下面是我们如何为closeButton的ActionEvent使用lambda表达式添加操作处理程序。

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

助记键

JButton支持键盘助记符,也称为键盘快捷键或键盘指示器。

助记键通常与诸如Alt键的修改键组合地按压。

要安装C键作为关闭JButton的助记符。当我们按Alt + C时,单击关闭JButton。

如果在JButton文本中找到由助记键表示的字符,则它的第一个出现被加下划线。

以下代码段将C设置为关闭JButton的助记键:

closeButton.setMnemonic("C");

我们还可以使用以下代码设置助记键。

closeButton.setMnemonic(KeyEvent.VK_C);

要将F3键设置为助记键,我们可以使用KeyEvent.VK_F3常量。

closeButton.setMnemonic(KeyEvent.VK_F3);

助记符,标签中的一个字符出现下划线。

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关联的Action对象。
2 void setAction(Action a)设置JButton的Action对象,并从指定的Action对象刷新JButton的所有属性。
3 图标getIcon()返回与JButton相关联的Icon对象。
4 void setIcon(图标图标)设置JButton的图标。
5 int getMnemonic()返回此JButton的键盘助记符。
6 int getMnemonic()返回此JButton的键盘助记符。...
7 String getText()返回JButton的文本。
8 void setText()设置JButton的文本。

JButton行动

以下代码显示了如何使用 Action 对象创建JButton。

我们可以创建一个Action对象,然后从中创建一个JButton。 操作对象可以重用到创建菜单项。 通过禁用操作对象,我们可以禁用从它创建的所有UI控件。

Action对象封装了按钮的状态和行为。

我们可以在Action对象中设置文本,图标,助记符,工具提示文本,其他属性和ActionListener,并使用相同的Action对象来创建不同的UI控件。

Action 是一个接口。 AbstractAction 类提供了Action接口的默认实现。

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设置任何属性,我们可以通过使用Action接口的putValue(String key,Object value)方法来实现。

要为Action对象设置工具提示文本closeAction.putValue(Action.SHORT_DESCRIPTION,“关闭应用程序");

要为Action对象设置助记键close Action.putValue(Action.MNEMONIC KEY,KeyEvent.VK_C);

JButton行动...

要使用不同的字体和颜色或多行显示组件上的文本,我们可以使用HTML字符串作为组件的文本。

Swing组件内置支持显示HTML文本作为其标签。

我们可以使用HTML格式的字符串作为JButton,JMenuItem,JLabel,JToolTip,JTabbedPane,JTree等的标签。

当使用HTML字符串时,应以< html>开头和结尾。 和< / html> 标签。

例如,要在JButton上显示文本“关闭窗口"作为其标签,请使用以下代码:

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中显示多个文本,请添加< br /> 到html串。

JButton b1  = new JButton(); 
b1.setText("<html><body>Line 1  <br/>Line 2</body></html>");