简体   繁体   中英

How to add lines of text to a window in Java swing?

I have a Java class that creates a user interface and a few white boxes, however I want to put a few lines of text about the boxes. I've tried System.out.println("Please select an Area or a Type Of Model from the drop down menus"); but that doesn't work. The code that I have is

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

class MenuActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.out.println("Selected: " + e.getActionCommand());
    }
}

public class testone{    

    public static void main(final String args[]) {
        JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();


        // File Menu, F - Mnemonic
        JMenu fileMenu = new JMenu("Areas");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(fileMenu);

        // File->New, N - Mnemonic
        JMenuItem newMenuItem = new JMenuItem("A1");
        newMenuItem.addActionListener(new MenuActionListener());
        fileMenu.add(newMenuItem);

        newMenuItem = new JMenuItem("A2");
        newMenuItem.addActionListener(new MenuActionListener());
        fileMenu.add(newMenuItem);

        newMenuItem = new JMenuItem("A3");
        newMenuItem.addActionListener(new MenuActionListener());
        fileMenu.add(newMenuItem);

        fileMenu = new JMenu("Types Of Models");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(fileMenu);

        newMenuItem = new JMenuItem("T1");
        newMenuItem.addActionListener(new MenuActionListener());
        fileMenu.add(newMenuItem);

        newMenuItem = new JMenuItem("T2");
        newMenuItem.addActionListener(new MenuActionListener());
        fileMenu.add(newMenuItem);

        newMenuItem = new JMenuItem("T3");
        newMenuItem.addActionListener(new MenuActionListener());
        fileMenu.add(newMenuItem);



        frame.setJMenuBar(menuBar);
        frame.setSize(1200, 650);
        frame.setVisible(true);

        // Set up  subpanels
        JPanel subPanel1 = new JPanel();
        subPanel1.setPreferredSize (new Dimension(135, 135));
        subPanel1.setBackground (Color.white);

        JPanel subPanel2 = new JPanel();
        subPanel2.setPreferredSize (new Dimension(135, 135));
        subPanel2.setBackground (Color.white);

        JPanel subPanel3 = new JPanel();
        subPanel3.setPreferredSize (new Dimension (135, 135));
        subPanel3.setBackground(Color.white);

        JPanel subPanel4 = new JPanel();
        subPanel4.setPreferredSize (new Dimension (135, 135));
        subPanel4.setBackground(Color.white);

        JPanel subPanel5 = new JPanel();
        subPanel5.setPreferredSize (new Dimension (135, 135));
        subPanel5.setBackground(Color.white);

        JPanel subPanel6 = new JPanel();
        subPanel6.setPreferredSize (new Dimension (135, 135));
        subPanel6.setBackground(Color.white);

        JPanel subPanel7 = new JPanel();
        subPanel7.setPreferredSize (new Dimension (135, 135));
        subPanel7.setBackground(Color.white);

        JPanel subPanel8 = new JPanel();
        subPanel8.setPreferredSize (new Dimension (135, 135));
        subPanel8.setBackground(Color.white);

        JPanel subPanel9 = new JPanel();
        subPanel9.setPreferredSize (new Dimension (135, 135));
        subPanel9.setBackground(Color.white);

        JPanel subPanel10 = new JPanel();
        subPanel10.setPreferredSize (new Dimension (135, 135));
        subPanel10.setBackground(Color.white);

        JPanel subPanel11 = new JPanel();
        subPanel11.setPreferredSize (new Dimension (135, 135));
        subPanel11.setBackground(Color.white);

        JPanel subPanel12 = new JPanel();
        subPanel12.setPreferredSize (new Dimension (135, 135));
        subPanel12.setBackground(Color.white);

        JPanel subPanel13 = new JPanel();
        subPanel13.setPreferredSize (new Dimension (135, 135));
        subPanel13.setBackground(Color.white);

        JPanel subPanel14 = new JPanel();
        subPanel14.setPreferredSize (new Dimension (135, 135));
        subPanel14.setBackground(Color.white);

        JPanel subPanel15 = new JPanel();
        subPanel15.setPreferredSize (new Dimension (135, 135));
        subPanel15.setBackground(Color.white);

        JPanel subPanel16 = new JPanel();
        subPanel16.setPreferredSize (new Dimension (135, 135));
        subPanel16.setBackground(Color.white);

        JPanel subPanel17 = new JPanel();
        subPanel17.setPreferredSize (new Dimension (135, 135));
        subPanel17.setBackground(Color.white);

        JPanel subPanel18 = new JPanel();
        subPanel18.setPreferredSize (new Dimension (135, 135));
        subPanel18.setBackground(Color.white);

        JPanel subPanel19 = new JPanel();
        subPanel19.setPreferredSize (new Dimension (135, 135));
        subPanel19.setBackground(Color.white);

        JPanel subPanel20 = new JPanel();
        subPanel20.setPreferredSize (new Dimension (135, 135));
        subPanel20.setBackground(Color.white);

        JPanel subPanel21 = new JPanel();
        subPanel21.setPreferredSize (new Dimension (135, 135));
        subPanel21.setBackground(Color.white);

        JPanel subPanel22 = new JPanel();
        subPanel22.setPreferredSize (new Dimension (135, 135));
        subPanel22.setBackground(Color.white);

        JPanel subPanel23 = new JPanel();
        subPanel23.setPreferredSize (new Dimension (135, 135));
        subPanel23.setBackground(Color.white);

        JPanel subPanel24 = new JPanel();
        subPanel24.setPreferredSize (new Dimension (135, 135));
        subPanel24.setBackground(Color.white);

        JPanel subPanel25 = new JPanel();
        subPanel25.setPreferredSize (new Dimension (135, 135));
        subPanel25.setBackground(Color.white);

        JPanel subPanel26 = new JPanel();
        subPanel26.setPreferredSize (new Dimension (135, 135));
        subPanel26.setBackground(Color.white);

        JPanel subPanel27 = new JPanel();
        subPanel27.setPreferredSize (new Dimension (135, 135));
        subPanel27.setBackground(Color.white);

        JPanel subPanel28 = new JPanel();
        subPanel28.setPreferredSize (new Dimension (135, 135));
        subPanel28.setBackground(Color.white);

        JPanel subPanel29 = new JPanel();
        subPanel29.setPreferredSize (new Dimension (135, 135));
        subPanel29.setBackground(Color.white);

        JPanel subPanel30 = new JPanel();
        subPanel30.setPreferredSize (new Dimension (135, 135));
        subPanel30.setBackground(Color.white);

        JPanel subPanel31 = new JPanel();
        subPanel31.setPreferredSize (new Dimension (135, 135));
        subPanel31.setBackground(Color.white);

        JPanel subPanel32 = new JPanel();
        subPanel32.setPreferredSize (new Dimension (135, 135));
        subPanel32.setBackground(Color.white);

        JPanel subPanel33 = new JPanel();
        subPanel33.setPreferredSize (new Dimension (135, 135));
        subPanel33.setBackground(Color.white);

        JPanel subPanel34 = new JPanel();
        subPanel34.setPreferredSize (new Dimension (135, 135));
        subPanel34.setBackground(Color.white);

        JPanel subPanel35 = new JPanel();
        subPanel35.setPreferredSize (new Dimension (135, 135));
        subPanel35.setBackground(Color.white);

        JPanel subPanel36 = new JPanel();
        subPanel36.setPreferredSize (new Dimension (135, 135));
        subPanel36.setBackground(Color.white);

        // Set up primary panel
        JPanel primary = new JPanel();
        primary.setBackground (Color.lightGray);
        primary.add (subPanel1);
        primary.add (subPanel2);
        primary.add (subPanel3);
        primary.add (subPanel4);
        primary.add (subPanel5);
        primary.add (subPanel6);
        primary.add (subPanel7);
        primary.add (subPanel8);
        primary.add (subPanel9);
        primary.add (subPanel10);
        primary.add (subPanel11);
        primary.add (subPanel12);
        primary.add (subPanel13);
        primary.add (subPanel14);
        primary.add (subPanel15);
        primary.add (subPanel16);
        primary.add (subPanel17);
        primary.add (subPanel18);
        primary.add (subPanel19);
        primary.add (subPanel20);
        primary.add (subPanel21);
        primary.add (subPanel22);
        primary.add (subPanel23);
        primary.add (subPanel24);
        primary.add (subPanel25);
        primary.add (subPanel26);
        primary.add (subPanel27);
        primary.add (subPanel28);
        primary.add (subPanel29);
        primary.add (subPanel30);
        primary.add (subPanel31);
        primary.add (subPanel32);
        primary.add (subPanel33);
        primary.add (subPanel34);
        primary.add (subPanel35);
        primary.add (subPanel36);

        frame.getContentPane().add(primary);
        frame.pack();
        frame.setVisible(true);
    }
}

What I want to put in is some instructions that if people want to use it, then they know what to do without being told all the time.

I want to put a few lines of text about the boxes

I recommend to add a JLabel to the window.

Add this to your code after adding the last subPanel primary.add(subPanel36); :

JPanel container = new JPanel();
JLabel lblAboutBoxes = new JLabel("Please select an Area or a Type Of Model from the drop down menus");
lblAboutBoxes.setHorizontalAlignment(JLabel.CENTER);
lblAboutBoxes.setFont(new Font("Courier New", Font.BOLD, 22));
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
container.add(lblAboutBoxes);
container.add(primary);

And replace

frame.getContentPane().add(primary); 

by

frame.getContentPane().add(container);

Output:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM