List of usage examples for java.awt Container add
public Component add(Component comp)
From source file:Main.java
public static void main(String[] args) { JRadioButton dem = new JRadioButton("Bill", false); dem.setActionCommand("Bill"); JRadioButton rep = new JRadioButton("Bob", false); rep.setActionCommand("Bob"); JRadioButton ind = new JRadioButton("Ross", false); ind.setActionCommand("Ross"); final ButtonGroup group = new ButtonGroup(); group.add(dem);/*from w w w. j av a 2s. co m*/ group.add(rep); group.add(ind); class VoteActionListener implements ActionListener { public void actionPerformed(ActionEvent ex) { String choice = group.getSelection().getActionCommand(); System.out.println("ACTION Candidate Selected: " + choice); } } class VoteItemListener implements ItemListener { public void itemStateChanged(ItemEvent ex) { String item = ((AbstractButton) ex.getItemSelectable()).getActionCommand(); boolean selected = (ex.getStateChange() == ItemEvent.SELECTED); System.out.println("ITEM Candidate Selected: " + selected + " Selection: " + item); } } ActionListener al = new VoteActionListener(); dem.addActionListener(al); rep.addActionListener(al); ind.addActionListener(al); ItemListener il = new VoteItemListener(); dem.addItemListener(il); rep.addItemListener(il); ind.addItemListener(il); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = frame.getContentPane(); c.setLayout(new GridLayout(4, 1)); c.add(new JLabel("Please Cast Your Vote")); c.add(dem); c.add(rep); c.add(ind); frame.pack(); frame.setVisible(true); }
From source file:YAxisDiffAlign.java
public static void main(String args[]) { JFrame frame = new JFrame("Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container panel1 = makeIt("Mixed", false); Container panel2 = makeIt("Mixed", true); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(1, 2)); contentPane.add(panel1); contentPane.add(panel2);//from w ww. j av a 2s. co m frame.setSize(300, 200); frame.setVisible(true); }
From source file:TitledPostBorder.java
public static void main(String args[]) { JFrame frame = new JFrame("Positioned Titled Borders"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TitledBorder aboveTopBorder = BorderFactory.createTitledBorder("AboveTop"); aboveTopBorder.setTitlePosition(TitledBorder.ABOVE_TOP); JButton aboveTopButton = new JButton(); aboveTopButton.setBorder(aboveTopBorder); TitledBorder topBorder = BorderFactory.createTitledBorder("Top"); topBorder.setTitlePosition(TitledBorder.TOP); JButton topButton = new JButton(); topButton.setBorder(topBorder);//w w w .j a va 2 s . c o m TitledBorder belowTopBorder = BorderFactory.createTitledBorder("BelowTop"); belowTopBorder.setTitlePosition(TitledBorder.BELOW_TOP); JButton belowTopButton = new JButton(); belowTopButton.setBorder(belowTopBorder); TitledBorder aboveBottomBorder = BorderFactory.createTitledBorder("AboveBottom"); aboveBottomBorder.setTitlePosition(TitledBorder.ABOVE_BOTTOM); JButton aboveBottomButton = new JButton(); aboveBottomButton.setBorder(aboveBottomBorder); TitledBorder bottomBorder = BorderFactory.createTitledBorder("Bottom"); bottomBorder.setTitlePosition(TitledBorder.BOTTOM); JButton bottomButton = new JButton(); bottomButton.setBorder(bottomBorder); TitledBorder belowBottomBorder = BorderFactory.createTitledBorder("BelowBottom"); belowBottomBorder.setTitlePosition(TitledBorder.BELOW_BOTTOM); JButton belowBottomButton = new JButton(); belowBottomButton.setBorder(belowBottomBorder); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(3, 2)); contentPane.add(aboveTopButton); contentPane.add(aboveBottomButton); contentPane.add(topButton); contentPane.add(bottomButton); contentPane.add(belowTopButton); contentPane.add(belowBottomButton); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { String title = (args.length == 0 ? "TextArea Example" : args[0]); JFrame frame = new JFrame(title); Container content = frame.getContentPane(); content.setLayout(new GridLayout(0, 2)); JTextArea leftTextArea = new JTextArea(); content.add(leftTextArea); leftTextArea.paste();/*from w w w .ja va 2 s.c om*/ JTextArea rightTextArea = new JTextArea() { public boolean isManagingFocus() { return false; } }; rightTextArea.paste(); JScrollPane rightPane = new JScrollPane(rightTextArea); content.add(rightPane); frame.setSize(250, 150); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(null);/*from w w w. j a va2 s. co m*/ JButton b1 = new JButton("Button"); JButton b2 = new JButton("2"); contentPane.add(b1); contentPane.add(b2); b1.setBounds(10, 10, 100, 20); b2.setBounds(120, 10, 150, 40); frame.setBounds(0, 0, 350, 100); frame.setVisible(true); }
From source file:FlowLayoutChangingGap.java
public static void main(String[] args) { JFrame aWindow = new JFrame("This is a Flow Layout"); aWindow.setBounds(50, 50, 500, 500); aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 20, 30); Container content = aWindow.getContentPane(); // Get the content pane content.setLayout(flow); // Set the container layout mgr for (int i = 1; i <= 6; i++) { content.add(new JButton("Press " + i)); // Add a Button to content pane }/*from www. ja va2s . c om*/ aWindow.setVisible(true); // Display the window }
From source file:KeyStrokeSample.java
public static void main(String args[]) { JFrame frame = new JFrame("KeyStroke Sample"); JButton buttonA = new JButton("<html><center>FOCUSED<br>control alt 7"); JButton buttonB = new JButton("<html><center>FOCUS/RELEASE<br>VK_ENTER"); JButton buttonC = new JButton("<html><center>ANCESTOR<br>VK_F4+SHIFT_MASK"); JButton buttonD = new JButton("<html><center>WINDOW<br>' '"); // Define ActionListener Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JButton source = (JButton) actionEvent.getSource(); System.out.println("Activated: " + source.getText()); }/*from w w w .j a v a 2 s . co m*/ }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); actionMap.put(ACTION_KEY, actionListener); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true); inputMap = buttonB.getInputMap(); inputMap.put(enter, ACTION_KEY); buttonB.setActionMap(actionMap); KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK); inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(shiftF4, ACTION_KEY); buttonC.setActionMap(actionMap); KeyStroke space = KeyStroke.getKeyStroke(' '); inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(space, ACTION_KEY); buttonD.setActionMap(actionMap); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(2, 2)); contentPane.add(buttonA); contentPane.add(buttonB); contentPane.add(buttonC); contentPane.add(buttonD); frame.setSize(400, 200); frame.setVisible(true); }
From source file:IconMatteBorder.java
public static void main(String args[]) { JFrame frame = new JFrame("Icon Matted Borders"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Icon diamondIcon = new DiamondIcon(Color.red, true, 10, 10); Border diamondBorder = new MatteBorder(10, 10, 10, 10, diamondIcon); JButton diamondButton = new JButton("diamonds"); diamondButton.setBorder(diamondBorder); Icon diamondIcon2 = new ImageIcon("diamond.gif"); Border diamondBorder2 = new MatteBorder(36, 40, 36, 40, diamondIcon2); JButton diamondButton2 = new JButton("diamonds2"); diamondButton2.setBorder(diamondBorder2); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(2, 1)); contentPane.add(diamondButton); contentPane.add(diamondButton2);// ww w .ja va 2s . c o m frame.setSize(300, 200); frame.setVisible(true); }
From source file:SimpleButtonGroupExample.java
public static void main(String[] args) { // Some choices JRadioButton choice1, choice2, choice3; choice1 = new JRadioButton("Bach: Well Tempered Clavier, Book I"); choice1.setActionCommand("bach1"); choice2 = new JRadioButton("Bach: Well Tempered Clavier, Book II"); choice2.setActionCommand("bach2"); choice3 = new JRadioButton("Shostakovich: 24 Preludes and Fugues"); choice3.setActionCommand("shostakovich"); // A group, to ensure that we only vote for one. final ButtonGroup group = new ButtonGroup(); group.add(choice1);/* w w w . j a v a 2 s . c o m*/ group.add(choice2); group.add(choice3); // A simple ActionListener, showing each selection using the ButtonModel class VoteActionListener implements ActionListener { public void actionPerformed(ActionEvent ev) { String choice = group.getSelection().getActionCommand(); System.out.println("ACTION Choice Selected: " + choice); } } // A simple ItemListener, showing each selection and deselection class VoteItemListener implements ItemListener { public void itemStateChanged(ItemEvent ev) { boolean selected = (ev.getStateChange() == ItemEvent.SELECTED); AbstractButton button = (AbstractButton) ev.getItemSelectable(); System.out .println("ITEM Choice Selected: " + selected + ", Selection: " + button.getActionCommand()); } } // Add listeners to each button ActionListener alisten = new VoteActionListener(); choice1.addActionListener(alisten); choice2.addActionListener(alisten); choice3.addActionListener(alisten); ItemListener ilisten = new VoteItemListener(); choice1.addItemListener(ilisten); choice2.addItemListener(ilisten); choice3.addItemListener(ilisten); // Throw everything together JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = frame.getContentPane(); c.setLayout(new GridLayout(0, 1)); c.add(new JLabel("Vote for your favorite prelude & fugue cycle")); c.add(choice1); c.add(choice2); c.add(choice3); frame.pack(); frame.setVisible(true); }
From source file:ImageLabelExample.java
public static void main(String[] args) { JLabel[] labels = new JLabel[9]; labels[0] = makeLabel(JLabel.TOP, JLabel.LEFT); labels[1] = makeLabel(JLabel.TOP, JLabel.CENTER); labels[2] = makeLabel(JLabel.TOP, JLabel.RIGHT); labels[3] = makeLabel(JLabel.CENTER, JLabel.LEFT); labels[4] = makeLabel(JLabel.CENTER, JLabel.CENTER); labels[5] = makeLabel(JLabel.CENTER, JLabel.RIGHT); labels[6] = makeLabel(JLabel.BOTTOM, JLabel.LEFT); labels[7] = makeLabel(JLabel.BOTTOM, JLabel.CENTER); labels[8] = makeLabel(JLabel.BOTTOM, JLabel.RIGHT); labels[0].setEnabled(false);// w w w. jav a 2 s. co m labels[1].setDisabledIcon(new ImageIcon("2.gif")); labels[1].setEnabled(false); labels[2].setIconTextGap(15); labels[3].setIconTextGap(0); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = frame.getContentPane(); c.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3)); for (int i = 0; i < 9; i++) c.add(labels[i]); frame.setSize(350, 150); frame.setVisible(true); }