0

I'm in a bit of a situation here.I'm making a new program, when you click on the menu bar it opens a new window for the Licence, now here is the problem, how would I add text into that new window, here is my code for the new window:

    JFrame frame = new JFrame("Licence"); 
frame.setSize(500,120); 
frame.setLocationRelativeTo(null); 
frame.setVisible(true);

I know this is a easy question, I just can't think of the correct code for it.

Petzl11
  • 161
  • 1
  • 3
  • 18

4 Answers4

2

You can try something like

JDialog dialog = new JDialog(your_frame_reference, "Licence"); 
dialog .setModal(true); 
dialog .setLocationRelativeTo(null); 
dialog. getContentPane().add(new JLabel(your_text);
dialog .setVisible(true);
Yogesh Patil
  • 908
  • 4
  • 14
1

You can add text by creating JLabels like so:

JLabel label = new JLabel("Hello World");

This can then be added to your JFrame.

Fraser Price
  • 899
  • 6
  • 15
  • 36
1

You can use label

JFrame frame = new JFrame("Licence");
JLabel label = new JLabel("Text-Only Label");
label.setFont(new Font("Serif", Font.PLAIN, 36));
frame.add(label);
Tareq Salah
  • 3,720
  • 4
  • 34
  • 48
  • One more thing, how would I make it so the text wraps? – Petzl11 Dec 18 '13 at 09:37
  • 1
    Check this question, this will help you : http://stackoverflow.com/questions/2420742/make-a-jlabel-wrap-its-text-by-setting-a-max-width – Tareq Salah Dec 18 '13 at 09:40
  • 1
    Shortly you can use html in your string and it will warp it automatically JLabel label = new JLabel("

    Hello World! blah blah blah

    ");
    – Tareq Salah Dec 18 '13 at 09:42
  • Last question, is it possible to add a scroll bar, the licence I have is rather long. – Petzl11 Dec 18 '13 at 09:46
  • 1
    I don't think that it is possible to use scroll in jLabel but if you want to use scroll, I think you can use JTextArae ... here you can see how to use it http://stackoverflow.com/questions/8849063/adding-a-scrollable-jtextarea-java – Tareq Salah Dec 18 '13 at 09:52
  • 1
    http://stackoverflow.com/questions/8849063/adding-a-scrollable-jtextarea-java ... check the first answer – Tareq Salah Dec 18 '13 at 09:53
  • Very last question, in the new window everything is perfect except that people can select and remove sections of the text. – Petzl11 Dec 18 '13 at 10:01
1

Try this Example

import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestDialog {

    protected static void initUI() {
        JPanel pane = newPane("Label in frame");
        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);

    }

    public static JPanel newPane(String labelText) {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(newLabel(labelText));
        pane.add(newButton("Open dialog"), BorderLayout.SOUTH);
        return pane;
    }

    private static JButton newButton(String label) {
        final JButton button = new JButton(label);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Window parentWindow = SwingUtilities.windowForComponent(button);
                JDialog dialog = new JDialog(parentWindow);
                dialog.setLocationRelativeTo(button);
                dialog.setModal(true);
                dialog.add(newPane("Label in dialog"));
                dialog.pack();
                dialog.setVisible(true);
            }
        });
        return button;
    }

    private static JLabel newLabel(String label) {
        JLabel l = new JLabel(label);
        l.setFont(l.getFont().deriveFont(24.0f));
        return l;
    }

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

            @Override
            public void run() {
                initUI();
            }
        });
    }
}
Prateek
  • 6,785
  • 2
  • 24
  • 37