Getting Java GUI to look 'decent'.....

Discussion in 'Software' started by TR15220, Mar 18, 2006.

  1. TR15220

    TR15220 Private First Class

    After using VB and Visual C++ with the 'drag & drop' to build the GUI - trying to get Buttons, Labels, etc. to look half way decent in Java is tough....for me anyway.
    This is a farly stupid Java assignment so I apologize for you having to look at something so lame......

    I've done a lot of googling and I think I could use the GridBag Layout to do what I want but I can't find an example that shows me how to code it!!!!!

    What I'd like is......

    ------------------------------------------------------------------
    |-----------------------------------------------|--Button 1-------|
    |-----------------------------------------------|--Button 2-------|
    |-----------------------------------------------|--Button 3-------|
    |-----------------------------------------------|--Button 4-------|
    |-------------Graphics Area---------------------|-----------------|
    |-----------------------------------------------|-----------------|
    |-----------------------------------------------|--Label 1 -------|
    |-----------------------------------------------|--Label 2 -------|
    |-----------------------------------------------|-----------------|
    |-----------------------------------------------|-----------------|
    |-----------------------------------------------|-----------------|

    There is nothing the book that deals with anything beyond FlowLayout and the Border but I'm thinking there has to be a way to make it look better.
    Both the FlowLayout and the Border look way crappy compared to what VB or Visual C++ can do.

    Thanks in advance for any help!!!!!

    Here is the code to give you an idea of what I am working on......

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Prog9_1 extends JFrame implements ActionListener {

    private JButton rightButton, leftButton, upButton, downButton;
    private JPanel panel;

    private Balloon balloon;

    public static void main(String[] args) {
    Prog9_1 demo = new Prog9_1 ();
    demo.setSize(600,600);
    demo.createGUI();
    demo.show();
    }

    private void createGUI() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    // window.setLayout(new FlowLayout() );
    window.setLayout(new GridLayout(2,3));

    panel = new JPanel();
    panel.setPreferredSize(new Dimension(400, 400));
    panel.setBackground(Color.white);
    window.add(panel);

    leftButton = new JButton("<-Left");
    window.add(leftButton);
    leftButton.addActionListener(this);

    rightButton = new JButton("Right->");
    window.add(rightButton);
    rightButton.addActionListener(this);

    upButton = new JButton("Up");
    window.add(upButton);
    upButton.addActionListener(this);

    downButton = new JButton("Down");
    window.add(downButton);
    downButton.addActionListener(this);

    balloon = new Balloon();
    }

    public void actionPerformed(ActionEvent event) {
    Graphics paper = panel.getGraphics();
    if (event.getSource() == leftButton) {
    balloon.moveLeft(20);
    }
    if (event.getSource() == rightButton) {
    balloon.moveRight(20);
    }
    if (event.getSource() == upButton) {
    balloon.moveUp(20);
    }
    if (event.getSource() == downButton) {
    balloon.moveDown(20);
    }
    paper.setColor(Color.white);
    paper.fillRect(0, 0, 300, 300);
    balloon.display(paper);
    }
    }


    This is a separate .java file......

    import java.awt.*;
    public class Balloon {

    private int x = 50;
    private int y = 50;
    private int diameter = 20;

    public void moveLeft(int xStep) {
    x = x - xStep;
    }
    public void moveRight(int xStep) {
    x = x + xStep;
    }
    public void moveUp(int yStep) {
    y = y - yStep;
    }
    public void moveDown(int yStep) {
    y = y + yStep;
    }
    public void display(Graphics paper) {
    paper.setColor(Color.blue);
    paper.drawOval(x, y, diameter, diameter);
    }
    }
     
  2. TR15220

    TR15220 Private First Class

    Look like.....

    ----------------------------------------------------------------------
    |-----------------------------------------------|--Button 1-------|
    |-----------------------------------------------|--Button 2-------|
    |-----------------------------------------------|--Button 3-------|
    |-----------------------------------------------|--Button 4-------|
    |-------------Graphics Area-------------|---------------------|
    |-----------------------------------------------|---------------------|
    |-----------------------------------------------|--Label 1 -------|
    |-----------------------------------------------|--Label 2 -------|
    |-----------------------------------------------|---------------------|
    ----------------------------------------------------------------------
     
  3. TR15220

    TR15220 Private First Class

    Bummer---No one works with the Java GUI, huh?

    I'l still digging thru the documentation.....but I could sure use a simple, straight forward example of how to do this.

    Is it just me or are all the Java examples and documentation overly complicated?
    I mean I understand that is is 'geek' orientated but, come on, this stuff shouldn't really be this hard......look at what you can do in VB and Visual C++ without even breaking a sweat!!!!!!
     
  4. jloyd01

    jloyd01 Private E-2

    Hard coding Java GUI's is very tedious, I usually use a tool like NetBeans which has a DND style GUI builder. If this is a one time thing, that might be too big of a hassle to learn.

    Your right about the grid bag layout. Basically it makes your container (usually a JPanel) a big grid where you can place GUI objects. I like to think of it like an HTML table. You can add cell spacing, span multiple columns and such.

    An example:

    The following should be close to what you want to display. I generated it with netbeans, but you should be able to follow the code pretty well

    Code:
        private javax.swing.JScrollPane graphics_area;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JButton jButton4;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextArea jTextArea1;
    
        private void initComponents() {
            java.awt.GridBagConstraints gridBagConstraints;
    
            jPanel1 = new javax.swing.JPanel();
            graphics_area = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jPanel1.setLayout(new java.awt.GridBagLayout());
    
            graphics_area.setPreferredSize(new java.awt.Dimension(162, 192));
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            graphics_area.setViewportView(jTextArea1);
    
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.gridheight = 10;
            gridBagConstraints.insets = new java.awt.Insets(7, 7, 7, 7);
            jPanel1.add(graphics_area, gridBagConstraints);
    
            jButton1.setText("jButton1");
            jPanel1.add(jButton1, new java.awt.GridBagConstraints());
    
            jButton2.setText("jButton2");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridy = 1;
            jPanel1.add(jButton2, gridBagConstraints);
    
            jButton3.setText("jButton3");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridy = 2;
            jPanel1.add(jButton3, gridBagConstraints);
    
            jButton4.setText("jButton4");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridy = 3;
            jPanel1.add(jButton4, gridBagConstraints);
    
            jLabel1.setText("jLabel1");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridy = 5;
            jPanel1.add(jLabel1, gridBagConstraints);
    
            jLabel2.setText("jLabel2");
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridy = 6;
            jPanel1.add(jLabel2, gridBagConstraints);
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            );
            pack();
        }
    
    Just set this up as a class that extends JFrame, call the method from the constructor. Now you can create and instance of the class and call "[object name].setVisible(true);"
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds