1

我正在制作给我的应用程序的更好版本。我仍然需要与给我的程序相同的组件,我只需要重新组织它。我还被告知不要复制任何原始代码并从头开始。第一个屏幕截图显示了我需要从原始程序添加到我的程序的文本字段。第二个屏幕截图是我的程序。我不确定 JTextField 是否最适合这个。我的代码如下。我试图添加一个带有 JLabel 的 JTextField 并将其居中,但是当我运行程序时,它会覆盖程序中的所有其他内容,并且只显示一个小白框。

原来的 我的程序

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
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.JTextField;


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);

// Make all necessary buttons
    JButton next = new JButton("Next");

    JButton save = new JButton("Save");
    JButton save2 = new JButton("Save");
    JButton save3 = new JButton("Save");
    JButton save4 = new JButton("Save");

    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");
    JButton ecancel = new JButton("Cancel");
    JButton fcancel = new JButton("Cancel");
    JButton gcancel = new JButton("Cancel");
    JButton hcancel = new JButton("Cancel");

    //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);

    //Make all necessary labels
    JLabel cardLabel = new JLabel("Card Number: ");
    JLabel expLabel = new JLabel("Exp. Date (MM/YY): ");
    JLabel cvvLabel = new JLabel("CVV: ");
    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 amountLabel = new JLabel("Amount: ");
    JLabel textLabel = new JLabel("Display Text: ");

    //Make all necessary TextFields 
    JTextField card = new JTextField(10);
    JTextField expDate = new JTextField(10);
    JTextField cvv = new JTextField(10);
    JTextField ip = new JTextField(10);
    JTextField ct = new JTextField(10);
    JTextField rto = new JTextField(10);
    JTextField sto = new JTextField(10);
    JTextField port = new JTextField(10);
    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);
    JTextField amountbox = new JTextField(10);
    JTextField textBox = new JTextField(10);


    //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(save);
    ethernetSettings.add(ecancel);

    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(save4);
    usbHIDSettings.add(hcancel);

    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(save3);
    usbCDCSettings.add(gcancel);

    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(save2);
    rsSettings.add(fcancel);

    JRadioButton ethernet = new JRadioButton("Ethernet");
    ethernet.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JDialog esettings = new JDialog(frame);
            esettings.setTitle("Ethernet Settings");
            esettings.add(ethernetSettings);
            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.add(rsSettings);
            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.add(usbCDCSettings);
            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.add(usbHIDSettings);
            usbh.setSize(400, 400);
            usbh.pack();
            usbh.setVisible(true);

        }
    });

    final JPanel PortSettings = new JPanel();
    PortSettings.setLayout(new GridLayout(3, 4));
    PortSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    PortSettings.add(ethernet);
    PortSettings.add(rs);
    PortSettings.add(usbcdc);
    PortSettings.add(usbhid);
    PortSettings.add(next);
    PortSettings.add(bcancel);

    final JPanel accountPanel = new JPanel();
    accountPanel.setLayout(new GridLayout(4, 2));
    accountPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    accountPanel.add(cardLabel);
    accountPanel.add(card);
    accountPanel.add(expLabel);
    accountPanel.add(expDate);
    accountPanel.add(cvvLabel);
    accountPanel.add(cvv);
    accountPanel.add(bok);
    accountPanel.add(ccancel);

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


    final JPanel apprvordecl = new JPanel();
    apprvordecl.setLayout(new GridLayout(3, 2));
    apprvordecl.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    apprvordecl.add(apprve);
    apprvordecl.add(decline);
    apprvordecl.add(textLabel);
    apprvordecl.add(textBox);
    apprvordecl.add(aok);
    apprvordecl.add(acancel);

    final JPanel amountPanel = new JPanel();
    amountPanel.setLayout(new GridLayout(2, 2));
    amountPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    amountPanel.add(amountLabel);
    amountPanel.add(amountbox);
    amountPanel.add(cok);
    amountPanel.add(dcancel);

    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

}






}
4

1 回答 1

1

去做 JTextArea

JTextArea 是一个显示纯文本的多行区域。

http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JTextArea.html

对于所需的布局,请参阅Oracle 的布局管理器

于 2013-05-31T15:02:59.437 回答