我正在创建一个摆动文件编辑器。我有一个jlabel,我的jframe中有一个jtextarea。当我输入JTextarea时,我会注意到Jlabel移动。这是这样的屏幕列表:ScreenCastify
这与我正在使用的BoxLayout有关吗?您能帮我解释这种行为并可能解决吗?
这是我的代码:
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.Highlighter;
import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileNotFoundException;
public class Editor2 extends JFrame{
private static final long serialVersionUID = 1L;
private static final String appName="JEdit: ";
Container c;
JMenuBar menubar;
JMenu filemenu,edit,optionsmenu;
JMenuItem save,saveas,
newfile,openfile,close,
find,clearfind,
textcolor;
JLabel filetitle;
JTextArea filecontent;
WriteFile out;
ReadFile in;
JFileChooser jfc;
File f;
Document filecontentdoc;
boolean upToDate;
Highlighter h;
DefaultHighlightPainter dhp;
public Editor2() throws FileNotFoundException {
super(appName+"Untitled");
f=null;
upToDate=true;
c=getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS));
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
filetitle = new JLabel("Editing Untitled");
c.add(filetitle);
menubar=new JMenuBar();
setJMenuBar(menubar);
filemenu = new JMenu("File");
edit = new JMenu("Edit");
optionsmenu = new JMenu("Options");
menubar.add(filemenu);
menubar.add(edit);
menubar.add(optionsmenu);
save=new JMenuItem("Save");
saveas=new JMenuItem("Save As");
newfile=new JMenuItem("New File");
openfile=new JMenuItem("Open File");
close=new JMenuItem("Close");
filemenu.add(save);
filemenu.add(saveas);
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.add(close);
find=new JMenuItem("Find");
clearfind=new JMenuItem("Clear Highlights");
edit.add(find);
edit.add(clearfind);
textcolor=new JMenuItem("Text Color");
optionsmenu.add(textcolor);
filecontent = new JTextArea(50,50);
c.add(filecontent);
filecontentdoc=filecontent.getDocument();
filecontentdoc.addDocumentListener(new DocumentListener() {
@Override public void removeUpdate(DocumentEvent e) {}
@Override
public void insertUpdate(DocumentEvent e) {
upToDate=false;
}
@Override public void changedUpdate(DocumentEvent e) {}
});
h = filecontent.getHighlighter();
dhp = new DefaultHighlightPainter(Color.YELLOW);
//pack();
setSize(1000, 1000);
this.addWindowListener(new WindowListener() {
@Override public void windowOpened(WindowEvent e) {}
@Override public void windowIconified(WindowEvent e) {}
@Override public void windowDeiconified(WindowEvent e) {}
@Override public void windowDeactivated(WindowEvent e) {}
@Override public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
@Override public void windowClosed(WindowEvent e) {
dispose();
System.exit(0);
}
@Override public void windowActivated(WindowEvent e) {}
});
}
public static void main(String[] args) throws FileNotFoundException {
Editor2 ef = new Editor2();
ef.setVisible(true);
}
public void setVisible(boolean b){
super.setVisible(b);
}
}
您忘了将JTextarea放在Jscrollpane中。更改此:
c.add(filecontent);
:
c.add(new JScrollPane(filecontent));
另外,您可能应该使用BorderLayout而不是BoxLayout,并且应该将JSCrollpane放在Borderlayout的中心。请注意,Jframe的内容窗格已经默认使用了BorderLayout,因此您根本不必更改布局,只需在将组件添加组件时指定正确的BorderLayout约束。