public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Exercise17_09 inst = new Exercise17_09();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}});
}//end main
public Exercise17_09() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
top = new JPanel();
getContentPane().add(top, BorderLayout.NORTH);
top.setPreferredSize(new java.awt.Dimension(392, 122));
{
jlblGrapes = new JLabel("Grapes", icGrapes, SwingConstants.CENTER);
jlblGrapes.setPreferredSize(new java.awt.Dimension(392,122));
jlblGrapes.setHorizontalTextPosition(SwingConstants.CENTER);
}
}//end
{
bottom = new JPanel();
GridLayout BottomPanelLayout = new GridLayout(1, 2);
BottomPanelLayout.setHgap(5);
BottomPanelLayout.setVgap(5);
BottomPanelLayout.setColumns(2);
bottom.setLayout(BottomPanelLayout);
getContentPane().add(bottom, BorderLayout.CENTER);
bottom.setPreferredSize(new java.awt.Dimension(392, 130));
{
AlignPanel = new JPanel();
BorderLayout AlignPanelLayout = new BorderLayout();
bottom.add(AlignPanel);
AlignPanel.setLayout(AlignPanelLayout);
AlignPanel.setBorder(BorderFactory.createTitledBorder(null, "Horizontal Alignment", TitledBorder.LEADING, TitledBorder.TOP));
{
lblpnl = new JPanel();
GridLayout jPanel1Layout = new GridLayout(2, 1);
jPanel1Layout.setHgap(5);
jPanel1Layout.setVgap(5);
jPanel1Layout.setColumns(1);
jPanel1Layout.setRows(2);
AlignPanel.add(lblpnl, BorderLayout.CENTER);
lblpnl.setLayout(jPanel1Layout);
{
jlblfive = new JLabel();
lblpnl.add(jlblfive);
jlblfive.setText("Horizontal");
}
{
jlblfour = new JLabel();
lblpnl.add(jlblfour);
jlblfour.setText("Vertical");
}
}
{
align = new JPanel();
GridLayout AligncomboLayout = new GridLayout(2, 1);
AligncomboLayout.setHgap(5);
AligncomboLayout.setVgap(5);
AligncomboLayout.setColumns(1);
AligncomboLayout.setRows(2);
AlignPanel.add(align, BorderLayout.EAST);
align.setLayout(AligncomboLayout);
{
ComboBoxModel jcboHAModel =
new DefaultComboBoxModel(
new String[] { "LEFT", "CENTER", "RIGHT" });
jcbofour = new JComboBox();
align.add(jcbofour);
jcbofour.setModel(jcboHAModel);
}
{
ComboBoxModel jcboVAModel =
new DefaultComboBoxModel(
new String[] { "TOP", "CENTER", "BOTTOM" });
jcbothree = new JComboBox();
align.add(jcbothree);
jcbothree.setModel(jcboVAModel);
}
}
}
{
text = new JPanel();
BorderLayout TextPanelLayout = new BorderLayout();
bottom.add(text);
text.setLayout(TextPanelLayout);
text.setBorder(BorderFactory.createTitledBorder("Text Position"));
{
lblpane = new JPanel();
GridLayout Label2panelLayout = new GridLayout(2, 1);
Label2panelLayout.setHgap(5);
Label2panelLayout.setVgap(5);
Label2panelLayout.setColumns(1);
Label2panelLayout.setRows(2);
text.add(lblpane, BorderLayout.CENTER);
lblpane.setLayout(Label2panelLayout);
{
jlblthree = new JLabel();
lblpane.add(jlblthree);
jlblthree.setText("Horizontal");
}
{
jlbltwo = new JLabel();
lblpane.add(jlbltwo);
jlbltwo.setText("Vertical");
}
}
{
txtcom = new JPanel();
GridLayout TextComboLayout = new GridLayout(2, 1);
TextComboLayout.setHgap(5);
TextComboLayout.setVgap(5);
TextComboLayout.setColumns(1);
TextComboLayout.setRows(2);
txtcom.setLayout(TextComboLayout);
text.add(txtcom, BorderLayout.EAST);
{
ComboBoxModel jcboTHAModel =
new DefaultComboBoxModel(
new String[] { "LEFT", "CENTER", "RIGHT" });
jcbotwo = new JComboBox();
txtcom.add(jcbotwo);
jcbotwo.setModel(jcboTHAModel);
jcbotwo.setSelectedItem("CENTER");
}
{
ComboBoxModel jcboTVAModel =
new DefaultComboBoxModel(
new String[] { "TOP", "CENTER", "BOTTOM" });
jcboone = new JComboBox();
txtcom.add(jcboone);
jcboone.setModel(jcboTVAModel);
jcboone.setSelectedItem("CENTER");
}
}
}
Your code tags haven't worked as you have an image embedded in the closing tag.
It's hard to review code when you don't know what it is supposed to do or if it even works or not.
I can say it doesn't conform to Java naming standards - you shouldn't have a variable called 'A'. 'A' apart from being the wrong case isn't very descriptive.
If you are using the same text in a number of places ie "LEFT", "RIGHT" etc you should declare them as constants. It reads better and saves making typing errors which can be hard to spot. If you have a constant called RIGHT and you type R1GHT by mistake the compiler will complain whereas if it is just text you won't get a warning and will be left scratching your head when it doesn't work.
Personally I don't like extending JFrame in these cases for a number of reasons, such as:
It fails the is-a-kind-of rule. Your class is not a specialisation of a JFrame it's a GUI wrapped in a JFrame.
It's less versatile. If you extend JPanel you can easily change your app from displaying the GUI in a JFrame to a JDialog or JInternalFrame or even adding it to a more complex GUI comprising of several panels.
Bookmarks