将JTextArea插入带有JLabel的JPanel

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

画画

截图

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's首选大小的默认宽度为0 x 0 。 您需要为JTextComponent's提供首选大小才能显示它们。 使用指定行和列的构造函数 :

 JTextArea card = new JTextArea(5, 10); 

不要忘记通过将JTextArea包含在JScrollPane来使其可滚动:

 accountPanel.add(new JScrollPane(card));