下面列出了java.awt.Button#addActionListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void createAndShowGUI() {
frame = new Frame("Test Frame");
textField1 = new TextField(5);
button = new Button("Go");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField1.requestFocusInWindow();
startUpdate();
}
});
frame.setLayout(new FlowLayout());
frame.setSize(400, 200);
frame.add(textField1);
frame.add(new TextField(5));
frame.add(new TextField(5));
frame.add(button);
frame.setVisible(true);
}
public void init(){
setLayout(new GridLayout(0,2));
selectButton = new Button("select");
add(selectButton);
selectButton.addActionListener(this);
acquireButton = new Button("acquire");
add(acquireButton);
acquireButton.addActionListener(this);
scanner=Scanner.getDevice();
scanner.addListener(this);
writer = (ImageWriter)ImageIO.getImageWritersByFormatName("tif").next();
System.out.println(writer.getClass().getName());
// should be: uk.co.mmscomputing.imageio.tiff.TIFFImageWriter
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
passB = new Button( "pass" );
passB.setActionCommand( "pass" );
passB.addActionListener( this );
buttonP.add( "East", passB );
failB = new Button( "fail" );
failB.setActionCommand( "fail" );
failB.addActionListener( this );
buttonP.add( "West", failB );
add( "South", buttonP );
pack();
show();
}
public TestDialog( Frame frame, String name )
{
super( frame, name );
int scrollBoth = TextArea.SCROLLBARS_BOTH;
instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
add( "North", instructionsText );
messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
add("Center", messageText);
passB = new Button( "pass" );
passB.setActionCommand( "pass" );
passB.addActionListener( this );
buttonP.add( "East", passB );
failB = new Button( "fail" );
failB.setActionCommand( "fail" );
failB.addActionListener( this );
buttonP.add( "West", failB );
add( "South", buttonP );
pack();
show();
}
public static void main(final String[] args) throws AWTException {
final bug7097771 frame = new bug7097771();
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
final Button button = new Button();
button.addActionListener(frame);
frame.add(button);
frame.setVisible(true);
sleep();
frame.setEnabled(false);
button.setEnabled(false);
button.setEnabled(true);
sleep();
Util.clickOnComp(button, new Robot());
sleep();
frame.dispose();
if (action) {
throw new RuntimeException("Button is not disabled.");
}
}
private static void createAndShowGUI() {
frame = new Frame("Test Frame");
textField1 = new TextField(5);
button = new Button("Go");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField1.requestFocusInWindow();
startUpdate();
}
});
frame.setLayout(new FlowLayout());
frame.setSize(400, 200);
frame.add(textField1);
frame.add(new TextField(5));
frame.add(new TextField(5));
frame.add(button);
frame.setVisible(true);
}
public Requester(Frame parent, String title, String lines, String posText_, String negText) {
super(parent, title, true);
setLayout(new GridBagLayout());
posText = posText_;
add(new MultiLineLabel(lines), Awt.constraints(true));
if(posText != null) {
Button pos = new Button(posText);
add(pos, Awt.constraints(false, 10, 4, GridBagConstraints.HORIZONTAL));
pos.addActionListener(this);
}
if(negText != null) {
Button neg = new Button(negText);
add(neg, Awt.constraints(true, 10, 4, GridBagConstraints.HORIZONTAL));
neg.addActionListener(this);
}
pack();
}
public static void main(final String[] args) throws AWTException {
final bug7097771 frame = new bug7097771();
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
final Button button = new Button();
button.addActionListener(frame);
frame.add(button);
frame.setVisible(true);
sleep();
frame.setEnabled(false);
button.setEnabled(false);
button.setEnabled(true);
sleep();
Util.clickOnComp(button, new Robot());
sleep();
frame.dispose();
if (action) {
throw new RuntimeException("Button is not disabled.");
}
}
private void init(Component parent, String title, String message,
String buttonText) {
Panel p = new Panel();
add("Center", new Label(message));
Button btn = new Button(buttonText);
btn.addActionListener(this);
p.add(btn);
add("South", p);
pack();
Dimension dDim = getSize();
if (parent != null) {
Rectangle fRect = parent.getBounds();
setLocation(fRect.x + ((fRect.width - dDim.width) / 2),
fRect.y + ((fRect.height - dDim.height) / 2));
}
}
private void executeTest() {
GraphicsDevice defDev = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int x = 0;
Frame f = null;
for (x = 0; x < gd.length; x ++) {
if (gd[x] != defDev) {
secFrame = new Frame("Screen " + x + " - secondary", gd[x].getDefaultConfiguration());
f = secFrame;
} else {
primaryFrame = new Frame("Screen " + x + " - primary", gd[x].getDefaultConfiguration());
f = primaryFrame;
}
Button b = new Button("Print");
b.addActionListener(this);
f.add("South", b);
f.addWindowListener (new WindowAdapter() {
public void windowClosing(WindowEvent we) {
((Window) we.getSource()).dispose();
}
});
f.setSize(200, 200);
f.setVisible(true);
}
}
private void init(Component parent, String title, String message,
String buttonText) {
Panel p = new Panel();
add("Center", new Label(message));
Button btn = new Button(buttonText);
btn.addActionListener(this);
p.add(btn);
add("South", p);
pack();
Dimension dDim = getSize();
if (parent != null) {
Rectangle fRect = parent.getBounds();
setLocation(fRect.x + ((fRect.width - dDim.width) / 2),
fRect.y + ((fRect.height - dDim.height) / 2));
}
}
/**
* Adds the directory or file field.
*
* @param label
* the label
* @param defaultPath
* the default path
* @param columns
* the columns
*/
public void addDirectoryOrFileField(String label, String defaultPath, int columns) {
addStringField(label, defaultPath, columns);
if (isHeadless())
return;
TextField text = (TextField) stringField.lastElement();
GridBagLayout layout = (GridBagLayout) getLayout();
GridBagConstraints constraints = layout.getConstraints(text);
Button button = new Button("Browse...");
DirectoryListener listener = new DirectoryListener("Browse for " + label, text,
JFileChooser.FILES_AND_DIRECTORIES);
button.addActionListener(listener);
button.addKeyListener(this);
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
panel.add(text);
panel.add(button);
layout.setConstraints(panel, constraints);
add(panel);
}
public static void main(final String[] args) throws AWTException {
final bug7097771 frame = new bug7097771();
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
final Button button = new Button();
button.addActionListener(frame);
frame.add(button);
frame.setVisible(true);
sleep();
frame.setEnabled(false);
button.setEnabled(false);
button.setEnabled(true);
sleep();
Util.clickOnComp(button, new Robot());
sleep();
frame.dispose();
if (action) {
throw new RuntimeException("Button is not disabled.");
}
}
/**
* Adds the file field.
*
* @param label
* the label
* @param defaultPath
* the default path
* @param columns
* the columns
*/
public void addFileField(String label, String defaultPath, int columns) {
addStringField(label, defaultPath, columns);
if (isHeadless())
return;
TextField text = (TextField) stringField.lastElement();
GridBagLayout layout = (GridBagLayout) getLayout();
GridBagConstraints constraints = layout.getConstraints(text);
Button button = new Button("Browse...");
FileListener listener = new FileListener("Browse for " + label, text);
button.addActionListener(listener);
button.addKeyListener(this);
Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
panel.add(text);
panel.add(button);
layout.setConstraints(panel, constraints);
add(panel);
}
private void init(Component parent, String title, String message,
String buttonText) {
Panel p = new Panel();
add("Center", new Label(message));
Button btn = new Button(buttonText);
btn.addActionListener(this);
p.add(btn);
add("South", p);
pack();
Dimension dDim = getSize();
if (parent != null) {
Rectangle fRect = parent.getBounds();
setLocation(fRect.x + ((fRect.width - dDim.width) / 2),
fRect.y + ((fRect.height - dDim.height) / 2));
}
}
public ConfirmationDialog(Frame parent, String[] messages) {
super(parent, true);
setTitle("Confirmation Dialog");
Font font = new Font("Courier", Font.PLAIN, 12);
setFont(font);
int rows = messages.length;
Panel textPanel = new Panel();
textPanel.setLayout(new GridLayout(rows,1));
for(int i = 0; i < rows; i++){
textPanel.add(new Label(messages[i]));
}
add("Center", textPanel);
Panel p = new Panel();
p.setLayout(new FlowLayout());
Button yes = new Button("Yes");
yes.addActionListener(this);
p.add(yes);
Button no = new Button("No");
no.addActionListener(this);
p.add(no);
add("South", p);
setSize(300, 100);
setLocation(100, 200);
pack();
}
private void createInstructionUI() {
instructionFrame = new Frame("Native Print Dialog and Page Dialog");
layout = new GridBagLayout();
mainControlPanel = new Panel(layout);
resultButtonPanel = new Panel(layout);
GridBagConstraints gbc = new GridBagConstraints();
String instructions
= "\nINSTRUCTIONS:\n"
+ "\n 1. Click on the 'show a native print dialog' button. A "
+ "native print dialog will come up."
+ "\n 2. Click on the 'Cancel' button on Mac OS X or "
+ "'close'(X) on other paltforms. Dialog will be closed."
+ "\n 3. After the dialog is closed another window should "
+ "become the active window."
+ "\n 4. If there no any active window then the test has "
+ "failed. Click on 'Fail' Button."
+ "\n 5. Click on the 'show a native page dialog' button. A "
+ "native page dialog will come up."
+ "\n 6. Click on the 'Cancel' button on Mac OS X or "
+ "'close'(X) on other paltforms. Dialog will be closed."
+ "\n 7. After the dialog is closed another window should "
+ "become the active window."
+ "\n 8. If there no any active window then the test has "
+ "failed. Click on 'Fail' Button."
+ "\n 9. Test Passed. Click on 'Pass' Button.";
instructionTextArea = new TextArea(13, 80);
instructionTextArea.setText(instructions);
instructionTextArea.setEnabled(false);
instructionTextArea.setBackground(Color.white);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0.5;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainControlPanel.add(instructionTextArea, gbc);
passButton = new Button("Pass");
passButton.setName("Pass");
passButton.addActionListener((ActionListener) this);
failButton = new Button("Fail");
failButton.setName("Fail");
failButton.addActionListener((ActionListener) this);
setButtonEnable(false);
gbc.gridx = 0;
gbc.gridy = 0;
resultButtonPanel.add(passButton, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
resultButtonPanel.add(failButton, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
mainControlPanel.add(resultButtonPanel, gbc);
instructionFrame.add(mainControlPanel);
instructionFrame.pack();
instructionFrame.setVisible(true);
}
public static void main(final String[] args) throws Exception {
if (sun.awt.OSInfo.getOSType() == sun.awt.OSInfo.OSType.MACOSX) {
System.err.println("This test is not for OS X. Menu.setFont() is not supported on OS X.");
return;
}
//Components initialization.
frame.setMenuBar(mb);
mb.setFont(new Font("Helvetica", Font.ITALIC, 5));
final Button button = new Button("Click Me");
button.addActionListener(new Listener());
frame.setLayout(new CardLayout());
frame.add(button, "First");
frame.setSize(400, 400);
frame.setVisible(true);
sleep();
final int fInsets = frame.getInsets().top; //Frame insets without menu.
addMenu();
final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.
final int menuBarHeight = fMenuInsets - fInsets;
// There is no way to change menubar height on windows. But on windows
// we can try to split menubar in 2 rows.
for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {
// Fill whole menubar.
addMenu();
}
mb.remove(0);
frame.validate();
sleep();
// Test execution.
// On XToolkit, menubar font should be changed to 60.
// On WToolkit, menubar font should be changed to default and menubar
// should be splitted in 2 rows.
mb.setFont(new Font("Helvetica", Font.ITALIC, 60));
sleep();
final Robot r = new Robot();
r.setAutoDelay(200);
final Point pt = frame.getLocation();
r.mouseMove(pt.x + frame.getWidth() / 2,
pt.y + fMenuInsets + menuBarHeight / 2);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
sleep();
frame.dispose();
if (clicked) {
fail("Font was not changed");
}
}
public static void main(final String[] args) throws Exception {
if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {
System.err.println("This test is not for OS X. Menu.setFont() is not supported on OS X.");
return;
}
//Components initialization.
frame.setMenuBar(mb);
mb.setFont(new Font("Helvetica", Font.ITALIC, 5));
final Robot r = new Robot();
r.setAutoDelay(200);
final Button button = new Button("Click Me");
button.addActionListener(new Listener());
frame.setLayout(new CardLayout());
frame.add(button, "First");
frame.setSize(400, 400);
frame.setVisible(true);
sleep(r);
final int fInsets = frame.getInsets().top; //Frame insets without menu.
addMenu();
final int fMenuInsets = frame.getInsets().top; //Frame insets with menu.
final int menuBarHeight = fMenuInsets - fInsets;
// There is no way to change menubar height on windows. But on windows
// we can try to split menubar in 2 rows.
for (int i = 0; i < 100 && fMenuInsets == frame.getInsets().top; ++i) {
// Fill whole menubar.
addMenu();
}
mb.remove(0);
frame.validate();
sleep(r);
// Test execution.
// On XToolkit, menubar font should be changed to 60.
// On WToolkit, menubar font should be changed to default and menubar
// should be splitted in 2 rows.
mb.setFont(new Font("Helvetica", Font.ITALIC, 60));
sleep(r);
final Point pt = frame.getLocation();
r.mouseMove(pt.x + frame.getWidth() / 2,
pt.y + fMenuInsets + menuBarHeight / 2);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
sleep(r);
frame.dispose();
if (clicked) {
fail("Font was not changed");
}
}