List of usage examples for javax.swing JButton JButton
public JButton(Action a)
Action
supplied. From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Line Borders"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Border thickBorder = new LineBorder(Color.WHITE, 12); JButton thickButton = new JButton("12 Pixel"); thickButton.setBorder(thickBorder);/* w w w .ja va2 s .c o m*/ Container contentPane = frame.getContentPane(); contentPane.add(thickButton, BorderLayout.NORTH); frame.pack(); frame.setSize(300, frame.getHeight()); frame.setVisible(true); }
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Line Borders"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Border thinBorder = LineBorder.createBlackLineBorder(); JButton thinButton = new JButton("1 Pixel"); thinButton.setBorder(thinBorder);/*from w w w.ja va2s . c o m*/ Container contentPane = frame.getContentPane(); contentPane.add(thinButton, BorderLayout.NORTH); frame.pack(); frame.setSize(300, frame.getHeight()); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("SpringLayout2"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); SpringLayout springLayout = new SpringLayout(); contentPane.setLayout(springLayout); JButton b1 = new JButton("Button 1"); JButton b2 = new JButton("Button Second"); contentPane.add(b1);//from ww w.j av a2 s. co m contentPane.add(b2); springLayout.putConstraint(SpringLayout.WEST, b1, 10, SpringLayout.WEST, contentPane); springLayout.putConstraint(SpringLayout.NORTH, b1, 20, SpringLayout.NORTH, contentPane); springLayout.putConstraint(SpringLayout.WEST, b2, 10, SpringLayout.EAST, b1); springLayout.putConstraint(SpringLayout.NORTH, b2, 20, SpringLayout.NORTH, contentPane); springLayout.putConstraint(SpringLayout.SOUTH, contentPane, 10, SpringLayout.SOUTH, b1); springLayout.putConstraint(SpringLayout.EAST, contentPane, 10, SpringLayout.EAST, b2); frame.pack(); frame.setVisible(true); }
From source file:GridBagLayoutRemainder.java
public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container pane = f.getContentPane(); pane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); pane.add(new JButton("First row, first column"), gbc); pane.add(new JButton("First row, second column"), gbc); pane.add(new JButton("First row, third column"), gbc); gbc.gridx = 0;/*ww w .j a v a 2 s. c om*/ pane.add(new JButton("Second row"), gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.fill = GridBagConstraints.HORIZONTAL; pane.add(new JButton("Third row, gridwidth set to REMAINDER"), gbc); f.setSize(600, 300); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox = new JComboBox<>(new String[] { "Something", "Stuff", "Beep" }); JButton add = new JButton("Add item"); add.addActionListener(new ActionListener() { @Override//from www . java 2 s . c om public void actionPerformed(ActionEvent e) { comboBox.addItem("Item"); } }); frame.add(comboBox); frame.add(add, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); }
From source file:UIDefaultsButton.java
License:asdf
public static void main(String[] args) { UIManager.put("Button.background", Color.BLACK); UIManager.put("Button.foreground", Color.RED); JFrame aWindow = new JFrame("This is a Border Layout"); aWindow.setBounds(30, 30, 300, 300); // Size aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton bn = new JButton("asdf"); aWindow.add(bn);/*from w ww . j a v a 2 s .c om*/ aWindow.setVisible(true); // Display the window }
From source file:Main.java
public static void main(String[] a) { JDialog f = new JDialog(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);/* w w w .ja va 2s .c o m*/ } }); JButton btOK = new JButton("Press Enter to click me, I am the default."); btOK.setToolTipText("Save and exit"); f.getRootPane().setDefaultButton(btOK); JPanel p = new JPanel(); p.add(btOK); p.add(new JButton("I am NOT the default.")); f.getContentPane().add(p); f.pack(); f.setSize(new Dimension(300, 200)); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 300);/* www .j a v a 2 s.co m*/ JPanel panel = new JPanel(new GridLayout(3, 1)); JLabel label = new JLabel(); JTextField tf = new JTextField(); JButton b = new JButton("calc sting width"); b.addActionListener(e -> { FontMetrics fm = label.getFontMetrics(label.getFont()); String text = tf.getText(); int textWidth = fm.stringWidth(text); label.setText("text width for \"" + text + "\": " + textWidth); }); panel.add(label); panel.add(tf); panel.add(b); frame.setContentPane(panel); frame.setVisible(true); }
From source file:RigidArea.java
public static void main(String[] args) { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setBorder(new EmptyBorder(new Insets(40, 60, 40, 60))); panel.add(new JButton("Button")); panel.add(Box.createRigidArea(new Dimension(0, 5))); panel.add(new JButton("Button")); panel.add(Box.createRigidArea(new Dimension(0, 5))); panel.add(new JButton("Button")); panel.add(Box.createRigidArea(new Dimension(0, 5))); panel.add(new JButton("Button")); JFrame f = new JFrame(); f.add(panel);/* w ww. j a va 2 s . co m*/ f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLocationRelativeTo(null); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); final JLabel valueLabel = new JLabel(String.valueOf(value)); JButton decButton = new JButton("-"); decButton.addActionListener(e -> valueLabel.setText(String.valueOf(--value))); JButton incButton = new JButton("+"); incButton.addActionListener(e -> valueLabel.setText(String.valueOf(++value))); JPanel panel = new JPanel(); panel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1;/* w w w.j a va2s. co m*/ c.gridx = 0; c.gridy = 0; panel.add(decButton, c); c.gridx = 1; panel.add(valueLabel, c); c.gridx = 2; panel.add(incButton, c); c.gridy = 1; int w = 32; for (c.gridx = 0; c.gridx < 3; c.gridx++) { panel.add(Box.createHorizontalStrut(w), c); } frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(panel); frame.pack(); frame.setVisible(true); }