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
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);
}
}
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>