Description: Create a gui application that provides the following functionality:
ID: 3856710 • Letter: D
Question
Description:
Create a gui application that provides the following functionality:
It has a control panel on the left side
The control panel consists of one submit button and 3 check boxes that correspond to three features (eyes, nose, and mouth)
All the features, whose check boxes are selected, change when the submit button is clicked
If none of the check boxes is selected the face itself changes
It has a face on the right side, that changes depending on the check box selection
Make sure that all the image files are part of the project and that they are accessed by using the resources of the class.
6 Face You choose. Eyes Nose D Mouth update 00:01 01 31 1.00xExplanation / Answer
import java.awt.*;
import javax.swing.*;
public class TestDisplayAreaAndBorder extends JFrame {
public TestDisplayAreaAndBorder() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
JTextArea comp = new JTextArea(10, 25);
comp.setBackground(new Color(200, 200, 200));
comp.setForeground(Color.BLUE);
comp.setBorder(BorderFactory.createLineBorder(Color.CYAN, 15));
comp.setPreferredSize(new Dimension(350, 200));
cp.add(comp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Appearance and Border");
setSize(400, 300);
setVisible(true);
StringBuffer msg = new StringBuffer();
msg.append("Width = " + comp.getWidth());
msg.append(" Height = " + comp.getHeight());
msg.append(" Origin-X = " + comp.getX());
msg.append(" Origin-Y = " + comp.getY());
msg.append(" Origin-X (on screen) = " + comp.getLocationOnScreen().x);
msg.append(" Origin-Y (on screen) = " + comp.getLocationOnScreen().y);
Insets insets = comp.getInsets();
msg.append(" Insets (top, right, bottom, left) = "
+ insets.top + "," + insets.right + "," + insets.bottom + "," + insets.left);
msg.append(" Real Width = " + (comp.getWidth() - insets.left - insets.right));
msg.append(" Real Height = " + (comp.getHeight() - insets.top - insets.bottom));
comp.setText(msg.toString());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestDisplayAreaAndBorder();
}
});
}
}
String imgFilename = "images/duke.png";
ImageIcon iconDuke = new ImageIcon(imgFilename);
// OR
ImageIcon iconDuke = null;
String imgFilename = "images/duke.png";
java.net.URL imgURL = getClass().getClassLoader().getResource(imgFilename);
if (imgURL != null) {
iconDuke = new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + imgFilename);
}
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
@SuppressWarnings("serial")
public class SwingJComponentSetterTest extends JFrame {
private String imgCrossFilename = "images/cross.gif";
private String imgNoughtFilename = "images/nought.gif";
public SwingJComponentSetterTest() {
ImageIcon iconCross = null;
ImageIcon iconNought = null;
URL imgURL = getClass().getClassLoader().getResource(imgCrossFilename);
if (imgURL != null) {
iconCross = new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + imgCrossFilename);
}
imgURL = getClass().getClassLoader().getResource(imgNoughtFilename);
if (imgURL != null) {
iconNought = new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + imgNoughtFilename);
}
Container cp = getContentPane();
cp.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
JLabel label = new JLabel("JLabel", iconCross, SwingConstants.CENTER);
label.setFont(new Font(Font.DIALOG, Font.ITALIC, 14));
label.setOpaque(true);
label.setBackground(new Color(204, 238, 241));
label.setForeground(Color.RED);
label.setPreferredSize(new Dimension(120, 80));
label.setToolTipText("This is a JLabel");
cp.add(label);
JButton button = new JButton();
button.setText("Button");
button.setIcon(iconNought);
button.setVerticalAlignment(SwingConstants.BOTTOM);
button.setHorizontalAlignment(SwingConstants.RIGHT);
button.setHorizontalTextPosition(SwingConstants.LEFT);
button.setVerticalTextPosition(SwingConstants.TOP);
button.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 15));
button.setBackground(new Color(231, 240, 248));
button.setForeground(Color.BLUE);
button.setPreferredSize(new Dimension(150, 80));
button.setToolTipText("This is a JButton");
button.setMnemonic(KeyEvent.VK_B);
cp.add(button);
JTextField textField = new JTextField("Text Field", 15);
textField.setFont(new Font(Font.DIALOG_INPUT, Font.PLAIN, 12));
textField.setForeground(Color.RED);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setToolTipText("This is a JTextField");
cp.add(textField);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JComponent Test");
setLocationRelativeTo(null);
setSize(500, 150);
setVisible(true);
System.out.println(label);
System.out.println(button);
System.out.println(textField);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SwingJComponentSetterTest();
}
});
}
}
javax.swing.JLabel[, 41, 10, 120x80, alignmentX=0.0, alignmentY=0.0, border=, flags=25165832,
maximumSize=, minimumSize=, preferredSize=java.awt.Dimension[width=120,height=80],
defaultIcon=file:.../cross.gif, disabledIcon=,
horizontalAlignment=CENTER, horizontalTextPosition=TRAILING,
iconTextGap=4, labelFor=, text=JLabel, verticalAlignment=CENTER, verticalTextPosition=CENTER]
javax.swing.JButton[, 171, 10, 150x80, alignmentX=0.0, alignmentY=0.5,
border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@c5e2cf, flags=424,
maximumSize=, minimumSize=, preferredSize=java.awt.Dimension[width=150,height=80],
defaultIcon=file:/.../nought.gif, disabledIcon=, disabledSelectedIcon=,
margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],
paintBorder=true, paintFocus=true, pressedIcon=, rolloverEnabled=true, rolloverIcon=,
rolloverSelectedIcon=, selectedIcon=, text=Button, defaultCapable=true]
javax.swing.JTextField[, 331, 39, 109x21, layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,
alignmentX=0.0, alignmentY=0.0, border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1991de1,
flags=296, maximumSize=, minimumSize=, preferredSize=,
caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],
disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229], editable=true,
margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],
selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],
selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],
columns=15, columnWidth=7, command=, horizontalAlignment=RIGHT]
Example
import java.awt.*;
import javax.swing.*;
public class TestSize {
public static void main(String[] args) {
JFrame frame = new JFrame("Display Area");
Container cp = frame.getContentPane();
cp.setLayout(new FlowLayout());
JButton btnHello = new JButton("Hello");
btnHello.setPreferredSize(new Dimension(100, 80));
cp.add(btnHello);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 150); // or pack() the components
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println(btnHello.getSize());
System.out.println(btnHello.getLocation());
System.out.println(btnHello.getLocationOnScreen());
System.out.println(cp.getSize());
System.out.println(cp.getLocation());
System.out.println(cp.getLocationOnScreen());
System.out.println(frame.getSize());
System.out.println(frame.getLocation());
System.out.println(frame.getLocationOnScreen());
}
}
java.awt.Dimension[width=100,height=80]
java.awt.Point[x=91,y=5]
java.awt.Point[x=590,y=349]
getLocationOnScreen()
java.awt.Dimension[width=282,height=105]
java.awt.Point[x=0,y=0]
java.awt.Point[x=499,y=344]
java.awt.Dimension[width=300,height=150]
java.awt.Point[x=490,y=308]
java.awt.Point[x=490,y=308]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextComponentDemo extends JFrame {
// Private variables of the GUI components
JTextField tField;
JPasswordField pwField;
JTextArea tArea;
JFormattedTextField formattedField;
public JTextComponentDemo() {
JPanel tfPanel = new JPanel(new GridLayout(3, 2, 10, 2));
tfPanel.setBorder(BorderFactory.createTitledBorder("Text Fields: "));
tfPanel.add(new JLabel(" JTextField: "));
tField = new JTextField(10);
tfPanel.add(tField);
tField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tArea.append(" You have typed " + tField.getText());
}
});
tfPanel.add(new JLabel(" JPasswordField: "));
pwField = new JPasswordField(10);
tfPanel.add(pwField);
pwField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tArea.append(" You password is " + new String(pwField.getPassword()));
}
});
// Formatted text field (Row 3)
tfPanel.add(new JLabel(" JFormattedTextField"));
formattedField = new JFormattedTextField(java.util.Calendar
.getInstance().getTime());
tfPanel.add(formattedField);
tArea = new JTextArea("A JTextArea is a "plain" editable text component, "
+ "which means that although it can display text "
+ "in any font, all of the text is in the same font.");
tArea.setFont(new Font("Serif", Font.ITALIC, 13));
tArea.setLineWrap(true);
tArea.setWrapStyleWord(true);
tArea.setBackground(new Color(204, 238, 241));
JScrollPane tAreaScrollPane = new JScrollPane(tArea);
tAreaScrollPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
tAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Container cp = this.getContentPane();
cp.setLayout(new BorderLayout(5, 5));
cp.add(tfPanel, BorderLayout.NORTH);
cp.add(tAreaScrollPane, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JTextComponent Demo");
setSize(350, 350);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JTextComponentDemo();
}
});
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BorderLayoutTest extends JFrame {
public static final String TITLE = "BorderLayout Demo";
private Container cp;
private JButton btnNorth, btnSouth, btnCenter, btnEast, btnWest;
private boolean leftToRight = true;
public BorderLayoutTest() {
cp = this.getContentPane();
btnNorth = new JButton("PAGE_START [HIDE]");
btnSouth = new JButton("PAGE_END [HIDE]");
btnWest = new JButton("LINE_START [HIDE]");
btnEast = new JButton("LINE_END [HIDE]");
btnCenter = new JButton("CENTER [SHOW ALL, CHANGE ORIENTATION]");
btnCenter.setPreferredSize(new Dimension(300, 100));
ActionListener listener = new ButtonListener();
btnNorth.addActionListener(listener);
btnSouth.addActionListener(listener);
btnEast.addActionListener(listener);
btnWest.addActionListener(listener);
btnCenter.addActionListener(listener);
addButtons();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle(TITLE);
pack(); // "this" JFrame packs all its components
setLocationRelativeTo(null);
setVisible(true);
}
private void addButtons() {
cp.removeAll();
cp.setComponentOrientation(leftToRight ?
ComponentOrientation.LEFT_TO_RIGHT : ComponentOrientation.RIGHT_TO_LEFT);
cp.add(btnNorth, BorderLayout.PAGE_START);
cp.add(btnSouth, BorderLayout.PAGE_END);
cp.add(btnWest, BorderLayout.LINE_START);
cp.add(btnEast, BorderLayout.LINE_END);
cp.add(btnCenter, BorderLayout.CENTER);
cp.validate();
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JButton source = (JButton)evt.getSource();
if (source == btnCenter) {
leftToRight = !leftToRight;
addButtons();
} else {
cp.remove(source);
cp.validate();
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BorderLayoutTest();
}
});
}
}
3.5 Absolute Positioning Without a Layout Manager
Graphics_AbsolutePositioning.png
You could use absolute position instead of a layout manager (such as FlowLayout or BorderLayout) by invoking method setLayout(null). You can then position you components using the method setBounds(int xTopLeft, int yTopLeft, int width, int height). For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.awt.*;
import javax.swing.*;
public class CGAbsolutePositioning extends JFrame {
public CGAbsolutePositioning() {
Container cp = getContentPane();
cp.setLayout(null);
JPanel p1 = new JPanel();
p1.setBounds(30, 30, 100, 100);
p1.setBackground(Color.RED);
cp.add(p1);
JPanel p2 = new JPanel();
p2.setBounds(150, 50, 120, 80);
p2.setBackground(Color.BLUE);
cp.add(p2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Absolute Positioning Demo");
setSize(400, 200);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new CGAbsolutePositioning();
}
}
}
}