Jump to content

ActionEvent/getText/StringBuilder

- - - - -

  • Please log in to reply
4 replies to this topic

#1
red

red

    Newbie

  • Members
  • Pip
  • 4 posts
I am new to Java and am trying to create a program that uses GridLayout, panels, has 2 labels and buttons across the top to enter a starting and end number, a button in the center to make the program calculate all of the numbers divisible by 2, and a display box centered at the bottom that uses scroll bars when necessary to display all the numbers divisible by 2 within the user defined range.

Below is my code. I would appreciate some help resolving the errors. I am not sure what I did wrong or how to solve these errors at all. I am required to use StringBuilder, setText(), and toString(). Thank you in advance for any pointers and advice on this!!

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

public class DivisibleByTwoGUI extends JFrame implements ActionListener{

private final int HEIGHT = 450;
private final int WIDTH = 600;

private JLabel lblStartNum, lblEndNum, lblBlank;
private JTextField txtStartNum, txtEndNum;
private JButton btnShow;
private JScrollPane scrollResults;
private JTextArea txtareaResults;
private JPanel p1, p2, p3, p4;

private String start, end;
private int startInt, endInt;


public DivisibleByTwoGUI(String title){
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(HEIGHT, WIDTH);

setLayout(new GridLayout(1,1));

p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();

p1.setLayout(new GridLayout(3,1));
p2.setLayout(new FlowLayout());
p3.setLayout(new GridLayout(1,3));
p4.setLayout(new GridLayout(1,3));

p2.add(p1);
p3.add(p1);
p4.add(p1);
add(p1);

p2.add(lblStartNum);
p2.add(txtStartNum);
p2.add(lblEndNum);
p2.add(txtEndNum);

p3.add(lblBlank);
p3.add(btnShow);
p3.add(lblBlank);

txtareaResults = new JTextArea(5,30);
txtareaResults.setEditable(false);
scrollResults = new JScrollPane(txtareaResults);

p4.add(lblBlank);
p4.add(scrollResults);
p4.add(lblBlank);

btnShow = new JButton("Show numbers divisible by 2");
btnShow.addActionListener(this);

}

public void actionPerformed(ActionEvent ae){
StringBuilder str = new StringBuilder();
if(event.getSource() == btnShow){

String start = txtStartNum.getText();
String end = txtEndNum.getText();
startInt = Integer.parseInt(start);
endInt = Integer.parseInt(end);

if(StartInt <= EndInt){
StartInt = StartInt + EndInt;
EndInt = StartInt - EndInt;
StartInt = StartInt - EndInt;
}

while(StartInt <= EndInt){
if(StartInt % 2 == 0){
str.append(StartInt + "\n");
StartInt++;
}
}
}
txtareaResults.setText(str.toString());
}

}

-------------------------------------------------------------------------------------------------------------------
Below is seperate code to test with:

public class DivisibleByTwoGUITest{
public static void main(String[] args){

DivisibleByTwoGUI screen = new DivisibleByTwoGUI("Divisible by Two");
screen.setSize(600,450);
screen.setVisible(true);
}
}

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Please also post the error. For us to find the error now we must go paste it in our ide and run it while we can propably just tell you what's wrong with the error.
(Yes i'm THAT lazy)

#3
red

red

    Newbie

  • Members
  • Pip
  • 4 posts
Hello, I have fixed the errors I am getting, but now upon running the program I get only a blank window with no panels, buttons, text areas, etc. Could you look at my code below and see if you can determine the issue and what i did wrong?


import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class DivisibleByTwoGUI extends JFrame implements ActionListener{


	private final int HEIGHT = 450;

	private final int WIDTH = 600;


	private JLabel lblStartNum, lblEndNum, lblBlank;

	private JTextField txtStartNum, txtEndNum;

	private JButton btnShow;

	private JScrollPane scrollResults;

	private JTextArea txtareaResults;

	private JPanel p1, p2, p3, p4;



	public DivisibleByTwoGUI(String title){

		super(title);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setSize(HEIGHT, WIDTH);


		setLayout(new GridLayout(1,1));


		p1 = new JPanel();

		p2 = new JPanel();

		p3 = new JPanel();

		p4 = new JPanel();


		lblStartNum = new JLabel("Enter Starting Number: ");

		lblEndNum = new JLabel("Ender Ending Number: ");

		lblBlank = new JLabel(" ");


		txtStartNum = new JTextField(10);

		txtEndNum = new JTextField(10);


		btnShow = new JButton("Show numbers divisible by 2");


		p1.setLayout(new GridLayout(3,1));

		p2.setLayout(new FlowLayout());

		p3.setLayout(new GridLayout(1,3));

		p4.setLayout(new GridLayout(1,3));


		p2.add(p1);

		p3.add(p1);

		p4.add(p1);

		add(p1);


		p2.add(lblStartNum);

		p2.add(txtStartNum);

		p2.add(lblEndNum);

		p2.add(txtEndNum);


		p3.add(lblBlank);

		p3.add(btnShow);

		p3.add(lblBlank);


		txtareaResults = new JTextArea(5,30);

		txtareaResults.setEditable(false);

		scrollResults = new JScrollPane(txtareaResults);


		p4.add(lblBlank);

		p4.add(scrollResults);

		p4.add(lblBlank);



		btnShow.addActionListener(this);


	}


	public void actionPerformed(ActionEvent event){


		int startInt, endInt;


		StringBuilder str = new StringBuilder();

		if(event.getSource() == btnShow){


			String start = txtStartNum.getText();

			String end = txtEndNum.getText();

			startInt = Integer.parseInt(start);

			endInt = Integer.parseInt(end);


			if(startInt <= endInt){

				startInt = startInt + endInt;

				endInt = startInt - endInt;

				startInt = startInt - endInt;

			}


			while(startInt <= endInt){

				if(startInt % 2 == 0){

					str.append(startInt + "\n");

					startInt++;

				}

			}

		}

		txtareaResults.setText(str.toString());

	}


}


[test code]
public class DivisibleByTwoGUITest{
public static void main(String[] args){

DivisibleByTwoGUI screen = new DivisibleByTwoGUI("Divisible by Two");
screen.setSize(600,450);
screen.setVisible(true);
}
}
[/code]

#4
mariob316

mariob316

    Newbie

  • Members
  • Pip
  • 8 posts
Where are you adding the panels to the JFrame?

is see

p2.add(p1);
p3.add(p1);
p4.add(p1);
add(p1);



I think you mean

p1.add(p2);
p1.add(p3);
p1.add(p4);
add(p1);


you should add that after btnShow.addActionListener(this);

#5
red

red

    Newbie

  • Members
  • Pip
  • 4 posts
Thank you!!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users