Question
Write a class StringList.java.
It has the following constructor.
new StringList(String[] strings)
It creates a list of buttons with text strings[0], string[1],....
and the buttons are laid down horizontally.
Here is an example, we create 3 StringLists.
StringListTest.java
You can get the program here.
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class StringListTest {
5: public static void main(String[] args) {
6: JFrame f = new JFrame();
7: int col = 1, vgap = 5, hgap = 5;
8: f.getContentPane().setLayout(new GridLayout(0,col,hgap,vgap));
9:
10: String[] stringArray1 = {"zero", "one", "two", "three"};
11: StringList list1 = new StringList(stringArray1);
12: f.getContentPane().add(list1);
13:
14: String[] stringArray2 = {"A", "B", "C"};
15: StringList list2 = new StringList(stringArray2);
16: f.getContentPane().add(list2);
17:
18: String[] stringArray3 = {"1","2","3","4","5"};
19: StringList list3 = new StringList(stringArray3);
20: f.getContentPane().add(list3);
21:
22: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23: f.pack();
24: f.setVisible(true);
25: }
26:}
Notice how we create StringList at line 11,15,19.
Answer : StringList.java
You can get the program here.
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class StringList extends JPanel {
5: private String[] stringArray;
6: private JButton[] buttons;
7: public StringList(String[] stringArray) {
8: this.stringArray = stringArray;
9: buttons = new JButton[stringArray.length];
10: setLayout(new GridLayout(1,0)); // setLayout is a method from JPanel
11: // set it equal to one row
12: for(int i = 0; i < stringArray.length; i++) {
13: buttons[i] = new JButton(stringArray[i]);
14: add(buttons[i]); // add buttons[i], a method from JPanel
15: }
16: }
17:}
Explanation :
- At line 4 we extends
JPanel and add buttons to it.
So StringList is a subclass of JPanel.
- Line 7 - 17 is the constructor body.
- Line 9 instantiate a button array.
- We set the layout at line 10.
Unlike
JFrame, we don't use getContentPane() here.
- line 12 - 15 : instantiate buttons and add to the
JPanel.
Unlike JFrame, we don't add by contentPane.
Question
Create a class StringList2, which is very similar to
StringList but we add an orientation.
If orientation is HORIZONTAL, then the buttons will be
on a horizontal row.
If orientation is VERTICAL, then the buttons will be
on a vertical column.
Here are all the public constructors and methods:
StringList2(String[] stringArray, int orientation):
orientation can be HORIZONTAL and
VERTICAL.
StringList2(String[] stringArray) : default orientation is
HORIZONTAL.
void setOrientation(int orientation)
int getOrientation()
In below is an example. We create 3 StringList2s.
StringList2Test.java
You can get the program here.
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class StringList2Test {
5: public static void main(String[] args) {
6: JFrame f = new JFrame();
7:
8: String[] stringArray1 = {"zero", "one", "two", "three"};
9: StringList2 list1 = new StringList2(stringArray1, StringList2.VERTICAL);
10: f.getContentPane().add(list1, BorderLayout.WEST);
11:
12: String[] stringArray2 = {"A", "B", "C"};
13: StringList2 list2 = new StringList2(stringArray2, StringList2.HORIZONTAL);
14: f.getContentPane().add(list2, BorderLayout.CENTER);
15:
16: String[] stringArray3 = {"1","2","3","4","5"};
17: StringList2 list3 = new StringList2(stringArray3);
18: f.getContentPane().add(list3, BorderLayout.NORTH);
19:
20: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21: f.pack();
22: f.setVisible(true);
23: }
24:}
We use the public constructors at line 9,13,17.
Answer : StringList2.java
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class StringList2 extends JPanel {
5: private String[] stringArray;
6: private JButton[] buttons;
7: private int orientation;
8: public static int HORIZONTAL = 0;
9: public static int VERTICAL = 1;
10: public StringList2(String[] stringArray, int orientation) {
11: this.stringArray = stringArray;
12: buttons = new JButton[stringArray.length];
13: setOrientation(orientation);
14: for(int i = 0; i < stringArray.length; i++) {
15: buttons[i] = new JButton(stringArray[i]);
16: add(buttons[i]); // add buttons[i], a method from JPanel
17: }
18: }
19:
20: public StringList2(String[] stringArray) {
21: this(stringArray, HORIZONTAL);
22: }
23:
24: public void setOrientation(int orientation) {
25: this.orientation = orientation;
26: if(orientation == VERTICAL) {
27: setLayout(new GridLayout(0,1)); // col = 1
28: } else {
29: setLayout(new GridLayout(1,0)); // row = 1
30: }
31: }
32:
33: public int getOrientation() {
34: return orientation;
35: }
36:}
Explanation :
- Line 8, 9 : set class variables
HORIZONTAL and
VERTICAL.
- Line 24 - 31 : set the orientation. To set the orientation, we use
GridLayout.
Question
Fill in the missing part in the program below, so that the output of the program
is
public class PanelTest {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("PanelTest");
// your code below
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250,100);
f.setVisible(true);
}
}
Answer : PanelTest.java
You can get the program here.
1:import java.awt.*;
2:import javax.swing.*;
3:
4:public class PanelTest {
5: public static void main(String[] args) {
6: JFrame f = new JFrame();
7: f.setTitle("PanelTest");
8: JPanel centerPanel = new JPanel(new BorderLayout());
9: centerPanel.add(new JLabel("Label", JLabel.CENTER), BorderLayout.NORTH);
10: centerPanel.add(new JButton("Button"),BorderLayout.CENTER);
11: f.getContentPane().add(centerPanel);
12: JPanel eastPanel = new JPanel(new GridLayout(3,1));
13: eastPanel.add(new JButton("alpha"));
14: eastPanel.add(new JButton("beta"));
15: eastPanel.add(new JButton("gamma"));
16: f.getContentPane().add(eastPanel, BorderLayout.EAST);
17: f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18: f.setSize(250,100);
19: f.setVisible(true);
20: }
21:}