0

I'm starting to get familiar with Java and the JFrame package, but I'm not quite there. This is what I would like to get:

enter image description here

As you can see, I'd like to have the possibility to fill boxes and then take what is in the boxes and make a pre-written sentence with it !

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Whilst it's great that you're ambitious and looking to learn, SO isn't the place for *this* kind of post. Here you required to present an issue/question to receive answers for. Take a look at [help center](http://stackoverflow.com/help) – Juxhin Dec 08 '14 at 14:27
  • You can head over to the [Java chat room](http://chat.stackoverflow.com/rooms/139/java) and discuss your issues with us. However programming *(just like math)* requires you to work to learn. Us writing for you won't work. If you still have issue email them to me. My email is on my profile. Hope this helps. – Juxhin Dec 08 '14 at 14:33
  • *"I'm starting to get familiar with Java and the `JFrame` package"* `JFrame` is a class, not a package. It resides in the `javax.swing` package. – Andrew Thompson Dec 09 '14 at 00:00
  • BTW - What is your actual *question?* It might be: 1) *"How do I make a button do something when clicked?"* 2) *"How do I retrieve values from fields?"* 3) *"How do I insert values in a string?"* 4) *"How do I display something new in a text area?"* 5) ..something else? Please be specific about what your question is. – Andrew Thompson Dec 09 '14 at 05:11

2 Answers2

1

In Swing, there are two different components. JTextArea and JTextPane. The JTextArea is easy to use, but doesn't allow formatting. If you do not plan on changing the formatting of different words, that is the one to use. The JTextArea is more robust but harder to use.

Check out the Java tutorial for more information.

http://docs.oracle.com/javase/tutorial/uiswing/components/text.html

Timothy Wright
  • 351
  • 2
  • 11
-1

I hope this helps...

TextGenerator.java

package textgenerator;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

public class TextGenerator {

    JFrame frame;
    JPanel panel;
    JTextPane textPane;
    JLabel namel;
    JLabel agel;
    JTextField namef;
    JTextField agef;
    JButton button;

    public TextGenerator() {
        frame = new JFrame("My Frame");//Construct the frame
        frame.setBounds(200, 100, 1000, 500);//set the size and position
        frame.setLayout(new GridLayout(1, 2));//set layout with 1 row and 2 columns
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);//restrict the resixing of the frame

        panel = new JPanel();//create a panel
        panel.setLayout(null);
        textPane = new JTextPane();//create a textpane
        textPane.setEditable(false);
        frame.add(panel);//add panel to the frame
        frame.add(textPane);//add textpane to the frame

        //create labels and textfields and add them to the panel
        namel = new JLabel("Name : ");
        namel.setBounds(20, 200, 150, 20);
        agel = new JLabel("Age : ");
        agel.setBounds(20, 250, 150, 20);
        namef = new JTextField();
        namef.setBounds(220, 200, 150, 20);
        agef = new JTextField();
        agef.setBounds(220, 250, 150, 20);
        panel.add(namel);
        panel.add(agel);
        panel.add(namef);
        panel.add(agef);

        //create button and add it to the panel
        button = new JButton("Done !");
        button.setBounds(350, 400, 100, 20);
        panel.add(button);

        //set the required text to the textfield on button click
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                textPane.setText("Hello !\n"
                        + "My name is " + namef.getText() + ", and I'm " + agef.getText() + ".\n"
                        + "How are you ?");
            }
        });

        frame.setVisible(true);//make the frame visible
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TextGenerator();
            }
        });
    }

}

Output looks like this

Shrinivas Shukla
  • 4,325
  • 2
  • 21
  • 33
  • 1
    Please wrap the call to `new TextGenerator` into a `SwingUtilities#invokeLater` to run it on the EDT – Robin Dec 08 '14 at 17:55
  • Made the changes as suggested by @Robin – Shrinivas Shukla Dec 08 '14 at 18:35
  • `panel.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Dec 09 '14 at 00:01
  • Yes @AndrewThompson, `panel.setLayout(null);` is not a good idea. I was trying to keep the code short so that @user4337633 can understand it better – Shrinivas Shukla Dec 09 '14 at 05:05
  • So.. will you be updating the answer so that it **is** using layouts? – Andrew Thompson Dec 09 '14 at 05:06