List of usage examples for javax.swing JButton JButton
public JButton(Action a)
Action
supplied. From source file:MainClass.java
public static void main(String args[]) { JFrame f = new JFrame("JOptionPane Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Ask"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component source = (Component) actionEvent.getSource(); Object response = JOptionPane.showInputDialog(source, "Choose One?", "JOptionPane Sample", JOptionPane.QUESTION_MESSAGE, null, new String[] { "A", "B", "C" }, "B"); System.out.println("Response: " + response); }/* w ww .jav a 2 s. c o m*/ }; button.addActionListener(actionListener); f.add(button, BorderLayout.CENTER); f.setSize(300, 200); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { final JButton ok = new JButton("ok"); JCheckBox cb = new JCheckBox("Enabled", true); cb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (ok.isEnabled()) ok.setEnabled(false);/*from w w w .j av a 2s. c o m*/ else ok.setEnabled(true); } }); ButtonModel model = new DefaultButtonModel() { public void setEnabled(boolean b) { if (b) System.out.println("Pressed: true"); else System.out.println("Pressed: false"); super.setEnabled(b); } public void setArmed(boolean b) { if (b) System.out.println("Armed: true"); else System.out.println("Armed: false"); super.setArmed(b); } public void setPressed(boolean b) { if (b) System.out.println("Pressed: true"); else System.out.println("Pressed: false"); super.setPressed(b); } }; ok.setModel(model); JFrame f = new JFrame(); f.setLayout(new FlowLayout()); f.add(ok); f.add(cb); f.setSize(350, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:ActionListenerTest3.java
public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Select File"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int returnVal = fileChooser.showOpenDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { System.out.println(fileChooser.getSelectedFile().getName()); }//w w w.j a va 2 s.co m } }); frame.add(button); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton("Click me"); button.addActionListener(e -> {/*from w w w .j a v a 2 s .co m*/ JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(new File(chooser.getCurrentDirectory(), "save.dat")); final JTextField textField = getTexField(chooser); if (textField == null) { return; } String text = textField.getText(); if (text == null) { return; } int index = text.lastIndexOf('.'); if (index == -1) { return; } textField.setSelectionStart(0); textField.setSelectionEnd(index); chooser.showSaveDialog(button); }); frame.add(button); frame.pack(); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame f = new JFrame("JColorChooser Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JButton button = new JButton("Pick to Change Background"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color initialBackground = button.getBackground(); Color background = JColorChooser.showDialog(null, "JColorChooser Sample", initialBackground); if (background != null) { button.setBackground(background); }/* ww w. j a v a2s . com*/ } }; button.addActionListener(actionListener); f.add(button, BorderLayout.CENTER); f.setSize(300, 200); f.setVisible(true); }
From source file:FocusCycleSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Focus Cycle Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridLayout(3, 3)); for (int i = 0; i < 3; i++) { JButton button = new JButton("" + i); frame.add(button);/*from w w w. j av a2s.c o m*/ } JPanel panel = new JPanel(); panel.setFocusCycleRoot(true); panel.setFocusTraversalPolicyProvider(true); panel.setLayout(new GridLayout(1, 3)); for (int i = 0; i < 3; i++) { JButton button = new JButton("" + (i + 3)); panel.add(button); } frame.add(panel); for (int i = 0; i < 3; i++) { JButton button = new JButton("" + (i + 6)); frame.add(button); } frame.setSize(300, 200); frame.setVisible(true); }
From source file:SimpleGridBag.java
public static void main(String[] args) { JFrame f = new JFrame(); JPanel p = new JPanel(); p.setLayout(new GridBagLayout()); p.add(new JButton("Java")); p.add(new JButton("Source")); p.add(new JButton("and")); p.add(new JButton("Support.")); WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);/*from ww w . ja v a2 s. co m*/ } }; f.addWindowListener(wndCloser); f.getContentPane().add(p); f.setSize(600, 200); f.show(); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("BorderLayout Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container container = frame.getContentPane(); // Add a button to each of the five areas of the BorderLayout container.add(new JButton("North"), BorderLayout.NORTH); container.add(new JButton("South"), BorderLayout.SOUTH); container.add(new JButton("East"), BorderLayout.EAST); container.add(new JButton("West"), BorderLayout.WEST); container.add(new JButton("Center"), BorderLayout.CENTER); frame.pack();//from ww w . j a v a 2s .co m frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("BoxLayout with Glue"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); Box hBox = Box.createHorizontalBox(); hBox.add(new JButton("First")); hBox.add(new JButton("Previous")); hBox.add(Box.createHorizontalGlue()); hBox.add(new JButton("Next")); hBox.add(new JButton("Last")); contentPane.add(hBox, BorderLayout.SOUTH); frame.pack();//from w w w. ja v a 2s .c o m frame.setVisible(true); }
From source file:EventObject.java
public static void main(String[] args) { JFrame f = new JFrame(); JButton ok = new JButton("Ok"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(event.getWhen()); Locale locale = Locale.getDefault(); String s = DateFormat.getTimeInstance(DateFormat.SHORT, locale).format(new Date()); if (event.getID() == ActionEvent.ACTION_PERFORMED) System.out.println(" Event Id: ACTION_PERFORMED"); System.out.println(" Time: " + s); String source = event.getSource().getClass().getName(); System.out.println(" Source: " + source); int mod = event.getModifiers(); if ((mod & ActionEvent.ALT_MASK) > 0) System.out.println("Alt "); if ((mod & ActionEvent.SHIFT_MASK) > 0) System.out.println("Shift "); if ((mod & ActionEvent.META_MASK) > 0) System.out.println("Meta "); if ((mod & ActionEvent.CTRL_MASK) > 0) System.out.println("Ctrl "); }//from w w w . j a v a2 s.c o m }); f.add(ok); f.setSize(420, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }