0

I am trying to get my JTextArea to display under all other contents of the llpPanel. My code is below with a screenshot of what my code displays. In the code you will see that I have set my dimensions for the JTextArea to (50, 50). Then in the llpPanel I have added BorderLayout.PAGE_END. I have also tried to (instead of PAGE_END) put CENTER and SOUTH. When I put SOUTH, it shows a white line at the very bottom of the program but you cannot do anything with it.

enter image description here

    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.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(1000, 1000);
        frame.setTitle("RBA Test Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        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 approvordecl = new JButton("Approve / Decline");

JTextArea logbox = new JTextArea(50, 50);

        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);
        llpPanel.add(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      
    }
    }

Updated code with screenshot is below...

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.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(1000, 1000);
    frame.setTitle("RBA Test Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);



    JTextArea logbox = new JTextArea(50, 50);




    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 approvordecl = new JButton("Approve / Decline");

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


    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);
    llpPanel.add(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

}






}

screenshot2

4

2 回答 2

2

JPanelsFlowLayout默认情况下使用,因此应用诸如此类BorderLayout的约束PAGE_END将无效。您需要设置面板​​的布局:

llpPanel.setLayout(new BorderLayout());

然后你会遇到组件在BorderLayout.CENTER位置上移动的问题。解决方案是为除on以外的组件创建另一个JPanel容器。logboxllpPanel

JPanel newButtonPanel = new JPanel();
newButtonPanel.add(online);
...
llpPanel.add(newButtonPanel);
JScrollPane scrollPane = new JScrollPane(logbox) {
    @Override
    public java.awt.Dimension getPreferredSize() {
        return new Dimension(500, 500);
    };
};
llpPanel.add(scrollPane, BorderLayout.PAGE_END);

使用 aJScrollPane而不是JTextArea直接添加到容器中。

于 2013-06-04T18:14:33.930 回答
1

设置组件的首选大小属性而不是其大小,并将其添加到 BorderLayout.SOUTH。对于 BorderLayout 布局,容器将尝试为边缘(北、南、东和西)使用首选尺寸,并相应地调整中心的大小。

一个简短的例子来说明。该视图是一个面板,其底部有 50 高的文本区域。这是通过在 BorderLayout.SOUTH 添加 JTextArea 组件并将首选大小属性设置为 Dimension(0,50) 来完成的。视图的其余部分由面板填充。这个面板被放置在 BorderLayout.CENTER 并且将由布局管理器调整大小。

JPanel view = new JPanel( );
view.setSize( 800, 600 );
view.setLayout( new BorderLayout( ) );

JPanel topArea = new JPanel( );
JTextArea textArea = new JTextArea( );
textArea.setPreferredSize( new Dimension( 0, 50 ) );

view.add( topArea, BorderLayout.CENTER );
view.add( textArea, BorderLayout.SOUTH );
于 2013-06-04T19:07:18.117 回答