import java.awt.*; import javax.swing.event.*; import javax.swing.*; import window.JWindowFrame; import java.awt.event.*; public class AssignmentOne extends JWindowFrame { private Container c; private ActionHandler ah; private JTextArea textDisplay; private JCheckBox bold; private JCheckBox italic; private JComboBox fontSizeCombo; private JComboBox fontFaceCombo; private MenuBar menuBar; private Menu fileMenu, formatMenu; private MenuItem exitMenuItem, colourMenuItem; private Container dlg_c; private JPanel colorPanel; private String colorNames[] = { "Red: ", "Green: ", "Blue: " }; private Label labels[] = new Label[3]; private JSlider slider[] = new JSlider[3]; private JButton saveColor; private JWindowFrame dlg; public AssignmentOne() { c = getContentPane(); ah = new ActionHandler(); GridBagLayout gbLayout = new GridBagLayout(); c.setLayout(gbLayout); JLabel faceLabel = new JLabel("Font Face: "); String faceComboLabel[] = { "Serif", "SansSerif", "Monospaced" }; fontFaceCombo = new JComboBox(faceComboLabel); JLabel sizeLabel = new JLabel("Font Size: "); String sizeComboLabel[] = { "8", "10", "12", "15", "18", "24", "36", "48" }; fontSizeCombo = new JComboBox(sizeComboLabel); bold = new JCheckBox("Bold"); italic = new JCheckBox("Italic"); textDisplay = new JTextArea("The quick brown fox jumps over the lazy dog"); textDisplay.setEditable(true); textDisplay.setLineWrap(true); menuBar = new MenuBar(); fileMenu = new Menu("File"); formatMenu = new Menu("Format"); fileMenu.add(exitMenuItem = new MenuItem("Exit")); exitMenuItem.addActionListener(ah); formatMenu.add(colourMenuItem = new MenuItem("Set Colour")); colourMenuItem.addActionListener(ah); menuBar.add(fileMenu); menuBar.add(formatMenu); setMenuBar(menuBar); fontFaceCombo.addActionListener(ah); fontSizeCombo.addActionListener(ah); bold.addActionListener(ah); italic.addActionListener(ah); // add components to grid GridBagConstraints constraints = new GridBagConstraints(); // do not fill up the entire area constraints.fill = GridBagConstraints.NONE; // position permanently in the east side constraints.anchor = GridBagConstraints.EAST; // the labels remain a constant width when the window is resize constraints.weightx = 0; constraints.weighty = 0; addConstraints(faceLabel, constraints, 0, 0, 1, 1); addConstraints(sizeLabel, constraints, 0, 1, 1, 1); // combo box to expand horizontal in the grid constraints.fill = GridBagConstraints.HORIZONTAL; constraints.weightx = 0; constraints.weighty = 0; addConstraints(fontFaceCombo, constraints, 1, 0, 1, 1); addConstraints(fontSizeCombo, constraints, 1, 1, 1, 1); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.CENTER; constraints.weightx = 0; constraints.weighty = 100; addConstraints(bold, constraints, 0, 2, 2, 1); addConstraints(italic, constraints, 0, 3, 2, 1); constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 100; constraints.weighty = 0; addConstraints(textDisplay, constraints, 2, 0, 1, 4); setTitle("GridBagLayout Demo"); setSize(400, 300); setVisible(true); } public void addConstraints(Component comp, GridBagConstraints constraints, int x, int y, int w, int h) { constraints.gridx = x; constraints.gridy = y; constraints.gridwidth = w; constraints.gridheight = h; c.add(comp, constraints); } public static void main(String args[]) { new AssignmentOne(); } public class ActionHandler implements ActionListener, ChangeListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == exitMenuItem) { System.exit(0); } else if (e.getSource() == colourMenuItem) { if (dlg == null) { dlg = new JWindowFrame(); dlg_c = dlg.getContentPane(); JPanel sliderPanel = new JPanel(); sliderPanel.setLayout(new GridLayout(0, 1)); for (int i = 0; i < 3; i++) { labels[i] = new Label(colorNames[i] + 255); sliderPanel.add(labels[i]); slider[i] = new JSlider(SwingConstants.HORIZONTAL, 0, 255, 255); slider[i].setMinorTickSpacing(10); slider[i].setMajorTickSpacing(50); slider[i].setPaintTicks(true); slider[i].setPaintLabels(true); sliderPanel.add(slider[i]); slider[i].addChangeListener(this); } dlg_c.add(sliderPanel, BorderLayout.NORTH); colorPanel = new JPanel(); colorPanel.setBackground(textDisplay.getForeground()); dlg_c.add(colorPanel, BorderLayout.CENTER); saveColor = new JButton("Accept"); saveColor.addActionListener(ah); dlg_c.add(saveColor, BorderLayout.SOUTH); dlg.setSize(500, 450); dlg.setTitle("Slider Demo"); } dlg.setVisible(true); } else if (e.getSource() == saveColor) { textDisplay.setForeground(new Color(slider[0].getValue(), slider[1].getValue(), slider[2].getValue())); dlg.setVisible(false); } else { int fontStyle = Font.PLAIN; String fontFace = fontFaceCombo.getSelectedItem().toString(); int fontSize = Integer.parseInt(fontSizeCombo.getSelectedItem().toString()); if (bold.isSelected()) fontStyle += Font.BOLD; if (italic.isSelected()) fontStyle += Font.ITALIC; textDisplay.setFont(new Font(fontFace, fontStyle, fontSize)); } } public void stateChanged(ChangeEvent e) { for (int i = 0; i < 3; i++) // slider component getValue method returns the slider value labels[i].setText(colorNames[i] + slider[i].getValue()); // setting the colorPanel background using the RGB value colorPanel.setBackground(new Color(slider[0].getValue(), slider[1].getValue(), slider[2].getValue())); } } }