2017-01-22 34 views
0

我試圖製作一個模型瀏覽器,但是這一個錯誤不斷出現,看起來很好的代碼行。找不到添加(TextField)的合適方法?

import java.awt.Container; 
import java.awt.Font; 
import java.awt.Insets; 
import javafx.scene.control.TextField; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author PC 
*/ 
public class Browser extends JFrame{ 
    private TextField field = new TextField(); 
    private JEditorPane display = new JEditorPane(); 
    private JScrollPane scroller = new JScrollPane(display); 

    public static void main(String args[]){ 
    Browser file = new Browser(); 
    file.framelander(); 
    } 

    public void framelander() { 
     setTitle("Browser"); 
     setSize(1200, 800); 
     setResizable(false); 
     setVisible(true); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(null); 
     setLocationRelativeTo(null); 
     addComponentsToFrame(getContentPane()); 
    } 

    public void addComponentsToFrame(Container pane) { 
     Insets insets = getInsets(); 

     pane.add(field); 
     pane.add(scroller); 

     Font font = new Font("Menlo", Font.PLAIN, 11); 
    field.setFont(font); 
    } 



} 

我得到的錯誤是在pane.add(field)與錯誤;

no suitable method found for add(TextField) 
    method Component.add(PopupMenu) is not applicable 
     (argument mismatch; TextField cannot be converted to PopupMenu) 
    method Container.add(Component) is not applicable 
     (argument mismatch; TextField cannot be converted to Component) 
---- 
(Alt-Enter shows hints) 

我也越來越「不兼容的類型:java.awt.Font中不能轉換到javafx.scene.text.Font」在field.setFont(字體)的加粗部分;但我假設這是由於最初的錯誤。不過,我會在這裏發佈它。

任何和所有的幫助表示讚賞。提前致謝。

+0

你的提示應該是「無法轉換」以及包名......「javafx」和「awt」類不是完全兼容 –

回答

3

您輸入了錯誤的TextField

import javafx.scene.control.TextField; 

你可能要導入此一:

import javax.swing.JTextField; 

,然後改變fieldJTextField

private JTextField field = new JTextField(); 

這應該可以解決你所描述的錯誤。 此外,你的JFrame應該有一個佈局集,我建議你看看這個指南A Visual Guide to Layout Managers

+0

''你可能想要導入這個:java.awt.TextField「 - 不是。如果不需要,不要鼓勵混合AWT和Swing組件。 –

+0

除了使用Swing中的JTextField,我不明白爲什麼他不會使用那個,基於上下文以及他如何使用它。我剛剛看到他正在使用的課程的名稱,並與之一起使用,但我明白你的觀點。 –

相關問題