2

old I have attached my code below, a screen shot of my program, and a drawing of what I want the tab to look like. I need my JPanel textPanel to appear below the JButtons on the tab LLP. I have tried to add textPanel to the tabsPanel but when I do that my tabs go away. I need the textPanel to stretch across the screen and fill the blank space below the LLP tab. But I also do not want textPanel to appear on the other tabs. The first picture shown is what the old program looked like. I was asked to start from scratch and make a better program. I cannot get the Input Output Log on my screen. But as you can see it displays what I have selected in the Port Settings option.Screen Shot of ProgramDrawing

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.TextArea;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;


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

        JLabel logLabel = new JLabel("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");
        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");
        JButton amount = new JButton("Amount");
        JButton reset = new JButton("Reset");
        JButton apprvordecl = new JButton("Apprv/Decl");
        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");

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

    JPanel textPanel = new JPanel(new BorderLayout());
    textPanel.add(logLabel);
    frame.add(logLabel); 

    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

3 回答 3

3

Hacking together a Swing GUI doesn't work very well. My suggestion is to go through the Oracle Swing Tutorial and learn some concepts that you can put together when hacking. Knowing the Swing components that are available for you to use, and knowing the Swing layout managers. Especially knowing the Swing layout managers.

Here's the GUI I produced.

RBM Test Application

The GUI is more complicated than it looks at first glance. There were many JPanels inside of JPanels. I used a few FLowLayouts, a couple of BoxLayouts, and a GridBagLayout.

Here's a tree diagram of the major JPanels that make up this GUI.

Main JFrame
    Main JPanel
        Communication JPanel
            First Button Row JPanel
            Second Button Row JPanel
        JTabbedPane
            LLP Tab JPanel
                Log JPanel

You should always have one main JPanel to hold all your other components.

That's one of the things that comes from experience with Swing. You start to see the rectangles that make up the various parts of the GUI.

I don't usually code this complicated a GUI in one class. I did so this time so that I'd have an SSCCE to post along with this explanation.

Study the code to see how that various JPanels and Swing components fit together.

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

public class RBMTestApplication implements Runnable {

    private static final Insets SPACE_INSETS    = new Insets(10, 10, 4, 10);

    private JFrame              frame;

    private JPanel              mainPanel;
    private JPanel              communicationPanel;
    private JPanel              cPanel1;
    private JPanel              cPanel2;
    private JPanel              llpPanel;

    private JTabbedPane         tabbedPane;

    @Override
    public void run() {
        createFrame();
    }

    private void createFrame() {
        frame = new JFrame();
        frame.setTitle("RBM Test Application");
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent event) {
                exitProcedure();
            }
        });
        createMainPanel();
        frame.add(mainPanel);
        frame.pack();
        frame.setVisible(true);
    }

    public void exitProcedure() {
        frame.dispose();
        System.exit(0);
    }

    private void createMainPanel() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        createCommunicationPanel();
        mainPanel.add(communicationPanel);
        createTabbedPane();
        mainPanel.add(tabbedPane);
    }

    private void createCommunicationPanel() {
        communicationPanel = new JPanel();
        Border border = BorderFactory.createTitledBorder("Communication");
        communicationPanel.setBorder(border);
        communicationPanel.setLayout(new BoxLayout(communicationPanel,
                BoxLayout.Y_AXIS));

        cPanel1 = new JPanel();

        JButton initializeButton = new JButton("Initialize");
        cPanel1.add(initializeButton);

        JButton connectButton = new JButton("Connect");
        cPanel1.add(connectButton);

        JButton disconnectButton = new JButton("Disconnect");
        cPanel1.add(disconnectButton);

        JButton shutdownButton = new JButton("Shutdown");
        cPanel1.add(shutdownButton);

        communicationPanel.add(cPanel1);

        cPanel2 = new JPanel();

        JButton portSettingsButton = new JButton("Port Settings");
        portSettingsButton.setHorizontalAlignment(JButton.CENTER);
        cPanel2.add(portSettingsButton);

        communicationPanel.add(cPanel2);
    }

    private void createTabbedPane() {
        tabbedPane = new JTabbedPane();
        createLLPPanel();
        tabbedPane.addTab("LLP", llpPanel);
        // tabbedPane.addTab("POS", posPanel);
        // tabbedPane.addTab("Tere", terePanel);
    }

    private void createLLPPanel() {
        llpPanel = new JPanel();
        llpPanel.setLayout(new GridBagLayout());

        int gridy = 0;

        JButton goOnlineButton = new JButton("Go online");
        addComponent(llpPanel, goOnlineButton, 0, gridy, 1, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JButton statusButton = new JButton("Status");
        addComponent(llpPanel, statusButton, 1, gridy, 1, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JButton pccountButton = new JButton("PC count");
        addComponent(llpPanel, pccountButton, 2, gridy, 1, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JButton resetButton = new JButton("Reset");
        addComponent(llpPanel, resetButton, 3, gridy++, 1, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JButton goOfflineButton = new JButton("Go offline");
        addComponent(llpPanel, goOfflineButton, 0, gridy, 1, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JButton rebootButton = new JButton("Reboot");
        addComponent(llpPanel, rebootButton, 1, gridy, 1, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JButton amountButton = new JButton("Amount");
        addComponent(llpPanel, amountButton, 2, gridy, 1, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JButton approveButton = new JButton("Approve / Decline");
        addComponent(llpPanel, approveButton, 3, gridy++, 1, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JPanel logPanel = new JPanel();
        Border border = BorderFactory.createTitledBorder("Input / output log");
        logPanel.setBorder(border);

        JTextPane textPane = new JTextPane();
        textPane.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(textPane);
        scrollPane.getViewport().setPreferredSize(new Dimension(400, 150));

        logPanel.add(scrollPane);

        addComponent(llpPanel, logPanel, 0, gridy++, 4, 1, SPACE_INSETS,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
    }

    private void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
            int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
        container.add(component, gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new RBMTestApplication());
    }

}

You should always start a Swing application with a call to the SwingUtilities invokeLater method. This ensures that the Swing Components are created and updated on the Event Dispatch thread.

于 2013-05-22T19:39:41.007 回答
0

Have a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

You have to set a suitable LayoutManager for your needs, otherwise the default LayoutManager is used that probably doesn't fit.

于 2013-05-22T18:55:46.950 回答
0

You need another nested panel, try this:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


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

    JLabel logLabel = new JLabel("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");
    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");
    JButton amount = new JButton("Amount");
    JButton reset = new JButton("Reset");
    JButton apprvordecl = new JButton("Apprv/Decl");
    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");

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

//CHANGED PART
JPanel llpPanel = new JPanel(new BorderLayout());
JPanel llpbuttonPanel = new JPanel();
llpbuttonPanel.add(online);
llpbuttonPanel.add(offline);
llpbuttonPanel.add(status);
llpbuttonPanel.add(reboot);
llpbuttonPanel.add(account);
llpbuttonPanel.add(amount);
llpbuttonPanel.add(reset);
llpbuttonPanel.add(apprvordecl);
llpPanel.add(llpbuttonPanel, BorderLayout.NORTH);

JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel);
llpPanel.add(textPanel, BorderLayout.CENTER);
//END CHANGED PART

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

}






}

To put in a correct text area: (you will want to create and reference the text area in a different way)

    //BEGIN CHANGE
JPanel llpPanel = new JPanel(new BorderLayout());
JPanel llpbuttonPanel = new JPanel();
llpbuttonPanel.add(online);
llpbuttonPanel.add(offline);
llpbuttonPanel.add(status);
llpbuttonPanel.add(reboot);
llpbuttonPanel.add(account);
llpbuttonPanel.add(amount);
llpbuttonPanel.add(reset);
llpbuttonPanel.add(apprvordecl);
llpPanel.add(llpbuttonPanel, BorderLayout.NORTH);

JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel, BorderLayout.NORTH);

JTextArea area = new JTextArea();
textPanel.add(area, BorderLayout.CENTER);
llpPanel.add(textPanel, BorderLayout.CENTER);

//END CHANGE
于 2013-05-22T18:56:16.417 回答