Java GUI help

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Why does this work.
Code:
import javax.swing.*;
import java.awt.*;

public class SolarSystemGUI extends JFrame {
	JPanel mypanel = new JPanel();
	JLabel label = new JLabel();
	JLabel label2 = new JLabel();
	FlowLayout layout = new FlowLayout();
	
	public SolarSystemGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		label.setText("Solar System controller:");
		label2.setText("Author: ");
		mypanel.add(label);
		mypanel.add(label2);
		mypanel.setLayout(layout);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(500, 300);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
	}
}

But this not?

Code:
import javax.swing.*;
import java.awt.*;

public class SolarSystemGUI extends JFrame {
	JPanel mypanel = new JPanel();
	JLabel label = new JLabel();
	JLabel label2 = new JLabel();
	FlowLayout layout = new FlowLayout();
	

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		label.setText("Solar System controller:");
		label2.setText("Author: ");
		mypanel.add(label);
		mypanel.add(label2);
		mypanel.setLayout(layout);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(500, 300);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
	
}

I can't understand it. To me they seem exactly the same.
 
Last edited:
Thanks.

If I want to add a block of text to a panel is there a better way than using multiple labels? JTextArea formats it right, but the background is white, I want it to look like a label does ie blend into the background, not like a text field where you can enter text on a white background.
 
You can also make use of HTML tags in most components, including JLabel so you could span text over multiple lines easily.

Code:
myJLabel.setText("<html>Hello <br> World!</html>");

That's much more elegant. :) I have to wonder why the people who created java don't allow you to just use \n.
 
Last edited:
Can't get this to work,

Code:
import javax.swing.*;
import java.awt.*;

public class SolarSystemGUI extends JFrame implements ActionListener {
	JPanel mypanel = new JPanel();
	JPanel mypanel2 = new JPanel();
	JLabel mylabel = new JLabel();
	BorderLayout layout = new BorderLayout();
	JButton button = new JButton();
	JButton button2 = new JButton();
	
	public SolarSystemGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		mylabel.setText("<html> Solar System Controller v1.0 <br> Author: ***** </html>");
		button.setText("Add planet");
		button2.setText("Find Planet");
		mypanel.setLayout(layout);
		mypanel2.add(button);
		mypanel2.add(button2);
		mypanel.add("South", mypanel2);
		mypanel.add("North", mylabel);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(350, 200);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {}
}

Says that it can't find ActionEvent or ActionListener.
 
Last edited:
Energize are you using an IDE for your Java programming? Modern IDEs such as Eclipse and NetBeans will pick up these errors immediatly and most of the time suggest corretive action to resolve the issue.

I do normally, but I have no idea how to add classes that I don't have the src code for into a project.
 
Ok, so based on looking at what netbeans does I've got this;

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


public class SolarSystemGUI extends JFrame implements ActionListener {
	JPanel mypanel = new JPanel();
	JPanel mypanel2 = new JPanel();
	JLabel mylabel = new JLabel();
	BorderLayout layout = new BorderLayout();
	JButton button = new JButton();
	JButton button2 = new JButton();
	
	public SolarSystemGUI() {
		button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                buttonActionPerformed(e);
            }
        });
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setContentPane(mypanel);
		mylabel.setText("<html> Solar System Controller v1.0 <br> Author:  </html>");
		button.setText("Add planet");
		button2.setText("Find Planet");
		mypanel.setLayout(layout);
		mypanel2.add(button);
		mypanel2.add(button2);
		mypanel.add("South", mypanel2);
		mypanel.add("North", mylabel);
		this.pack();
		this.setLocation(250, 250);
		this.setSize(350, 200);
		this.setTitle("Solar System v1.0");
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
	
	}
	
	public void buttonActionPerformed(ActionEvent e) {
		System.out.println("test");
	}
}

However I'm assuming there's a much better way to do it than that? The lecture notes mention using .getSource(); and an if statement, but if I try to make the button press to go the actionPerformed method I get a ton of runtime errors.
 
Change it to button.addActionListener(this); then your actionPerformed(ActionEvent e) method will be called.

Are your lecture notes suggesting something like
Code:
public class blah implements ActionListener {
  private JButton mButton1;
  private JButton mButton2;

  public blah(){
    mButton1 = new JButton("1");
    mButton2 = new JButton("2");
  
    mButton1.addActionListener(this);
    mButton2.addActionListener(this);
  }

  public void actionPerformed(ActionEvent e){
    Object Source = e.getSource();
    if(Source == mButton1){
      //button 1 pressed
    }else if(Source == mButton2){
      //button 2 pressed
    }else{
      //impossible to hit here in this case
    }
  }
}

Yes thanks, that's it. :)

I'm just catching up on the java work because the lectures start too early on the monday to go to. Unless I want to be stuck at uni for 11 hours anyway. :eek: Luckily the Java Course has finished for this year, so there's nothing more to learn.
 
Last edited:
How can I add an actionListener to a component automatically upon creation? I have an array which adds pictures to a JButton like this,
Code:
for (int x = 0; x < 12; x++) {
	content.add(new JButton(new ImageIcon("bart" + x + ".jpg")));
}
 
Back
Top Bottom