2013-05-30 42 views
1

我正在尝试显示已发布的绘图。当我的代码运行并且用户单击帐户时,面板只显示按钮“确定”和“取消”(请参阅​​屏幕截图)。我已经为每个面板的accountPanel添加了三个带有JLabel的JTextAreas,但它们不显示。我的代码如下。将JTextArea插入带有JLabel的JPanel

Drawing

Screenshot

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 

} 






} 
+0

我会建议你使用[的WindowBuilder(HTTPS: //developers.google.com/java-dev-tools/wbpro/quick_start)来创建GUI。 – Kai

+0

我会,但我是一名实习生,我不能使用它。 :( –

+2

@ user714965但是在使用这类工具之前,最好先掌握一些概念..... – pinkpanther

回答

2

JPanel使用FlowLayout默认哪些方面优选的尺寸。 JTextArea's的首选尺寸的默认宽度为0 x 0。您需要给JTextComponent's一个首选大小以显示它们。使用specifys行&列constructor

JTextArea card = new JTextArea(5, 10); 

不要忘记围住后,使JTextArea滚动一个JScrollPane

accountPanel.add(new JScrollPane(card)); 
+0

但是,我怎样才能标记每一个像我需要? –

+0

你有所有的变量作为局部变量。改为创建变量类成员变量。然后你可以根据需要使用任何成员变量的引用 – Reimeus

+0

好的非常感谢你! –