setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn = new JButton("Open the other JFrame!"); btn.addActionListener(this); btn.setActionCommand("Open"); add(btn); pack(); public void run() new StartupWindow().setVisible(true);
import java.awt.GridLayout; import javax.swing.*; class RadioBtnDialogPanel extends JPanel { private static final String[] BUTTON_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; private ButtonGroup buttonGroup = new ButtonGroup(); public RadioBtnDialogPanel() { setLayout(new GridLayout(0, 1)); // give JPanel a decent layout // create radio buttons, add to button group and to JPanel for (String buttonText : BUTTON_TEXTS) { JRadioButton radioBtn = new JRadioButton(buttonText); radioBtn.setActionCommand(buttonText); // set the actionCommand here buttonGroup.add(radioBtn); add(radioBtn); } } // getter or accessor method to get selected JRadioButton's actionCommand text public String getSelectedButtonText() { ButtonModel model = buttonGroup.getSelection(); if (model == null) { // no radiobutton selected return ""; } else { return model.getActionCommand(); } } }
CopyPastePanel(CP cp) { super(new GridLayout(2, 1, 0, 2)); this.cp = cp; if(copyIcon == null) { copyIcon = TinyLookAndFeel.loadIcon("cp_icons/mencopy.gif"); pasteIcon = TinyLookAndFeel.loadIcon("cp_icons/menpaste.gif"); pasteDisabledIcon = TinyLookAndFeel.loadIcon("cp_icons/menpastedis.gif"); } JButton b = new IconButton(copyIcon); b.setActionCommand("copy"); b.setToolTipText("Copy Parameter Set"); b.addActionListener(this); add(b); pasteButton = new IconButton(pasteIcon); pasteButton.setDisabledIcon(pasteDisabledIcon); pasteButton.setActionCommand("paste"); pasteButton.setToolTipText("Paste Parameter Set"); pasteButton.addActionListener(this); pasteButton.setEnabled(false); add(pasteButton); }
frame.setSize(200, 100); JButton hello = new JButton("Hello"); hello.setActionCommand(Actions.HELLO.name()); hello.addActionListener(instance); frame.add(hello); JButton goodbye = new JButton("Goodbye"); goodbye.setActionCommand(Actions.GOODBYE.name()); goodbye.addActionListener(instance); frame.add(goodbye); frame.setVisible(true);
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0)); BtnListener btnListener = new BtnListener(); for (String text : TEXTS) { JToggleButton toggleBtn = new JToggleButton(text); toggleBtn.addActionListener(btnListener); toggleBtn.setActionCommand(text); btnPanel.add(toggleBtn); btnGroup.add(toggleBtn); JPanel otherPanel = new JPanel(); otherPanel.add(textField ); // just to take focus elsewhere add(btnPanel); add(otherPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true);
String[] data = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}; JPanel panel = new JPanel(new GridLayout(0, 1)); MyButtonGroup myBtnGrp = new MyButtonGroup(); myBtnGrp.addActionListener(new ActionListener() { radioBtn.setActionCommand(text); myBtnGrp.add(radioBtn); panel.add(radioBtn); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
int h = bi.getHeight(); int step = w/3; JPanel p = new JPanel(new GridLayout(3,3)); p.setOpaque(false); int count = 0; JButton button = new JButton(new ImageIcon(icon)); button.setActionCommand(""+count); button.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent ae) { p.add(button); } else { JLabel label = new JLabel(new ImageIcon(icon)); p.add(label); JPanel center = new JPanel(new GridBagLayout()); center.setBackground(Color.BLACK); center.add(p); JOptionPane.showMessageDialog(null, center);
import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import javax.swing.GroupLayout;import javax.swing.GroupLayout.Alignment;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.LayoutStyle.ComponentPlacement;import javax.swing.border.EmptyBorder;import javax.swing.filechooser.FileFilter;import javax.swing.filechooser.FileNameExtensionFilter;public class ConfigureDialog extends JDialog implements ActionListener{private static final long serialVersionUID=1L;private final JPanel contentPanel=new JPanel();private JTextField driverPathTextField;private JLabel lblDriverPath;private JButton btnBrowse;public static void main(String[]args){try{ConfigureDialog dialog=new ConfigureDialog(new JFrame());dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);dialog.setVisible(true);}catch(Exception e){e.printStackTrace();}} public ConfigureDialog(JFrame parent){super(parent,"",true);if(parent!=null){Dimension parentSize=parent.getSize();Point p=parent.getLocation();setLocation(p.x+parentSize.width+100,p.y+parentSize.height/1);} setBounds(100,100,479,141);getContentPane().setLayout(new BorderLayout());contentPanel.setBorder(new EmptyBorder(5,5,5,5));getContentPane().add(contentPanel,BorderLayout.CENTER);{lblDriverPath=new JLabel("Driver Path : ");} {driverPathTextField=new JTextField(System.getProperty("web.ie.driver"));driverPathTextField.setColumns(10);} btnBrowse=new JButton("Browse");GroupLayout gl_contentPanel=new GroupLayout(contentPanel);gl_contentPanel.setHorizontalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addContainerGap().addComponent(lblDriverPath).addPreferredGap(ComponentPlacement.RELATED).addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addComponent(btnBrowse).addComponent(driverPathTextField,GroupLayout.DEFAULT_SIZE,207,Short.MAX_VALUE)).addContainerGap()));gl_contentPanel.setVerticalGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPanel.createSequentialGroup().addGap(5).addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE).addComponent(driverPathTextField,GroupLayout.PREFERRED_SIZE,GroupLayout.DEFAULT_SIZE,GroupLayout.PREFERRED_SIZE).addComponent(lblDriverPath)).addPreferredGap(ComponentPlacement.UNRELATED).addComponent(btnBrowse).addContainerGap(21,Short.MAX_VALUE)));contentPanel.setLayout(gl_contentPanel);{JPanel buttonPane=new JPanel();buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));getContentPane().add(buttonPane,BorderLayout.SOUTH);{JButton okButton=new JButton("OK");okButton.setActionCommand("OK");okButton.addActionListener(this);buttonPane.add(okButton);getRootPane().setDefaultButton(okButton);} {JButton cancelButton=new JButton("Cancel");cancelButton.setActionCommand("Cancel");cancelButton.addActionListener(this);buttonPane.add(cancelButton);}} btnBrowse.addActionListener(this);}@Override public void actionPerformed(ActionEvent e){if("Cancel".contains(e.getActionCommand())){dispose();}else if("Browse".contains(e.getActionCommand())){JFileChooser fileopen=new JFileChooser();FileFilter filter=new FileNameExtensionFilter("exe file","exe");fileopen.addChoosableFileFilter(filter);fileopen.setAcceptAllFileFilterUsed(false);fileopen.setFileFilter(filter);fileopen.setFileSelectionMode(JFileChooser.FILES_ONLY);int ret=fileopen.showOpenDialog(this);if(ret==JFileChooser.APPROVE_OPTION){File file=fileopen.getSelectedFile();System.out.println(file);driverPathTextField.setText(file.getPath());}}else if("OK".contains(e.getActionCommand())){System.setProperty("web.ie.driver",driverPathTextField.getText());dispose();}}}
j.setSize(300,300); j.setLayout(new FlowLayout()); j.setDefaultCloseOperation(j.EXIT_ON_CLOSE); j.setVisible(true); j.add(jlab); JButton j1=new JButton("Button1"); j1.setActionCommand("Your pressed Button 1"); j1.addActionListener(this ); j.add(j1); JButton j2=new JButton("Button2"); j2.setActionCommand("Your pressed Button 2"); j2.addActionListener(this ); j.add(j2);
setDefaultCloseOperation(EXIT_ON_CLOSE); for (int i = 0; i < 10; i++) { String name = new Integer(i+1).toString(); btn = new JButton(name); btn.setActionCommand(name); btn.addActionListener(new Action(field)); add(btn); add(field); setVisible(true);
super(); initGUI(); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); JPanel panel = new JPanel(); for (int i = 0; i < NUM_BUTTONS; i++) { String label = "Button " + i; JButton button = new JButton(label); button.setActionCommand(label); button.addActionListener(this); buttons.add(button); panel.add(button); getContentPane().add(panel);
frame.add(nPanel, BorderLayout.NORTH); Panel sPanel = new Panel(); frame.add(sPanel, BorderLayout.SOUTH); nPanel.add(tf[k]); JButton bRand = new JButton("Randomize"); bRand.setActionCommand("bRand"); bRand.addActionListener(listen); sPanel.add(bRand); JButton bMaxMin = new JButton("Max, Min"); bMaxMin.setActionCommand("bMaxMin"); // These 2 lines were refering to bRand, not to bMaxMin bMaxMin.addActionListener(listen); sPanel.add(bMaxMin); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultCloseOperation(EXIT_ON_CLOSE); btn1 = new JButton(BUTTON1_COMMAND); btn1.addActionListener(this); btn1.setActionCommand(BUTTON1_COMMAND); btn2 = new JButton(BUTTON2_COMMAND); btn2.addActionListener(this); btn2.setActionCommand(BUTTON2_COMMAND); btn3 = new JButton(BUTTON3_COMMAND); btn3.addActionListener(this); btn3.setActionCommand(BUTTON3_COMMAND); getContentPane().add(btn1); setSize(200, 100); setVisible(true); JPanel panel = new JPanel(); panel.add(btn2); pane1.showOptionDialog(null, panel, "JOptionPane #1", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null); JPanel panel = new JPanel(); panel.add(btn3); pane2.showOptionDialog(null, panel, "JOptionPane #2", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{}, null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); view_list = new JButton("View Property"); view_list.setActionCommand("view_list"); view_list.addActionListener(this); save_list = new JButton("Save List"); save_list.setActionCommand("save_list"); save_list.addActionListener(this); pane = new JPanel(); pane.setLayout(grid); pane.add(view_list); pane.add(save_list); frame.add(scrollpane); frame.add(pane, BorderLayout.NORTH); frame.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE); setPreferredSize(new Dimension(400, 90)); ((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13) ); setLayout(new FlowLayout()); JButton btn = new JButton("Change"); btn.setActionCommand("myButton"); btn.addActionListener(this); label = new JLabel("flag"); field = new JTextField(5); add(field); add(btn); add(label); pack(); setLocationRelativeTo(null); setVisible(true); setResizable(false);
panel = new JPanel(); label = new JLabel(" "); ActionListener listener = new MyActionListener(); JButton buttonA = new JButton("Press me A"); buttonA.setActionCommand("A"); buttonA.addActionListener(listener); JButton buttonB = new JButton("Press me B"); buttonB.setActionCommand("B"); buttonB.addActionListener(listener); JButton buttonC = new JButton("Press me C"); buttonC.setActionCommand("C"); buttonC.addActionListener(listener); panel.add(buttonA); panel.add(buttonB); panel.add(buttonC); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setVisible(Boolean.TRUE);
JPanel textPanel = new JPanel(); textPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); textPanel.add(desc); getContentPane().add("Center",textPanel); okButton = new JButton(okText_); okButton.addActionListener(this); okButton.setActionCommand("ok"); cancelButton = new JButton(cancelText_); cancelButton.addActionListener(this); cancelButton.setActionCommand("cancel"); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); buttonPanel.add(okButton); buttonPanel.add(cancelButton); getContentPane().add("South", buttonPanel);
setLayout(new FlowLayout()); setSize(550, 100); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton hideJCombo = new JButton("Hide my JCombobox!"); JButton showJCombo = new JButton("Show my JCombobox!"); add(hideJCombo); add(test); add(showJCombo); hideJCombo.setActionCommand("hide"); showJCombo.setActionCommand("show"); hideJCombo.addActionListener(this); showJCombo.addActionListener(this); new FrameTest().setVisible(true);
private JPanel pnl = new JPanel(); private JLabel userLabel; private JLabel passLabel; userField = new JTextField(16); passField = new JTextField(16); login = new JButton("Login"); login.setActionCommand("login"); login.setMnemonic(KeyEvent.VK_L); closeLogin = new JButton("Close"); closeLogin.setActionCommand("closeLogin"); closeLogin.setMnemonic(KeyEvent.VK_E); help = new JButton("Help"); help.setActionCommand("helpLogin"); help.setMnemonic(KeyEvent.VK_H); login.addActionListener(this); closeLogin.addActionListener(this); help.addActionListener(this); pnl.add(userLabel); pnl.add(userField); pnl.add(passLabel); pnl.add(passField); pnl.add(login);
datBoi = new JButton("dat boi"); datBoi.setActionCommand("dat boi"); datBoi.addActionListener(this); jf = new JLabel(); JPanel panel = new JPanel(); panel.add(jf); getContentPane().add(panel); add(datBoi); JPanel panel2 = new JPanel(); getContentPane().add(panel2); demo.setVisible( true );