【发布时间】: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