Nyx*_*Nyx 9 java resize dynamic jscrollpane jtextarea
我有两个文件.一个扩展JFrame,另一个扩展JPanel.每当我改变帧的大小时,无论是最大化,拖动还是其他什么,我都希望ScrollPane能够适应帧的当前大小.除此之外,还有一个顶级菜单栏和一个底栏,但为了简单起见,我把它们留了下来.
基本上,我希望它像记事本一样工作.
现在,我在框架上使用ComponentListener,在另一个类中调用setSize方法.setSize方法只是:
public void resize(int x, int y)
{
textA.setPreferredSize(new Dimension(x, y-50));
areaScrollPane.setPreferredSize(new Dimension(x,y-50));
}
Run Code Online (Sandbox Code Playgroud)
另外,供参考:
public void componentResized(ComponentEvent e)
{
textA.resize(panel.getWidth(),panel.getHeight());
}
Run Code Online (Sandbox Code Playgroud)
仅供参考,它扩展了JPanel,因为我将其添加到框架中:
panel = (JPanel) this.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(textA, BorderLayout.CENTER);
Run Code Online (Sandbox Code Playgroud)
那么最好的方法是什么?谢谢!
编辑:这是滚动窗格文件.它在我的主要部分称为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));
}
}
Run Code Online (Sandbox Code Playgroud)
主要:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
关于你发布的代码,为了摆脱对setSize的所有调用 - 当使用布局管理器并摆脱所有ComponentListener东西时,这些通常都不受尊重,因为它是多余的,因为你使用布局管理器调整大小.我看到的最大问题是你允许你的TextArea JPanel使用它的默认布局,即FlowLayout,这样做会阻止它所持有的JScrollPane大小调整.给这个类一个BorderLayout(或者更好的只是从类中返回一个JScrollPane),然后你就可以了.例如,通过快速修改和重命名类来防止与标准Java类发生冲突,
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class JEdit2 extends JFrame {
TextArea2 textA = new TextArea2();
JPanel panel;
public static void main(String[] args) {
new JEdit2();
}
public JEdit2() {
setTitle("JEdit 2");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = (JPanel) this.getContentPane();
panel.add(textA, BorderLayout.CENTER);
pack(); //!! added
setLocationRelativeTo(null);
setVisible(true);
}
}
@SuppressWarnings("serial")
class TextArea2 extends JPanel {
JTextArea textA = new JTextArea(500, 500); // !! this is one friggin' huge JTextArea!
JScrollPane areaScrollPane = new JScrollPane(textA);
public TextArea2() {
textA.setEditable(true);
textA.setForeground(Color.WHITE);
textA.setBackground(Color.DARK_GRAY);
this.setFont(null);
setLayout(new BorderLayout()); //!! added
add(areaScrollPane, BorderLayout.CENTER);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28595 次 |
| 最近记录: |