2013-05-30 84 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

好的非常感謝你! –