2013-06-20 58 views
1

我有几个JPanel需要同时显示。当我按下端口设置按钮时,它们会显示,但我需要在每个面板上方都有一个标题,以便用户知道他们正在选择哪个选项。下面是我的代码以及截图。带标题的JPanels

package myGUI; 

import java.awt.BorderLayout; 
import java.awt.ComponentOrientation; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
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.JComboBox; 
import javax.swing.JDialog; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JFrame; 
import javax.swing.JRadioButton; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 


public class TestApplication implements ActionListener { 

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


    JButton select1 = new JButton("Select"); 
    JButton select2 = new JButton("Select"); 
    JButton select3 = new JButton("Select"); 
    JButton select4 = new JButton("Select"); 


    //Make the drop down lists 
    String[] baudrates = {"57600", "115200", "128000", "256000"}; 
    JComboBox baudlist = new JComboBox(baudrates); 

    String[] baudrates2 = {"57600", "115200", "128000", "256000"}; 
    JComboBox baudlist2 = new JComboBox(baudrates2); 

    String[] bytesizes = {"7", "8"}; 
    JComboBox bytelist = new JComboBox(bytesizes); 

    String[] bytesizes2 = {"7", "8"}; 
    JComboBox bytelist2 = new JComboBox(bytesizes2); 

    String[] stopbit = {"1", "2"}; 
    JComboBox stoplist = new JComboBox(stopbit); 

    String[] stopbit2 = {"1", "2"}; 
    JComboBox stoplist2 = new JComboBox(stopbit2); 

    String[] flows = {"None", "Hardware","Xon", "Xoff"}; 
    JComboBox flowlist = new JComboBox(flows); 

    String[] flows2 = {"None", "Hardware","Xon", "Xoff"}; 
    JComboBox flowlist2 = new JComboBox(flows2); 

    String[] paritys = {"None", "Even", "Odd"}; 
    JComboBox paritylist = new JComboBox(paritys); 

    String[] paritys2 = {"None", "Even", "Odd"}; 
    JComboBox paritylist2 = new JComboBox(paritys2); 


    JLabel ipLabel = new JLabel("IP Address: "); 
    JLabel connectLabel = new JLabel("Connect Time: "); 
    JLabel sendLabel = new JLabel("Send Time Out: "); 
    JLabel receiveLabel = new JLabel("Receive Time Out: "); 
    JLabel portLabel = new JLabel("Port: "); 
    JLabel baudrate = new JLabel("Baud Rate: "); 
    JLabel bytesize = new JLabel("Byte Size: "); 
    JLabel stopbits = new JLabel("Stop Bits: "); 
    JLabel flow = new JLabel("Flow Con...: "); 
    JLabel parity = new JLabel("Parity: "); 
    JLabel stoLabel = new JLabel("Send Time Out: "); 
    JLabel rtoLabel = new JLabel("Receive Time Out: "); 
    JLabel portLabel2 = new JLabel("Port: "); 
    JLabel baudrate2 = new JLabel("Baud Rate: "); 
    JLabel bytesize2 = new JLabel("Byte Size: "); 
    JLabel stopbits2 = new JLabel("Stop Bits: "); 
    JLabel flow2 = new JLabel("Flow Con...: "); 
    JLabel parity2 = new JLabel("Parity: "); 
    JLabel stoLabel2 = new JLabel("Send Time Out: "); 
    JLabel rtoLabel2 = new JLabel("Receive Time Out: "); 
    JLabel portLabel3 = new JLabel("Port: "); 
    JLabel vendor = new JLabel("Vendor ID: "); 
    JLabel product = new JLabel("Product ID: "); 
    JLabel stoLabel3 = new JLabel("Send Time Out: "); 
    JLabel rtoLabel3 = new JLabel("Receive Time Out: "); 

    JLabel logLabel = new JLabel("Input/Output Log"); 


    JTextField ip = new JTextField(10); 
    ip.setText("192.168.0.102"); 

    JTextField ct = new JTextField(10); 
    ct.setText("5000"); 

    JTextField rto = new JTextField(10); 
    rto.setText("5000"); 

    JTextField sto = new JTextField(10); 
    sto.setText("5000"); 

    JTextField port = new JTextField(10); 
    port.setText("12000"); 

    JTextField sendto = new JTextField(10); 
    JTextField reto = new JTextField(10); 
    JTextField comport = new JTextField(10); 
    JTextField sendto2 = new JTextField(10); 
    JTextField reto2 = new JTextField(10); 
    JTextField comport2 = new JTextField(10); 
    JTextField vendorid = new JTextField(10); 
    JTextField productid = new JTextField(10); 
    JTextField sendtime = new JTextField(10); 
    JTextField receiveto = new JTextField(10); 

    JTextArea logbox = new JTextArea() { 
    @Override 
     public java.awt.Dimension getPreferredSize() { 
      return new Dimension(300, 450); 
     }; 
    }; 

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

    logbox.add(logLabel); 



    //Add components to the panels 
    final JPanel ethernetSettings = new JPanel(); 
    ethernetSettings.setLayout(new GridLayout(6, 2)); 
    ethernetSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
    ethernetSettings.add(ipLabel); 
    ethernetSettings.add(ip); 
    ethernetSettings.add(connectLabel); 
    ethernetSettings.add(ct); 
    ethernetSettings.add(receiveLabel); 
    ethernetSettings.add(rto); 
    ethernetSettings.add(sendLabel); 
    ethernetSettings.add(sto); 
    ethernetSettings.add(portLabel); 
    ethernetSettings.add(port); 
    ethernetSettings.add(select1); 

    final JPanel usbHIDSettings = new JPanel(); 
    usbHIDSettings.setLayout(new GridLayout(5, 2)); 
    usbHIDSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
    usbHIDSettings.add(vendor); 
    usbHIDSettings.add(vendorid); 
    usbHIDSettings.add(product); 
    usbHIDSettings.add(productid); 
    usbHIDSettings.add(stoLabel3); 
    usbHIDSettings.add(sendtime); 
    usbHIDSettings.add(rtoLabel3); 
    usbHIDSettings.add(receiveto); 
    usbHIDSettings.add(select2); 


    final JPanel usbCDCSettings = new JPanel(); 
    usbCDCSettings.setLayout(new GridLayout(9, 2)); 
    usbCDCSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
    usbCDCSettings.add(baudrate2); 
    usbCDCSettings.add(baudlist); 
    usbCDCSettings.add(bytesize2); 
    usbCDCSettings.add(bytelist); 
    usbCDCSettings.add(stopbits2); 
    usbCDCSettings.add(stoplist); 
    usbCDCSettings.add(flow2); 
    usbCDCSettings.add(flowlist); 
    usbCDCSettings.add(parity2); 
    usbCDCSettings.add(paritylist); 
    usbCDCSettings.add(stoLabel2); 
    usbCDCSettings.add(sendto2); 
    usbCDCSettings.add(rtoLabel2); 
    usbCDCSettings.add(reto2); 
    usbCDCSettings.add(portLabel3); 
    usbCDCSettings.add(comport2); 
    usbCDCSettings.add(select3); 


    final JPanel rsSettings = new JPanel(); 
    rsSettings.setLayout(new GridLayout(9, 2)); 
    rsSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
    rsSettings.add(baudrate); 
    rsSettings.add(baudlist2); 
    rsSettings.add(bytesize); 
    rsSettings.add(bytelist2); 
    rsSettings.add(stopbits); 
    rsSettings.add(stoplist2); 
    rsSettings.add(flow); 
    rsSettings.add(flowlist2); 
    rsSettings.add(parity); 
    rsSettings.add(paritylist2); 
    rsSettings.add(stoLabel); 
    rsSettings.add(sendto); 
    rsSettings.add(rtoLabel); 
    rsSettings.add(reto); 
    rsSettings.add(portLabel2); 
    rsSettings.add(comport); 
    rsSettings.add(select4); 

    final JPanel PortSettings = new JPanel(); 
    PortSettings.setLayout(new GridLayout(1, 4)); 
    PortSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 
    PortSettings.add(ethernetSettings); 
    PortSettings.add(rsSettings); 
    PortSettings.add(usbCDCSettings); 
    PortSettings.add(usbHIDSettings); 


    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 approvegroup = new ButtonGroup(); 
    approvegroup.add(apprve); 
    approvegroup.add(decline); 

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


    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.setLayout(new BorderLayout()); 
    llpPanel.add(newButtonPanel, BorderLayout.PAGE_START); 
    llpPanel.add(logLabel, BorderLayout.CENTER); 
    llpPanel.add(new JScrollPane(logbox), BorderLayout.PAGE_END); 

    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 

} 






} 

enter image description here

回答

1

为标题上JPanel,您可以使用标题边框,如下。

TitledBorder title = BorderFactory.createTitledBorder("YOUR_TITLE"); 
YOURPANEL.setBorder(title); 
0

我想你要设置在框架上的标题,而不是面板。 JPanels没有标题,但他们的父框架可以。

试试这个:

SwingUtilities.getRoot(yourPanel).setTitle("SomeTitle"); 
+0

请务必阅读[如何使用边框](http://docs.oracle.com/javase/tutorial/uiswing/components/border.html) –

1

试试这个:

myPanel.setBorder(BorderFactory.createTitledBorder("MyTitle"));