Image Properties Code Review
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Image Properties Code Review

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    5

    Image Properties Code Review

    Is there any issues with this code that can be fixed or made better?


    [code]
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.TitledBorder;

    public class Exercise17_09 extends JFrame {
    private ImageIcon icGrapes = new ImageIcon("C:\temp");
    private JPanel top;
    private JPanel bottom;
    private JPanel text;
    private JPanel align;
    private JPanel txtcom;
    private JPanel lblpane;
    private JPanel lblpnl;
    private JPanel AlignPanel;
    private JLabel jlblGrapes;
    private JLabel jlbltwo;
    private JLabel jlblthree;
    private JLabel jlblfour;
    private JLabel jlblfive;
    private JComboBox jcboone;
    private JComboBox jcbotwo;
    private JComboBox jcbothree;
    private JComboBox jcbofour;




    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");
    }
    }
    }

    jcbofour.addItemListener(new ItemListener() {


    public void itemStateChanged(ItemEvent e) {

    String A = (String)jcbofour.getSelectedItem();
    if(A.equals("LEFT")) {
    jlblGrapes.setHorizontalAlignment(SwingConstants.LEFT);
    }
    else if(A.equals("CENTER")) {
    jlblGrapes.setHorizontalAlignment(SwingConstants.CENTER);
    }
    else if(A.equals("RIGHT")) {
    jlblGrapes.setHorizontalAlignment(SwingConstants.RIGHT);
    }
    }
    });

    jcbothree.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
    String A = (String)jcbothree.getSelectedItem();
    if(A.equals("TOP")) {
    jlblGrapes.setVerticalAlignment(SwingConstants.TOP);
    }
    else if(A.equals("CENTER")) {
    jlblGrapes.setVerticalAlignment(SwingConstants.CENTER);
    }
    else if(A.equals("BOTTOM")) {
    jlblGrapes.setVerticalAlignment(SwingConstants.BOTTOM);
    }
    }
    });


    jcbotwo.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
    String A = (String)jcbotwo.getSelectedItem();
    if(A.equals("LEFT")) {
    jlblGrapes.setHorizontalTextPosition(SwingConstants.LEFT);
    }
    else if(A.equals("CENTER")) {
    jlblGrapes.setHorizontalTextPosition(SwingConstants.CENTER);
    }
    else if(A.equals("RIGHT")) {
    jlblGrapes.setHorizontalTextPosition(SwingConstants.RIGHT);
    }
    }
    });

    jcboone.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent e) {
    String A = (String)jcboone.getSelectedItem();
    if(A.equals("TOP")) {
    jlblGrapes.setVerticalTextPosition(SwingConstants.TOP);
    }
    else if(A.equals("CENTER")) {
    jlblGrapes.setVerticalTextPosition(SwingConstants.CENTER);
    }
    else if(A.equals("BOTTOM")) {
    jlblGrapes.setVerticalTextPosition(SwingConstants.BOTTOM);
    }
    }
    });

    }
    pack();
    setSize(400, 300);
    } catch (Exception e) {
    e.printStackTrace();



    }

    }

    }

    [/codeName:  grapes.gif
Views: 58
Size:  3.0 KB]

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Image Properties Code Review

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Image Properties Code Review

    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center