JScrollPane只有垂直和调整内容水平

时间:2015-12-14 13:29:34

标签: jscrollpane

我正在使用当前最新的JRE jre1.8.0_66。

我的代码存在以下问题。我有一个带有BoxLayout(Y_AXIS)的jpanel到JScrollPane中,当我调整窗口大小时,我希望我的BoxLayout宽度总是适合可用空间的大小。

这是一些可以更好地解释我的问题的照片:

当消息到达可用的垂直空间时出现第一个问题,滚动条出现但是...我更喜欢调整面板一点来纠正水平尺寸,不显示水平滚动条,我只需要垂直条继续阅读所有消息。

这不是一个真正的问题,因为我可以使用垂直滚动条的策略来解决它。但如果可以的话,我更喜欢不要一直显示滚动条。

Minor Problem: When the scroll Appear

当我调整窗口大小时会出现真正的问题。如果我让窗户更大,一切都没问题,但......当我再次制作它时,水平尺寸继续相同的大小,我无法调整大小。

Big Problem: Resize width when I do less wide

我尝试了很多东西,听众在调整组件大小时会听,但是,我无法解决它。

这些是我的课程:

的MainView

lsof

ScrollPaneColor 这是ScrollPane无法正常工作。我只想要垂直滚动,而不是水平滚动。在这个类中,我有一个JPanel内容,在MainView中定义了一个BoxLayout(Y_AXIS)来添加我想要垂直滚动的内容。

我尝试了很多东西来解决我的问题,我做一个监听器调整大小,如果我做setHorizo​​ntalScrollBarPolicy(HORIZONTAL_SCROLLBAR_​​NEVER);我的问题比现在还要严重。

package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class MainView extends JFrame{
    private JPanel content;
    private ScrollPaneColor scrollPane;

    public MainView() {
        getContentPane().setLayout(new BorderLayout(0, 0));

        /*
         * Content Panel to scrollPane
         */
        content = new JPanel();
        content.setBackground(backgroundColor);
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));

        /*
         * scrollPane
         */
        scrollPane = new ScrollPaneColor(content, backgroundColor, grey);

        /*
         * Action Panel to add more content
         */
        JPanel actions = new JPanel();
        getContentPane().add(actions, BorderLayout.SOUTH);
        actions.setLayout(new GridLayout(1, 0, 0, 0));

        JButton btnAddBlue = new JButton("add Blue");
        btnAddBlue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                MessageFor blue = new MessageFor("This is just a test to see how the ScrollPane interacts with the view ", backgroundColor, paletteBlue);
                content.add(blue);
                content.updateUI();
            }
        });
        actions.add(btnAddBlue);

        JButton btnAddRed = new JButton("add Red");
        btnAddRed.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                MessageFor red = new MessageFor("This is just a test to see how the ScrollPane interacts with the view ", backgroundColor, paletteRed);
                content.add(red);
                content.updateUI();
            }
        });
        actions.add(btnAddRed);

        /*
         * Nothing important, only the Split Panel
         */
        JPanel panel = new JPanel();
        panel.setBackground(Color.BLACK);
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel,scrollPane);
        splitPane.setOneTouchExpandable(true);
        splitPane.setDividerLocation(150);
        getContentPane().add(splitPane, BorderLayout.CENTER);

        /*
         * Nothing important, only the Title Panel
         */
        JPanel title = new JPanel();
        getContentPane().add(title, BorderLayout.NORTH);

        JLabel lblNewLabel = new JLabel("title of actions");
        title.add(lblNewLabel);
    }

    /*
     * Nothing important, only the MAIN to run the program.
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MainView view = new MainView();
        view.setVisible(true);
        view.setSize(900, 450);
        view.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    /*
     * Nothing important, only the PaletteColors and backgroundColor to decorate.
     */
    private PaletteColor grey = new PaletteColor(
            new Color(0xBFBFBF),
            new Color(0xD8D8D8), new Color(0xF2F2F2), new Color(0xFFFFFF),
            new Color(0xA5A5A5), new Color(0x7F7F7F),
            new Color(0x000000), new Color(0x000000), new Color(0xFFFFFF));

    private PaletteColor paletteRed = new PaletteColor(
            new Color(0xDA1F28),
            new Color(0xEB757B), new Color(0xF2A3A7), new Color(0xF8D1D3),
            new Color(0xA3171E), new Color(0x6D0F14),
            new Color(0xFFFFFF), new Color(0x000000), new Color(0xFFFFFF));

    private PaletteColor paletteBlue = new PaletteColor(
            new Color(0x4F81BD),
            new Color(0x95B3D7), new Color(0xB8CCE4), new Color(0xDBE5F1),
            new Color(0x366092), new Color(0x244061),
            new Color(0xFFFFFF), new Color(0x000000), new Color(0xFFFFFF));

    private Color backgroundColor = Color.WHITE; 
}

MessageFor 带有x消息的JPanel,带有背景和调色板的颜色。这个JPanel很多次都会添加到ScrollPaneColor中,我们需要垂直滚动才能继续阅读它。

package view;

import java.awt.Color;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 * Define a JScrollPane with the colors of the palette.
 * 
 * @author Jorge Dacal Cantos
 *
 */
public class ScrollPaneColor extends JScrollPane {
    private JPanel content;

    /**
     * Create a <code>JScrollPane</code> with scrollbars  as needed and
     * personal colors
     * 
     * @see #setViewportView
     * @param panel  The component to display in the scrollpane's viewport
     * @param background  The color for panel background.
     * @param palette  Palette with colors for scrollbars.
     * @author Jorge Dacal Cantos
     */ 
    public ScrollPaneColor(JPanel panel, Color background, PaletteColor palette) {
        super(panel);
        content = panel;
        setBackground(background);
        setBorder(null);
        //setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
        //setScrollsOnExpand(false);
        /*
         * irrelevant code, only changes the shape and color of the scrollBar
         *
        JScrollBar sb = getVerticalScrollBar();
        sb.setUI(new ScrollBarColorUI(palette));
        sb = getHorizontalScrollBar();
        sb.setUI(new ScrollBarColorUI(palette));
         */
        addComponentListener(new resizeListener());
    }

    /*
     * listener to try to fix my problem resizing.
     */
    class resizeListener extends ComponentAdapter {
        public void componentResized(ComponentEvent e) {
            //Recalculate the variable you mentioned
            /*
            System.out.println("I'm on resizeListener");
            int width = getWidth();
            int height = getHeight();
            System.out.println("srollpane w: " + width + " scrollpane h: " + height);
            System.out.println("content w: " + content.getWidth() + " content h: " + content.getHeight());
            content.setSize(width, height);
            content.getParent().setSize(width, height);
            //content.revalidate();
            System.out.println("content w: " + content.getWidth() + " content h: " + content.getHeight());
            content.repaint();
            */
        }
    }
}

PaletteColor 仅用于装饰,但需要它才能尝试

package view;

import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.border.CompoundBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 * Define a <code>JPanel</code> with:
 * 
 *   * NORTH: <code>JPanel</code> panelNorth with BorderLayout:
 *      * CENTER: JLabel With date.
 *      * WEST: <code>JPanel</code> for Margin.
 *   * CENTER: <code>TextArea</code> with message for.
 *   * WEST: <code>JPanel</code> for Margin.
 * 
 * @author Jorge Dacal Cantos
 */
public class MessageFor extends JPanel {

    JLabel labelDate;

    /**
     * Create a <code>JPanel</code> with the message for, with the colors
     * of the background and palette.
     * 
     * @form message  Message to show.
     * @form background  Background color of the panel.
     * @form palette  Colors for the message.
     * @author Jorge Dacal Cantos
     */
    public MessageFor(String message, Color background, PaletteColor palette) {
        setBorder(new LineBorder(background, 10));
        setBackground(background);
        setLayout(new BorderLayout(0, 0));

        JTextArea textAreaMensaje = new JTextArea(message);
        textAreaMensaje.setColumns(40);
        textAreaMensaje.setLineWrap(true);
        BevelBorder borderTextExterior = new BevelBorder(BevelBorder.RAISED, palette.getAclaradoNv2(), palette.getAclaradoNv2(), palette.getEstandar(), palette.getEstandar());
        LineBorder borderTextInterior = new LineBorder(palette.getAclaradoNv1(), 10, true);
        textAreaMensaje.setBorder(new CompoundBorder(borderTextExterior, borderTextInterior));
        textAreaMensaje.setEditable(false);
        textAreaMensaje.setBackground(palette.getAclaradoNv1());
        textAreaMensaje.setForeground(palette.getTextoEnAclarado());
        textAreaMensaje.setSelectedTextColor(palette.getTextoEnEstandar());
        textAreaMensaje.setSelectionColor(palette.getEstandar());
        add(textAreaMensaje);

        JPanel panelMarginWest = new JPanel();
        FlowLayout flowLayout = (FlowLayout) panelMarginWest.getLayout();
        flowLayout.setHgap(25);
        panelMarginWest.setBackground(background);
        add(panelMarginWest, BorderLayout.WEST);

        JPanel panelNorth = new JPanel();
        add(panelNorth, BorderLayout.NORTH);
        panelNorth.setLayout(new BorderLayout(0, 0));

        JPanel panelDate = new JPanel();
        panelNorth.add(panelDate);
        panelDate.setBackground(background);
        Calendar date = Calendar.getInstance();
        SimpleDateFormat formato = new SimpleDateFormat("hh:mm");
        String DateWithFormat = formato.format(date.getTime());
        panelDate.setLayout(new BorderLayout(0, 0));
        labelDate = new JLabel(DateWithFormat);
        labelDate.setHorizontalAlignment(SwingConstants.LEFT);
        labelDate.setForeground(palette.getEstandar());
        panelDate.add(labelDate);

        JPanel panelMarginDate = new JPanel();
        panelMarginDate.setBackground(background);
        panelNorth.add(panelMarginDate, BorderLayout.WEST);
        panelMarginDate.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 5));
    }

    /**
     * to add name next the date.
     * 
     * @form name  Name to show with the Date.
     */
    public void especificarname(String name){
        name = labelDate.getText() + " - for " + name;
        labelDate.setText(name);
    }
}

我希望你能帮助我,因为我已经不知道还有什么可以尝试的。我在这里和互联网上读过很多东西,没有什么能解决我的问题。

很抱歉我的问题很长,非常感谢大家。

最好的问候,豪尔赫

0 个答案:

没有答案
相关问题