簡體   English   中英

JScrollPane不起作用/顯示滾動條

[英]JScrollPane doesn't work/show scroll Bars

我有一個JFrame ,其中包含一個JPanel ,其中包含一個JScrollPane ,該JScrollPane包含另一個JPanel ,具有兩個組件( JPanels )。 出於某種原因,當我使用WindowBuilder的預覽選項查看框架時, JScrollPane顯示了水平滾動條,但是當我編譯並運行該應用程序時卻沒有。 看起來像這樣:

從預覽選項:

編譯時:

在此處輸入圖片說明

這是我的代碼:

package home;

import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.SystemColor;

import javax.swing.JPanel;
import javax.swing.SpringLayout;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.Component;
import java.awt.BorderLayout;
import java.awt.Frame;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.Font;

import javax.swing.BoxLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.ScrollPaneConstants;

public class AtmManeger implements Serializable  {

    private JFrame frmAtmManeger;
    public int NumOfOpenAtmMachines = 0;
    private final AtmManeger frame = this;
    private ArrayList<ATMmachine> ATMs = new ArrayList<ATMmachine>();//Array list of all the ATM machines that were opened
    private AtmAccountDataBase atmDataBase = new AtmAccountDataBase();
    private JLabel contLabel = null;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AtmManeger window = new AtmManeger();
                    window.frmAtmManeger.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * Create the application.
     */
    public AtmManeger getFrame() {
        return this.frame;
    }
    public AtmManeger() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        frmAtmManeger = new JFrame();
        frmAtmManeger.setMinimumSize(new Dimension(615, 420));
        frmAtmManeger.getContentPane().setBackground(SystemColor.activeCaption);
        JPanel mainPanel = new JPanel();
        mainPanel.setPreferredSize(new Dimension(10, 10));
        frmAtmManeger.setContentPane(mainPanel);
        JScrollPane scrollPane = new JScrollPane();
        mainPanel.add(scrollPane, BorderLayout.CENTER);
        JPanel subPanel = new JPanel();
        subPanel.setSize(new Dimension(1190, 350));
        subPanel.setBackground(SystemColor.activeCaption);
        subPanel.setPreferredSize(new Dimension(1190, 350));
        scrollPane.setViewportView(subPanel);
        subPanel.setLayout(new MigLayout("", "[50.00%,grow][50.00%,grow]", "[grow]"));
        JPanel panel = new JPanel();
        subPanel.add(panel,"cell 0 0,grow");
        panel.setBorder(null);
        panel.setBackground(SystemColor.textHighlight);

        panel.setLayout(new MigLayout("", "[100.00%,grow]", "[71px][13.54%][10.09%][41px][][grow]"));

        JPanel panel_1 = new JPanel();
        subPanel.add(panel_1, "cell 1 0,grow");
        frmAtmManeger.setTitle("ATM Maneger");
        frmAtmManeger.setBounds(700, 400, 609, 420);
        frmAtmManeger.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

我制作了另一個與此應用程序非常相似的應用程序,只是一個固有的JFrame 我找不到這段代碼和上面的代碼之間的任何區別。 碼:

package home;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;

import java.awt.Color;
import java.awt.FlowLayout;

import net.miginfocom.swing.MigLayout;

import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JButton;

public class test02 extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test02 frame = new test02();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public test02() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            // Handle exception
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 432, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);

        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(800, 220));
        panel.setBackground(Color.RED);
        scrollPane.setViewportView(panel);
        panel.setLayout(new MigLayout("", "[50.00%,grow][50.00%,grow]", "[grow]"));

        JPanel panel_1 = new JPanel();
        panel.add(panel_1, "cell 0 0,grow");
        panel_1.setLayout(new MigLayout("", "[][][][][][][]", "[][][][][][]"));

        JLabel lblNewLabel = new JLabel("New label");
        panel_1.add(lblNewLabel, "cell 2 1");

        JButton btnNewButton_1 = new JButton("New button");
        panel_1.add(btnNewButton_1, "cell 3 3");

        JButton btnNewButton = new JButton("New button");
        panel_1.add(btnNewButton, "cell 2 5");

        JLabel lblNewLabel_1 = new JLabel("New label");
        panel_1.add(lblNewLabel_1, "cell 6 5");

        JPanel panel_2 = new JPanel();
        panel.add(panel_2, "cell 1 0,grow");
    }

}

因此,這可能只是一個愚蠢的錯誤,但這是什么?

我看不到您更改滾動條策略。 由於沒有溢出,因此在不更改該設置的情況下,您不會看到滾動條出現。

嘗試:

scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

我已經編寫了一個程序來根據要求顯示面板。

在此處輸入圖片說明

碼:

private void createUI() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JScrollPane pane = new JScrollPane();

        JPanel leftPnl = new JPanel(new BorderLayout());
        JPanel rightPnl = new JPanel(new BorderLayout());

        leftPnl.add(new JLabel("Left"), BorderLayout.CENTER);
        leftPnl.setBorder(new LineBorder(Color.black, 5));
        leftPnl.setPreferredSize(new Dimension(400, 400));
        rightPnl.add(new JLabel("Right"), BorderLayout.CENTER);
        rightPnl.setBorder(new LineBorder(Color.black, 5));
        rightPnl.setPreferredSize(new Dimension(400, 400));

        panel.add(leftPnl);
        panel.add(rightPnl);
        panel.add(pane);

        frame.setPreferredSize(new Dimension(400, 400));
        frame.setTitle("ScrollPane Example");
        frame.add(new JScrollPane(panel));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM