private JButton tip1Null = new JButton(" test button "); tip1Null.setForeground(Color.darkGray); tip1Null.setPreferredSize(new Dimension(50, 30)); tip1Null.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(ActionEvent e) { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tip1Null, BorderLayout.CENTER); frame.setLocation(150, 150); frame.setPreferredSize(new Dimension(310, 75)); frame.setLocationRelativeTo(null); frame.pack(); frame.setVisible(true); paintText(g, b, b.getBounds(), b.getText()); g.setColor(Color.red.brighter()); g.fillRect(0, 0, b.getSize().width, b.getSize().height);
addTextAction(svwnb); addTextAction(blypb); addTextAction(pw91b); addTextAction(b97db); addTextAction(pbepbesolb); ... private void addTextAction(final AbstractButton button) { button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { input[5] = button.getText(); dftframe.setVisible(false); } }); }
final JButton b1 = new JButton("Yes, please"); b1.setName("yes_please"); final JButton b2 = new JButton("No, thx"); b2.setName("no_thx"); final JButton b3 = new JButton("Leave me alone"); b3.setName("leave_me_alone"); Object[] options = new Object[]{b1, b2, b3}; b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(b1.getText()); optionPane.setValue(0); dialog.dispose(); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(b2.getText()); optionPane.setValue(1); dialog.dispose(); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(b3.getText()); optionPane.setValue(2); dialog.dispose();
for (Season s : Season.values()) { group.add(s.button); radioPanel.add(s.button); s.button.addActionListener(this); this.add(radioPanel); this.pack(); this.setLocationRelativeTo(frame); public void actionPerformed(ActionEvent e) { JRadioButton b = (JRadioButton) e.getSource(); JOptionPane.showMessageDialog(null, "You chose: " + b.getText());
this.add(new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0)); this.add(new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA)); this.add(new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0)); this.add(new MoveButton("\u2193", KeyEvent.VK_DOWN, 0, DELTA)); this.dx = dx; this.dy = dy; this.setAction(new AbstractAction(this.getText()) { f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.add(new ControlPanel(), BorderLayout.SOUTH); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
Random r = new Random(); for (int i = 0, length = arr.length; i < length; i ++) { if (arr[i].getText().isEmpty()) { isFound = false; if (b.getText().isEmpty()) { isFound = true; System.out.println("Button Name ::: " + b.getName()); System.out.println("Button text before replace ::: " + b.getText()); b.setText(text); System.out.println("Button text after replace ::: " + b.getText()); b.setEnabled(false); JButton button1 = new JButton("Empty"); button1.setName("button1"); JButton button2 = new JButton(); button2.setName("button2"); JButton button3 = new JButton(); button3.setName("button3");
jMenus.get(i).addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(jMenus.get(index).getText());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setLayout(new BorderLayout()); display.setHorizontalAlignment(JLabel.RIGHT); display.setText("0"); window.add(display, BorderLayout.NORTH); int[] digitOrder = new int[] { 7,8,9,4,5,6,1,2,3,0 }; for (int digit : digitOrder) { JButton button = new JButton(Integer.toString(digit)); button.addActionListener(this); digitsPanel.add(button); window.add(digitsPanel, BorderLayout.CENTER); String[] operators = new String[] { "+","-","x","/","=" }; for (String operator : operators) { JButton button = new JButton(operator); button.addActionListener(this); operatorsPanel.add(button); window.setVisible(true); public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { String buttonText = ((JButton)e.getSource()).getText(); if (Character.isDigit(buttonText.charAt(0))) { digitsEntered.append(buttonText.charAt(0));
import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.Color; import java.awt.event.ActionEvent; public class FactButton extends JButton implements ActionListener{ private static final long serialVersionUID = 1L; public FactButton() { this.addActionListener(this); } public void actionPerformed(ActionEvent e){ String prob = this.getText(); int i = 0,j = 0; if(!prob.matches("-?\\d+")){//it was on the "x*y" form String left = prob.substring(0, prob.indexOf(" ")); String right = prob.substring(prob.indexOf(" ")+3, prob.length()); i = Integer.parseInt(left); j = Integer.parseInt(right); this.setText(i*j+""); this.setBackground(Color.WHITE); this.setContentAreaFilled(false); this.setOpaque(true); } else {//so it was on the form of a number this.setText(Grid.getOriginalFromButton(this)); //any other changes like color } } }
JCheckBox checkBox = new JCheckBox(columnNames[i]); checkBox.setSelected(true); checkBox.addActionListener(new ActionListener() { String columnName = cb.getText(); checkBoxes.add(checkBox); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(scrollPane); frame.getContentPane().add(checkBoxes, BorderLayout.SOUTH); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
private JButton newGameButton = new JButton("New Game"); private JButton[] numberedButtons = new JButton[3]; private JPanel southPanel = new JPanel(); this.game = game; newGameButton.addActionListener(this); numberedButtons[i] = new JButton((new Integer(i+1)).toString()); numberedButtons[i].addActionListener(this); southPanel.add(numberedButtons[i]); getContentPane().add(newGameButton, BorderLayout.NORTH); getContentPane().add(southPanel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); JButton pressedButton = (JButton) event.getSource(); if (pressedButton.getText() == "New Game") { game.startNewGame(); Game.checkThreads();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); JScrollPane scrollPane = new JScrollPane(txtDetails, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); btnCopy = new JButton("Copy"); btnCopy.setFocusPainted(false); btnCopy.setEnabled(false); btnCopy.addActionListener(this); JPanel panProgressBars = new JPanel(new BorderLayout(0, 5)); panInputLabels.add(lblSource, BorderLayout.NORTH); panInputLabels.add(lblTarget, BorderLayout.CENTER); panInputFields.add(txtSource, BorderLayout.NORTH); panInputFields.add(txtTarget, BorderLayout.CENTER); panProgressLabels.add(lblProgressAll, BorderLayout.NORTH); public void actionPerformed(ActionEvent e) if("Copy".equals(btnCopy.getText())) else if("Cancel".equals(btnCopy.getText())) public void run() new FileCopierUtility().setVisible(true);
ui.add(text, BorderLayout.PAGE_START); io = new JTextField(15); Font font = io.getFont(); io.setHorizontalAlignment(SwingConstants.TRAILING); io.setFocusable(false); text.add(io); ui.add(buttons, BorderLayout.CENTER); String[] keyValues = { "7", "8", "9", "/", JButton equals = new JButton("="); JButton b = new JButton(text); Font f = b.getFont(); b.setFont(f.deriveFont(f.getSize() * 1.5f)); b.addActionListener(this); b.addKeyListener(this); controls.add(b); String s = button.getText(); if (text.endsWith(s) || (s.equals("=")
JCheckBox checkBox = new JCheckBox(columnNames[i]); checkBox.setSelected(true); checkBox.addActionListener(new ActionListener() { String columnName = cb.getText(); checkBoxes.add(checkBox); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(scrollPane); frame.getContentPane().add(checkBoxes, BorderLayout.SOUTH); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
checkBoxes[i].addItemListener(this); enabledFlags[i] = true; pnlEnablers.add(checkBoxes[i]); contentPane.add(pnlEnablers); contentPane.add(scroll); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocation(240, 280); UIManager.put("List.background", Color.lightGray); f.setVisible(true); int index = -1; for (int i = 0; i < ITEMS.length; i++) { if (ITEMS[i].equals(checkBox.getText())) { index = i; break;
private JButton french = new JButton(); private JButton german = new JButton(); private JButton irish = new JButton(); this.add(french); this.add(german); this.add(irish); ActionListener ac = (ActionEvent ae) -> { System.out.println(((JButton) ae.getSource()).getText()); }; this.french.addActionListener(ac); this.german.addActionListener(ac); this.irish.addActionListener(ac); this.irish.addActionListener(Translator.this); System.out.println(((JButton) e.getSource()).getText()); jframe.pack(); jframe.setLocationRelativeTo(null); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setVisible(true);
public static void main(String args[]) { JFrame frame = new JFrame("Custom Layout Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton but1 = new JButton("Button1"); JButton but2 = new JButton("Button2"); JButton but3 = new JButton("Button3"); cur.addActionListener(bh); frame.add(but1); frame.add(but2); frame.add(but3); frame.setVisible(true); @Override public void actionPerformed(ActionEvent arg0) { System.out.println(((JButton)(arg0.getSource())).getText() + " Clicked !");
getContentPane().setLayout(null); btnHola = new JButton("Hello"); btnHola.setBounds(63, 210, 89, 23); getContentPane().add(btnHola); btnHola.addActionListener(new ActionListener() { mensaje = Frame1.this.btnHola.getText(); Frame2 frame2 = new Frame2(mensaje); btnAdios = new JButton("Bye"); btnAdios.setBounds(245, 210, 89, 23); getContentPane().add(btnAdios); btnAdios.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { mensaje = Frame1.this.btnAdios.getText(); Frame2 frame2 = new Frame2(mensaje); frame.setVisible(true);
new JButton("1"), new JButton("2"), new JButton("3"), new JButton("4"), new JButton("5"), super("Calculator"); setSize(250, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); opPad.setLayout(new GridLayout(4, 1)); getContentPane().setLayout(new BorderLayout()); getContentPane().add(numPad, BorderLayout.LINE_START); getContentPane().add(displayPanel, BorderLayout.PAGE_START); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { displayPanel.setText(displayPanel.getText() + button.getText()); numPad.add(button);