JAVA GUI

For a class assignment, I am writing a GUI program that converts all lowercase letters to capitol letters. I have the GUI part pretty well figured out. I am confused about exactly how to add the string to be input by the user. This is what I have so far:

CODE--

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class A3CA3205890test {

private static class Caps extends JPanel {

public void paintComponent(Graphics g) {

super.paintComponent(g);

}

}

private static class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

}

public static void main(String[] args) {

Caps displayPanel = new Caps();

JButton okButton = new JButton("OK");

ButtonHandler listener = new ButtonHandler();

okButton.addActionListener(listener);

JPanel content = new JPanel();

content.setLayout(new BorderLayout());

content.add(displayPanel, BorderLayout.CENTER);

content.add(okButton, BorderLayout.SOUTH);

JFrame window = new JFrame("A3CA3205890 JAVA GUI");

window.setContentPane(content);

window.setSize(500,200);

window.setLocation(100,100);

window.setVisible(true);

}

}

__CODE

Can anyone help me out?

Thanks

[1373 byte] By [superfoolioa] at [2007-11-27 9:11:45]
# 1
yes. i can probably write the whole thing for you.
mkoryaka at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 2
public class A3CA3205890test {I just threw up in my mouth...
CaptainMorgan08a at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 3
Your Caps class is equivalent to a normal JPanel. How do you want to get input? Do you want to use a text area, or something like a JOptionPane?
CaptainMorgan08a at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 4
I think a text area would probably be best, but whatever you think is better.
superfoolioa at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 5

not the best one but try to modify and learn it.

package com.org.guiprogramming;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class CapitalLetter extends JFrame {

private JPanel panel;

private JButton button;

private JTextField textField;

private JTextField answerField;

public static void main(String[] args) {

CapitalLetter frame = new CapitalLetter();

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public CapitalLetter() {

setSize(200,300);

panel = new JPanel(new FlowLayout());

button = new JButton("Capitalize");

textField = new JTextField(10);

answerField = new JTextField(10);

Container contentPane = getContentPane();

textField.setText("this is nice");

panel.add(textField);

panel.add(button);

panel.add(answerField);

contentPane.add(panel);

ActionListener buttonListener = new ActionListener() {

public void actionPerformed(ActionEvent event) {

String text = textField.getText();

String answer = text.toUpperCase();

answerField.setText(answer);

};

};

button.addActionListener(buttonListener);

}

}

fastmikea at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 6

to convert only first letter of word

ActionListener buttonListener = new ActionListener() {

public void actionPerformed(ActionEvent event) {

String text = textField.getText();

char ch;

StringBuffer result = new StringBuffer();

for(int i=0; i<text.length(); i++) {

ch = text.charAt(i);

if(Character.isLetter(ch) && ((i==0) || !Character.isLetter(text.charAt(i-1))))

result.append(Character.toUpperCase(ch));

else

result.append(Character.toLowerCase(ch));

}

//String answer = text.toUpperCase();

answerField.setText(result.toString());

};

};

button.addActionListener(buttonListener);

Good luck>

fastmikea at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 7
Thank you sir, I checked this out and it drives me crazy how there seems to be so many vastly different ways to do the same thing, but I understand what you did and appreciate your help.
superfoolioa at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 8
ah fastmike, you love helping people cheat.
mkoryaka at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 9
Why write all that code for the GUI when NetBeans or JSE will do it for you graphically? This is not a smart alec question. I would really like to know why people would rather write their own GUI code than use the GUI creator.
pberardi1a at 2007-7-12 21:57:34 > top of Java-index,Java Essentials,New To Java...
# 10
because its reliable. if i get hired for a java swing developer i want to make sure i know everything about swing components. FUCKK neatbeans gui Edtior will give you pain sooner or later on..
schumachera at 2007-7-12 21:57:35 > top of Java-index,Java Essentials,New To Java...