The Artima Developer Community
Sponsored Link

Java Answers Forum
very little GUI help needed

7 replies on 1 page. Most recent reply: Apr 21, 2002 9:11 PM by Hoody

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

very little GUI help needed Posted: Apr 19, 2002 10:34 PM
Reply to this message Reply
Advertisement
any one know how i can make this look better like the text fields up top and the add and subtract on the bottom side by side or something?
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import javax.swing.*;
 
public class TextFieldTest extends JFrame implements ActionListener
{
 
    private JTextField number1Field;  // used in both constructor and actionPerformed
	private JTextField number2Field;
	private JTextField sumField;
	private JTextField subField;
 
 
 
	public TextFieldTest(String title)
	{
      super(title);
 
      // Set up text field for number 1
      number1Field = new JTextField(4);
 
      // Set up text field for number 2
      number2Field = new JTextField(4);
      // Set up text field for result
      sumField = new JTextField(5);
      sumField.setEditable(false);
 
      subField = new JTextField(5);
      subField.setEditable(false);
 
 
      // set up a button that will add number 1 and number 2 and add a listener
      JButton calcButton = new JButton("Add");
      calcButton.addActionListener(this);
      calcButton.setActionCommand("Add");
 
      // set up a button that will add number 1 and number 2 and add a listener
      JButton subButton = new JButton("Subtract");
      subButton.addActionListener(this);
	  subButton.setActionCommand("Subtract");
 
 
      // set up panel for input numbers with labels
      JPanel inputNumberPanel = new JPanel();
      inputNumberPanel.add(new JLabel(" Number1"));
      inputNumberPanel.add(number1Field);
      inputNumberPanel.add(new JLabel(" Number2"));
      inputNumberPanel.add(number2Field);
 
      // set up panel to display result
      JPanel resultPanel = new JPanel();
      resultPanel.add(new JLabel("Sum: "));
      resultPanel.add(sumField);
 
      // set up panel to display result
	  JPanel resultPanel2 = new JPanel();
	  resultPanel2.add(new JLabel("Subtract: "));
      resultPanel2.add(subField);
 
 
 
      // add components to content pane
	  getContentPane().add(inputNumberPanel, "Center");
	  getContentPane().add(resultPanel, "South");
	  getContentPane().add(resultPanel2, "East");
 
 
 
      // set up a panel for the button and place the button in a panel
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(calcButton);
 
      // set up a panel for the button and place the button in a panel
	  JPanel buttonPanel2 = new JPanel();
      buttonPanel2.add(subButton);
 
 
 
      // stack up these two panels in another panel
      JPanel southPanel = new JPanel();
      southPanel.setLayout(new GridLayout(2, 1));
      southPanel.add(resultPanel);
      southPanel.add(buttonPanel);
 
	  // stack up these two panels in another panel
	  JPanel eastPanel = new JPanel();
	  eastPanel.setLayout(new GridLayout(3, 3));
	  eastPanel.add(resultPanel2);
      eastPanel.add(buttonPanel2);
 
 
 
      getContentPane().add(southPanel, "South");
	  getContentPane().add(eastPanel, "East");
 
 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      pack();
      show();
   }
 
 
 
   public void actionPerformed(ActionEvent event)
   {  	String command = event.getActionCommand();
	   // get user input from input textfields
 
         int num1 = Integer.parseInt(number1Field.getText().trim());
         int num2 = Integer.parseInt(number2Field.getText().trim());
 
         // compute average and display in result field
 
         int sum;
         int subtract;
 
 
         if (command.equals("Add"))
         {
		    sum = num1 + num2;
		    sumField.setText("" + sum);
   		 }
		 else if (command.equals("Subtract"))
		 {
		    subtract = num1 - num2;
		    subField.setText("" + subtract);
   		 }
 
 
 
 
 
 
         // set result in textfield resultField
 
 
   }
 
 
   public static void main(String args[])
   {
   		new TextFieldTest("Test of Text Fields");
   }
}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: very little GUI help needed Posted: Apr 20, 2002 6:17 PM
Reply to this message Reply
 
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import javax.swing.*;

public class TextFieldTest extends JFrame implements ActionListener{

private JTextField number1Field; // used in both constructor and actionPerformed
private JTextField number2Field;
private JLabel resultLabel;

public TextFieldTest(String title) {
super(title);

JPanel inputPanel = new JPanel();

// Set up text field for number 1
number1Field = new JTextField(4);

// Set up text field for number 2
number2Field = new JTextField(4);

inputPanel.add(new JLabel(" Number1"));
inputPanel.add(number1Field);
inputPanel.add(new JLabel(" Number2"));
inputPanel.add(number2Field);

// set up a button that will add number 1 and number 2 and add a listener
JButton calcButton = new JButton("Add");
calcButton.addActionListener(this);
calcButton.setActionCommand("Add");

// set up a button that will add number 1 and number 2 and add a listener
JButton subButton = new JButton("Subtract");
subButton.addActionListener(this);
subButton.setActionCommand("Subtract");
JPanel buttonPanel = new JPanel();
buttonPanel.add(calcButton);
buttonPanel.add(subButton);

JPanel resultPanel = new JPanel();
resultLabel = new JLabel("");
resultPanel.add(new JLabel("Result: "));
resultPanel.add(resultLabel);

// add components to content pane
getContentPane().add(inputPanel, "North");
getContentPane().add(buttonPanel, "Center");
getContentPane().add(resultPanel, "South");
pack();
show();
}



public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
int num1 = toInteger(number1Field.getText().trim());
int num2 = toInteger(number2Field.getText().trim());

if (command.compareTo("Add") == 0){

resultLabel.setText(String.valueOf(num1 + num2));
}else if (command.compareTo("Subtract") == 0){
resultLabel.setText(String.valueOf(num1 - num2));
}
}


private int toInteger(String s){
int n = -1;
try{
n = Integer.parseInt(s);
}catch(NumberFormatException nfe){
System.err.println("NumberFormatException: " + nfe.getMessage());
}
return n;
}

public static void main(String args[]) {
new TextFieldTest("Test of Text Fields");
}

}

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: very little GUI help needed Posted: Apr 20, 2002 10:04 PM
Reply to this message Reply
the problem i was having wasnt the way it worked it was the way the layout looked because it did not look to be user friendly it was kind of cluttered and i wanted to get the subtract to the bottom so its side by side with the add but i dont know how to do it

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: very little GUI help needed Posted: Apr 21, 2002 10:56 AM
Reply to this message Reply

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

public class TextFieldTest extends JFrame implements ActionListener{

private JTextField number1Field; // used in both constructor and actionPerformed
private JTextField number2Field;
private JLabel resultLabel;

public TextFieldTest(String title) {
super(title);

JPanel inputPanel = new JPanel();

// Set up text field for number 1
number1Field = new JTextField(4);

// Set up text field for number 2
number2Field = new JTextField(4);

inputPanel.add(new JLabel(" Number1"));
inputPanel.add(number1Field);
inputPanel.add(new JLabel(" Number2"));
inputPanel.add(number2Field);

// set up a button that will add number 1 and number 2 and add a listener
JButton calcButton = new JButton("Add");
calcButton.addActionListener(this);
calcButton.setActionCommand("Add");
JPanel addPanel = new JPanel();
addPanel.add(calcButton);

// set up a button that will add number 1 and number 2 and add a listener
JButton subButton = new JButton("Subtract");
subButton.addActionListener(this);
subButton.setActionCommand("Subtract");
JPanel subtractPanel = new JPanel();
subtractPanel.add(subButton);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BorderLayout());
buttonPanel.add(subtractPanel,"West");
buttonPanel.add(addPanel,"East");



JPanel resultPanel = new JPanel();
resultLabel = new JLabel("");
resultPanel.add(new JLabel("Result: "));
resultPanel.add(resultLabel);

// add components to content pane
getContentPane().add(inputPanel, "North");
getContentPane().add(resultPanel, "Center");
getContentPane().add(buttonPanel, "South");
pack();
show();
}



public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
int num1 = toInteger(number1Field.getText().trim());
int num2 = toInteger(number2Field.getText().trim());

if (command.compareTo("Add") == 0){

resultLabel.setText(String.valueOf(num1 + num2));
}else if (command.compareTo("Subtract") == 0){
resultLabel.setText(String.valueOf(num1 - num2));
}
}


private int toInteger(String s){
int n = -1;
try{
n = Integer.parseInt(s);
}catch(NumberFormatException nfe){
System.err.println("NumberFormatException: " + nfe.getMessage());
}
return n;
}

public static void main(String args[]) {
new TextFieldTest("Test of Text Fields");
}

}

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: very little GUI help needed Posted: Apr 21, 2002 4:52 PM
Reply to this message Reply
the layout is the same as that last code

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: very little GUI help needed Posted: Apr 21, 2002 7:03 PM
Reply to this message Reply

No its not.

I created an addPanel and a subtractPanel and added them to a new panel with its own layout so the addPanel and subtractPanel are side by side and at the bottom just as you asked.

If this is still not it, perhaps you could be more specific?

Anyway, if you want to solve your problem, I suggest you create JPanel components to hold the desired sub components with the type layout you need, and then add the JPanels to parent panels or the getContentPane() panel in your JFrame.

You can do it!! Don't give up. There are lots of ways to do this kind of thing.

Best wishes,
Charles

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: very little GUI help needed Posted: Apr 21, 2002 9:04 PM
Reply to this message Reply
i took your code and ran it and it was the same layout as that i had orginally with the two number fields up top the subtract on top to the right and the add on the bottom in the center not subtract and add side by side on the bottom

Hoody

Posts: 33
Nickname: hoodlum
Registered: Feb, 2002

Re: very little GUI help needed Posted: Apr 21, 2002 9:11 PM
Reply to this message Reply
my fault it was my mistake when i changed the name saving it...thanx for ur help sorry about the confusion

Flat View: This topic has 7 replies on 1 page
Topic: parsing an sql statement using grammers in java Previous Topic   Next Topic Topic: finding classpath visible to classloader

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use