Java Tutorial/SWT/Button — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:20, 31 мая 2010
Содержание
- 1 Add Image to Button
- 2 Add Radio Buttons to a Composite to form a group
- 3 ArrowButton
- 4 CheckBox Button
- 5 FlatButton
- 6 Introducing Button and Button Styles
- 7 LEFT, RIGHT, CENTER: These three styles specify the text/image alignment in buttons.
- 8 Push Button
- 9 RadioButton
- 10 Select a Radio Button
- 11 Set the alignment of the text/image label of a button
- 12 Setting Button selection
- 13 To escape the mnemonic character &, you can double it and a single "&" will be displayed.
- 14 To get and set a button"s text label
- 15 Toggle Button
- 16 You can include the mnemonic character in the text label.
Add Image to Button
To get and set a button"s image label, use the following method
public Image getImage()
public void setImage(Image image)
Add Radio Buttons to a Composite to form a group
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class RadioButtonComposite {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NULL);
composite.setLayout(new RowLayout());
Button mrButton = new Button(composite, SWT.RADIO);
mrButton.setText("Mr.");
Button mrsButton = new Button(composite, SWT.RADIO);
mrsButton.setText("Mrs.");
Button msButton = new Button(composite, SWT.RADIO);
msButton.setText("Ms.");
Button drButton = new Button(composite, SWT.RADIO);
drButton.setText("Dr.");
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
}
ArrowButton
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class ArrowButtonExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
// Create three arrow buttons
new Button(shell, SWT.ARROW);
new Button(shell, SWT.ARROW | SWT.LEFT);
new Button(shell, SWT.ARROW | SWT.DOWN);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
CheckBox Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class CheckBoxButtonExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
// Create three checkboxes
new Button(shell, SWT.CHECK).setText("Checkbox 1");
new Button(shell, SWT.CHECK).setText("Checkbox 2");
new Button(shell, SWT.CHECK).setText("Checkbox 3");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
FlatButton
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class FlatButtonExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
// Create three flat buttons
new Button(shell, SWT.FLAT).setText("Flat 1");
new Button(shell, SWT.FLAT).setText("Flat 2");
new Button(shell, SWT.FLAT).setText("Flat 3");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Introducing Button and Button Styles
- SWT uses Button to represent checkboxes, toggle buttons, and radio buttons.
- You determine the type by the style constants.
- You may pass only one of SWT.LEFT, SWT.CENTER, or SWT.RIGHT.
- You may pass only one of SWT.ARROW, SWT.CHECK, SWT.PUSH, SWT.RADIO, or SWT.TOGGLE.
- If you pass SWT.ARROW, you may pass only one of SWT.UP, SWT.DOWN, SWT.LEFT, or SWT.RIGHT.
StyleDescriptionSWT.ARROWCreates a push button that displays an arrow.SWT.CHECKCreates a checkbox.SWT.PUSHCreates a push button.SWT.RADIOCreates a radio button.SWT.TOGGLECreates a push button that preserves its pushed or nonpushed state.SWT.FLATCreates a push button that appears flat.SWT.UPWhen combined with SWT.ARROW, displays an upward-pointing arrow.SWT.DOWNWhen combined with SWT.ARROW, displays a downward-pointing arrow.SWT.CENTERCenters the associated text.SWT.LEFTLeft-aligns the associated text. When combined with SWT.ARROW, displays a leftward-pointing arrow.SWT.RIGHTRight-aligns the associated text. When combined with SWT.ARROW, displays a rightward-pointing arrow.
LEFT, RIGHT, CENTER: These three styles specify the text/image alignment in buttons.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ButtonTextAlignment {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout());
Button button = new Button(shell, SWT.RIGHT);
button.setText("text on the button");
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
}
Push Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class PushButtonExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
// Create three push buttons
new Button(shell, SWT.PUSH).setText("Push 1");
new Button(shell, SWT.PUSH).setText("Push 2");
new Button(shell, SWT.PUSH).setText("Push 3");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
RadioButton
By default all the radio buttons sharing the same parent belong to the same group.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class RadioButtonExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
// Create three radio buttons
new Button(shell, SWT.RADIO).setText("Radio 1");
new Button(shell, SWT.RADIO).setText("Radio 2");
new Button(shell, SWT.RADIO).setText("Radio 3");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Select a Radio Button
If you need to select a button programmatically, use the following method:
public void setSelection(boolean selected)
Set the alignment of the text/image label of a button
Set the alignment of the text/image label of a button in the constructor of the Button class, or you can use the setAlignment method after it has been created:
public void setAlignment(int alignment)
Setting Button selection
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ButtonSelection {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
for (int i = 0; i < 20; i++) {
Button button = new Button(shell, SWT.TOGGLE);
button.setText("B" + i);
}
Control[] children = shell.getChildren();
for (int i = 0; i < children.length; i++) {
Control child = children[i];
((Button) child).setSelection(true);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
To escape the mnemonic character &, you can double it and a single "&" will be displayed.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class EscapeMnemonicCharacter {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new RowLayout());
Button button = new Button(shell, SWT.RIGHT);
button.setText("&&text on the button");
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
}
To get and set a button"s text label
public String getText()
public void setText(String text)
Toggle Button
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class ToggleButtonExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, true));
// Create three toggle buttons
new Button(shell, SWT.TOGGLE).setText("Toggle 1");
new Button(shell, SWT.TOGGLE).setText("Toggle 2");
new Button(shell, SWT.TOGGLE).setText("Toggle 3");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
You can include the mnemonic character in the text label.
Inserting an ampersand (&) before a character makes the character the mnemonic.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ButtonAmpersandCharacter {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new RowLayout());
Button button = new Button(shell, SWT.RIGHT);
button.setText("&text on the button");
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
}
When the user presses a key matching the mnemonic, the corresponding button is selected and a selection event is generated as a result.