【发布时间】:2011-06-05 02:39:21
【问题描述】:
我有两个文件。一个扩展 JFrame,另一个扩展 JPanel。 每当我改变框架的大小时,无论是最大化、拖动还是其他,我都希望 ScrollPane 能够适应框架的当前大小。 还有更多,还有一个顶部菜单栏和一个底部栏,但为了简单起见,我将它们省略了。
基本上,我希望它像记事本一样工作。
现在,我在调用另一个类中的 setSize 方法的框架上使用 ComponentListener。 setSize 方法就是:
public void resize(int x, int y)
{
textA.setPreferredSize(new Dimension(x, y-50));
areaScrollPane.setPreferredSize(new Dimension(x,y-50));
}
还有,供参考:
public void componentResized(ComponentEvent e)
{
textA.resize(panel.getWidth(),panel.getHeight());
}
仅供参考,它扩展了 JPanel,因为我将它添加到框架中的方式:
panel = (JPanel) this.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(textA, BorderLayout.CENTER);
那么最好的方法是什么? 谢谢!
编辑:这是滚动窗格文件。它在我的 main 中称为 textA。
public class TextArea extends JPanel
{
JTextArea textA=new JTextArea(500,500);
JScrollPane areaScrollPane = new JScrollPane(textA);
Toolkit toolkit = Toolkit.getDefaultToolkit ();
Dimension dim = toolkit.getScreenSize();
Dimension dim2=(new Dimension((int)(dim.getWidth()),(int)(dim.getHeight()-120)));
public TextArea()
{
//textA.setLineWrap(true);
//textA.setWrapStyleWord(true);
textA.setEditable(true);
textA.setForeground(Color.WHITE);
textA.setBackground(Color.DARK_GRAY);
this.setFont(null);
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
areaScrollPane.setMinimumSize(new Dimension(300,300));
areaScrollPane.setSize(new Dimension(800,800));
textA.setPreferredSize(dim2);
areaScrollPane.setPreferredSize(dim2);
areaScrollPane.setMaximumSize(dim2);
add(areaScrollPane);
}
@Override
public void resize(int x, int y)
{
textA.setPreferredSize(new Dimension(x, y-50));
areaScrollPane.setPreferredSize(new Dimension(x,y-50));
}
}
还有主要的:
public class JEdit extends JFrame implements ComponentListener
{
TextArea textA=new TextArea();
JPanel panel;
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
JEdit run=new JEdit();
}
public JEdit()
{
setTitle("JEdit");
setLayout(new BorderLayout());
setSize(1100, 1000);
this.setMinimumSize(new Dimension(100,100));
//setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
System.out.println("error1");
} catch (InstantiationException ex) {
System.out.println("error2");
} catch (IllegalAccessException ex) {
System.out.println("error3");
} catch (UnsupportedLookAndFeelException ex) {
System.out.println("error4");
}
panel = (JPanel) this.getContentPane();
panel.setLayout(new BorderLayout());
//TopBar top=new TopBar();
// PositionBar posB=new PositionBar();
panel.add(textA, BorderLayout.CENTER);
// add(top,BorderLayout.NORTH);
// add(posB,BorderLayout.SOUTH);
addComponentListener(this);
setVisible(true);
}
public void componentResized(ComponentEvent e)
{
textA.resize(panel.getWidth(),panel.getHeight());
}
public void componentMoved(ComponentEvent e) {
textA.resize(panel.getWidth(),panel.getHeight());
}
public void componentShown(ComponentEvent e) {
textA.resize(panel.getWidth(),panel.getHeight());
}
public void componentHidden(ComponentEvent e) {
textA.resize(panel.getWidth(),panel.getHeight());
}
}
【问题讨论】:
-
您被告知无需设置任何首选大小或使用任何类型的侦听器。您还告诉您将滚动窗格直接添加到框架中,但您将其添加到 JPanel。如果您需要帮助,请努力实际尝试这些建议!
标签: java dynamic resize jscrollpane jtextarea