DesignGridLayout调整JButtons的大小

时间:2014-05-11 04:48:55

标签: java swing awt layout-manager

我一直在玩DesignGridLayout作为布局管理员。在大多数情况下,它运作得相当好。但是,我在此示例类中遇到了一个问题:

import net.java.dev.designgridlayout.DesignGridLayout;

import javax.swing.*;
import java.awt.*;

public class ConsolePanel extends JPanel {
    private ConButton oneCon = new ConButton(">");
    private ConButton twoCon = new ConButton(">");
    private ConButton threeCon = new ConButton(">");
    private ConButton fourCon = new ConButton("<");
    private ConButton fiveCon = new ConButton("<");
    private ConButton sixCon = new ConButton("<");
    private JTextArea console = new JTextArea(10,20);

    private Dimension dim = new Dimension(430,250);

    private DesignGridLayout layout = new DesignGridLayout(this);

    public ConsolePanel () {
        buildConsole();

    }

    private void buildConsole () {

        layout.row().grid().add(oneCon).add(console,3).add(fourCon);
        layout.row().grid().add(twoCon).spanRow().add(fiveCon);
        layout.row().grid().add(threeCon).spanRow().add(sixCon);

        console.setBackground(Color.BLACK);
        console.setForeground(Color.GREEN);
        console.setColumns(20);
        console.setRows(10);
        console.setLineWrap(true);
        console.setWrapStyleWord(true);
        console.setEditable(false);
    }

    @Override
    public Dimension getPreferredSize() {
        // comply to contract if set
        if(isPreferredSizeSet())
            return super.getPreferredSize();
        // do whatever we want
        return new Dimension(dim);
    }

    public JPanel getConsoleLayout() {
        return this;
    }

    private class ConButton extends JButton {
        private Dimension dim = new Dimension(10,25);

        public ConButton (String text) {
            super(text);
        }

        @Override
        public Dimension getPreferredSize() {
            // comply to contract if set
            if(isPreferredSizeSet())
                return super.getPreferredSize();
            // do whatever we want
            return new Dimension(dim);
        }
    }
}

我需要能够调整&#34;&lt;&#34;和&#34;&gt;&#34;到一些较小的尺寸。然后布局管理器默认为什么。我试图覆盖此处#21052894所述的getPreferredSize()方法。虽然,这不是很正常。

此外,这是JFrame / JPanel内的第二个JPanel,例如

---------------------------------------------&lt; - JFrame内的JPanel | | | JPanel2 | JPanel3 | | |一个我无法得到 | |对|

JFrame / JPanel的设置如下:

_frame = new JFrame();
_frame.setName(getClass().getSimpleName());
_frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


Font titleFont = new Font(title.getFont().getName(), title.getFont().getStyle(), 16);
title.setFont(titleFont);

JPanel top = new JPanel();
top.setName("TOP");
top.setLayout(new BorderLayout());
top.add(title,BorderLayout.PAGE_START);
top.add(<Panel2>, BorderLayout.LINE_START);
top.add(<Panel3>, BorderLayout.LINE_END);
addTopPanel(top);
prePack();
_frame.pack();
_frame.setLocationRelativeTo(null);
_frame.setResizable(false);
_frame.setVisible(true);

1 个答案:

答案 0 :(得分:0)

这对我来说似乎不起作用。我从JTextArea更改为JTextPane,现在使用正确的布局。这是当前的代码:

import net.java.dev.designgridlayout.DesignGridLayout;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class ConsolePanel extends JPanel {
    private JButton oneCon = new JButton(">");
    private JButton twoCon = new JButton(">");
    private JButton threeCon = new JButton(">");
    private JButton fourCon = new JButton("<");
    private JButton fiveCon = new JButton("<");
    private JButton sixCon = new JButton("<");
    private ConJTextPane console = new ConJTextPane();

    private Dimension dim = new Dimension(430,250);

    private DesignGridLayout layout = new DesignGridLayout(this);
    private AbstractDocument doc;

    public ConsolePanel () {
        buildConsole();

    }

    private void buildConsole () {
        StyledDocument styledDoc = console.getStyledDocument();
        if (styledDoc instanceof AbstractDocument) {
            doc = (AbstractDocument)styledDoc;
            doc.setDocumentFilter(new DocumentSizeFilter());
        }

        layout.row().grid().add(oneCon).add(console,3).add(fourCon);
        layout.row().grid().add(twoCon).spanRow().add(fiveCon);
        layout.row().grid().add(threeCon).spanRow().add(sixCon);

        console.setBackground(Color.BLACK);
        console.setForeground(Color.GREEN);
        console.setEditable(false);
    }

    @Override
    public Dimension getPreferredSize() {
        // comply to contract if set
        if(isPreferredSizeSet())
            return super.getPreferredSize();
        // do whatever we want
        return new Dimension(dim);
    }

    public JPanel getConsoleLayout() {
        return this;
    }

    private class ConJTextPane extends JTextPane {
        private Dimension dim = new Dimension(120,100);

        public ConJTextPane () { super(); }

        @Override
        public Dimension getPreferredSize() {
            // comply to contract if set
            if(isPreferredSizeSet())
                return super.getPreferredSize();
            // do whatever we want
            return new Dimension(dim);
        }

    }
    private class DocumentSizeFilter extends DocumentFilter {
        public DocumentSizeFilter() {

        }

        public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
            System.out.println(str);
            if (str.equals("y")) {
                System.out.println("You have pressed y.");
            }
        }

        public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)  throws BadLocationException {

        }
    }
}

此外,删除了自定义JButtons因为我不再需要它们了。不确定为什么JTextArea没有使用这个布局管理器正确格式化。

相关问题