2016-06-24 4 views
1

私はスイングファイルエディタを作成しています。 JFrameにはJLabelとJTextAreaがあります。私がJTextAreaを入力すると、JLabelが動いていることがわかります。ここにスクリーンキャストされたのは:ScreencastifyJLabelはJTextAreaに入力されたテキストで移動します

これは私が使っている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); 
    } 
} 
+0

これは奇妙です。ドキュメントリスナーを削除するか、レイアウトをBoxLayout.Y_AXISに変更しても、その動作は変更されません。 – Berger

+0

@Berger多くのアクションリスナーがありましたが、コードを短くするためにリスナーを削除しました。それらを削除しても、動作は変わりませんでした。私はこれがスウィングの内部のものだと思う。 – vikarjramun

答えて

3

あなたがJScrollPaneの中JTextAreaのを置くのを忘れ:

は、ここに私のコードです。これに

c.add(filecontent); 

を::これを変更

c.add(new JScrollPane(filecontent)); 

また、あなたはおそらくBoxLayoutの代わりにのBorderLayoutを使用する必要があり、あなたはBorderLayoutの中心部にJScrollPaneのことを置いてください。 JFrameのコンテンツペインではデフォルトでBorderLayoutが既に使用されているので、レイアウトを変更する必要はありません。コンポーネントを追加するときに正しいBorderLayout制約を指定するだけです。

+0

ありがとうございます。私はすぐにそれを試してみましょう! – vikarjramun

+0

それは動作します!どうもありがとうございました! – vikarjramun

関連する問題