【问题标题】:DesignGridLayout Sizing JButtonsDesignGridLayout 调整 JButton 大小
【发布时间】:2014-05-11 04:48:55
【问题描述】:

我一直在使用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);
        }
    }
}

我需要能够将“”调整为更小的尺寸。然后是布局管理器的默认设置。我试图覆盖getPreferredSize() 方法,如此处#21052894 所述。虽然,这不太正确。

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

----------------------------------------------

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

【问题讨论】:

    标签: java swing awt layout-manager


    【解决方案1】:

    这似乎对我不起作用。我从 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 {
    
            }
        }
    }
    

    另外,删除了自定义 JButton,因为我不再需要它们。不知道为什么 JTextArea 使用此布局管理器无法正确格式化。

    【讨论】:

      猜你喜欢
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多