将JTextArea插入带有JLabel的JPanel [英] Insert JTextArea into a JPanel with a JLabel

查看:181
本文介绍了将JTextArea插入带有JLabel的JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示我发布的图纸。当我的代码运行并且用户单击帐户时,面板仅显示确定和取消按钮(参见屏幕截图)。我已经为面板accountPanel添加了三个带有JLabel的JTextAreas,但是它们都没有显示。我的代码如下。

I am trying to display the drawing that I have posted. When my code runs and the user clicks Account, the panel only displays the buttons "OK" and "Cancel" (see screenshot). I have added three JTextAreas with a JLabel for each to the panel accountPanel but they don't display. My code is below.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


public class TestApplication implements ActionListener {

    public static void main(String[] args) {
    JLabel input = new JLabel();
    final JFrame frame = new JFrame();
    frame.setSize(1000, 1000);
    frame.setTitle("RBA Test Application");
    frame.add(input);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    JRadioButton apprve = new JRadioButton("Approve");
    JRadioButton decline = new JRadioButton("Decline");

    JRadioButton ethernet = new JRadioButton("Ethernet");
    ethernet.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JDialog esettings = new JDialog(frame);
            esettings.setTitle("Ethernet Settings");
            esettings.setSize(400, 400);
            esettings.pack();
            esettings.setVisible(true);

        }
    });

    JRadioButton rs = new JRadioButton("RS232");
    rs.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JDialog rsettings = new JDialog(frame);
            rsettings.setTitle("RS232 Settings");
            rsettings.setSize(400, 400);
            rsettings.pack();
            rsettings.setVisible(true);

        }
    });

    JRadioButton usbcdc = new JRadioButton("USB_CDC");
    usbcdc.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JDialog usbc = new JDialog(frame);
            usbc.setTitle("USB_CDC Settings");
            usbc.setSize(400, 400);
            usbc.pack();
            usbc.setVisible(true);

        }
    });

    JRadioButton usbhid = new JRadioButton("USB_HID");
    usbhid.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JDialog usbh = new JDialog(frame);
            usbh.setTitle("USB_HID Settings");
            usbh.setSize(400, 400);
            usbh.pack();
            usbh.setVisible(true);

        }
    });



    JButton next = new JButton("Next");
    JButton aok = new JButton("OK");
    JButton bok = new JButton("OK");
    JButton cok = new JButton("OK");
    JButton acancel = new JButton("Cancel");
    JButton bcancel = new JButton("Cancel");
    JButton ccancel = new JButton("Cancel");
    JButton dcancel = new JButton("Cancel");


    JLabel cardLabel = new JLabel("Card Number: ");
    JLabel expLabel = new JLabel("Exp. Date: ");
    JLabel cvvLabel = new JLabel("CVV: ");

    JTextArea card = new JTextArea();
    card.add(cardLabel);

    JTextArea expDate = new JTextArea();
    expDate.add(expLabel);

    JTextArea cvv = new JTextArea();
    cvv.add(cvvLabel);


    final JPanel PortSettings = new JPanel();
    PortSettings.add(ethernet);
    PortSettings.add(rs);
    PortSettings.add(usbcdc);
    PortSettings.add(usbhid);
    PortSettings.add(next);
    PortSettings.add(bcancel);


    final JPanel accountPanel = new JPanel();
    accountPanel.add(bok);
    accountPanel.add(ccancel);
    accountPanel.add(card);
    accountPanel.add(expDate);
    accountPanel.add(cvv);



    final JPanel apprvordecl = new JPanel();
    apprvordecl.add(apprve);
    apprvordecl.add(decline);
    apprvordecl.add(aok);
    apprvordecl.add(acancel);

    final JPanel amountPanel = new JPanel();
    amountPanel.add(cok);
    amountPanel.add(dcancel);


    input.setFont(new java.awt.Font("Tahoma", 3, 18)); 
    input.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    input.setText("Input / Output Log");



    JButton initialize = new JButton("Initialize");

    JButton connect = new JButton("Connect");

    JButton disconnect = new JButton("Disconnect");

    JButton shutdown = new JButton("Shut Down");


    JButton portsettings = new JButton("Port Settings");
     portsettings.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JDialog port = new JDialog(frame);
                port.setTitle("Port Settings");
                port.setSize(400, 400);
                port.add(PortSettings);
                port.pack();
                port.setVisible(true);

            }
        });

    JButton online = new JButton("Go Online");

    JButton offline = new JButton("Go Offline");

    JButton status = new JButton("Status");

    JButton reboot = new JButton("Reboot");


    JButton account = new JButton("Account");
     account.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JDialog accountDialog = new JDialog(frame);
                accountDialog.setTitle("Account");
                accountDialog.setSize(400, 400);
                accountDialog.add(accountPanel);
                accountDialog.pack();
                accountDialog.setVisible(true);



            }
        });

    JButton amount = new JButton("Amount");
     amount.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JDialog amount2 = new JDialog(frame);
                amount2.setTitle("Amount");
                amount2.setSize(400, 400);
                amount2.add(amountPanel);
                amount2.pack();
                amount2.setVisible(true);



            }
        });

    JButton reset = new JButton("Reset");


    JButton approvordecl = new JButton("Approve / Decline");
     approvordecl.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JDialog apprv = new JDialog(frame);
                apprv.setTitle("Approve / Decline");
                apprv.setSize(400, 400);
                apprv.add(apprvordecl);
                apprv.pack();
                apprv.setVisible(true);

            }
        });
    JButton test = new JButton("Test Button #1");

    JButton testing = new JButton("Test Button #2");

    JRadioButton button = new JRadioButton("Radio Button");

    JRadioButton button2 = new JRadioButton("Radio Button");

    JCheckBox checkbox = new JCheckBox("Check Box");

    JCheckBox checkbox2 = new JCheckBox("Check Box");

    ButtonGroup group = new ButtonGroup();
    group.add(usbhid);
    group.add(usbcdc);
    group.add(ethernet);
    group.add(rs);

    ButtonGroup approvegroup = new ButtonGroup();
    approvegroup.add(apprve);
    approvegroup.add(decline);


    JPanel testPanel = new JPanel();
    testPanel.add(button);
    testPanel.add(button2);
    testPanel.add(checkbox2);

    JPanel posPanel = new JPanel();
    posPanel.add(test);
    posPanel.add(testing);
    posPanel.add(checkbox);

    JPanel llpPanel = new JPanel();
    llpPanel.add(online);
    llpPanel.add(offline);
    llpPanel.add(status);
    llpPanel.add(reboot);
    llpPanel.add(account);
    llpPanel.add(amount);
    llpPanel.add(reset);
    llpPanel.add(approvordecl);

    JPanel buttonPanel = new JPanel();
    buttonPanel.add(initialize);
    buttonPanel.add(connect);
    buttonPanel.add(disconnect);
    buttonPanel.add(shutdown);
    buttonPanel.add(portsettings);
    frame.add(buttonPanel);
    frame.add(buttonPanel, BorderLayout.NORTH);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
    tabbedPane.addTab("Test", null, testPanel, "Test");

    JPanel tabsPanel = new JPanel(new BorderLayout());
    tabsPanel.add(tabbedPane);
    frame.add(tabsPanel, BorderLayout.CENTER);


    frame.pack();



}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}






}


推荐答案

JPanel 使用 FlowLayout 默认情况下,它尊重首选尺寸。 JTextArea的的首选大小的默认宽度为 0 x 0 。您需要为 JTextComponent的提供首选大小才能显示。使用构造函数指定行和&列:

JPanel uses FlowLayout by default which respects preferred sizes. The default width of the preferred size for the JTextArea's is 0 x 0. You need to give the JTextComponent's a preferred size for them to appear. Use the constructor that specifys rows & columns:

JTextArea card = new JTextArea(5, 10);

别忘了制作 JTextArea 可以通过将其封装在 JScrollPane 中来滚动:

Don't forget to make the JTextArea scrollable by enclosing it in a JScrollPane:

accountPanel.add(new JScrollPane(card));

这篇关于将JTextArea插入带有JLabel的JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆