2014-07-22 88 views
-1

我正在嘗試使用一些按鈕和文本字段的小程序。 我能夠與JPanel創建窗口,但沒有想法如何添加按鈕和文本字段JAVA-無法將文本字段和按鈕添加到容器

我正在使用的代碼是:

public UI() { 

    sprites = new HashMap(); 
    // spriteCache = stage.getSpriteCache(); 
    JFrame okno = new JFrame ("VoLTE Script"); 
    setBounds(0,0,SZEROKOSC,WYSOKOSC); 
    JPanel panel = (JPanel)okno.getContentPane(); 
    panel.setLayout (null); 
    panel.add(this); 
    okno.setBounds(0,0,800,600); 
    okno.setVisible(true); 
    JTextField pole = new JTextField(10); 
    JButton przycisk = new JButton("teasda"); 
    przycisk.setPreferredSize(new Dimension(300, 350)); 
    przycisk.setLayout(new BorderLayout()); 
    panel.add(przycisk); 
    przycisk.setVisible(true); 
    pole.setBounds (300,300,200,200); 
    pole.setLayout(null); 
    pole.setVisible(true); 
    panel.add(pole); 

    okno.addWindowListener(new WindowAdapter(){ 
      public void windowClosing (WindowEvent e){ 
       System.exit(0); 
      } 
    }); 
    okno.setResizable(false); 

    createBufferStrategy(2); 
    strategia=getBufferStrategy(); 
    requestFocus(); 
    // addKeyListener(this); 
    // addMouseListener(this); 

    } 

回答

0

您需要正確使用的佈局,當使用邊界佈局,你需要告訴它使用哪個邊框(,BorderLayout.NORTH或者其他),看看在oracles頁面的教程。

P.S.想想你如何命名你的領域等。命名「przycisk」只是給我一個理由,不要進一步閱讀代碼。

+0

The P.S.不當。一些用戶使用母語更舒適。請尊重這一點。謝謝。 – laune

0

這段代碼是從該網站:Example of Java GUI

//Imports are listed in full to show what's being used 
//could just import javax.swing.* and java.awt.* etc.. 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JComboBox; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import java.awt.BorderLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class GuiApp1 { 

    //Note: Typically the main method will be in a 
    //separate class. As this is a simple one class 
    //example it's all in the one class. 
    public static void main(String[] args) { 

     new GuiApp1(); 
    } 

    public GuiApp1() 
    { 
     JFrame guiFrame = new JFrame(); 

     //make sure the program exits when the frame closes 
     guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     guiFrame.setTitle("Example GUI"); 
     guiFrame.setSize(300,250); 

     //This will center the JFrame in the middle of the screen 
     guiFrame.setLocationRelativeTo(null); 

     //Options for the JComboBox 
     String[] fruitOptions = {"Apple", "Apricot", "Banana" 
       ,"Cherry", "Date", "Kiwi", "Orange", "Pear", "Strawberry"}; 

     //Options for the JList 
     String[] vegOptions = {"Asparagus", "Beans", "Broccoli", "Cabbage" 
       , "Carrot", "Celery", "Cucumber", "Leek", "Mushroom" 
       , "Pepper", "Radish", "Shallot", "Spinach", "Swede" 
       , "Turnip"}; 

     //The first JPanel contains a JLabel and JCombobox 
     final JPanel comboPanel = new JPanel(); 
     JLabel comboLbl = new JLabel("Fruits:"); 
     JComboBox fruits = new JComboBox(fruitOptions); 

     comboPanel.add(comboLbl); 
     comboPanel.add(fruits); 

     //Create the second JPanel. Add a JLabel and JList and 
     //make use the JPanel is not visible. 
     final JPanel listPanel = new JPanel(); 
     listPanel.setVisible(false); 
     JLabel listLbl = new JLabel("Vegetables:"); 
     JList vegs = new JList(vegOptions); 
     vegs.setLayoutOrientation(JList.HORIZONTAL_WRAP); 

     listPanel.add(listLbl); 
     listPanel.add(vegs); 

     JButton vegFruitBut = new JButton("Fruit or Veg"); 

     //The ActionListener class is used to handle the 
     //event that happens when the user clicks the button. 
     //As there is not a lot that needs to happen we can 
     //define an anonymous inner class to make the code simpler. 
     vegFruitBut.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent event) 
      { 
       //When the fruit of veg button is pressed 
       //the setVisible value of the listPanel and 
       //comboPanel is switched from true to 
       //value or vice versa. 
       listPanel.setVisible(!listPanel.isVisible()); 
       comboPanel.setVisible(!comboPanel.isVisible()); 

      } 
     }); 

     //The JFrame uses the BorderLayout layout manager. 
     //Put the two JPanels and JButton in different areas. 
     guiFrame.add(comboPanel, BorderLayout.NORTH); 
     guiFrame.add(listPanel, BorderLayout.CENTER); 
     guiFrame.add(vegFruitBut,BorderLayout.SOUTH); 

     //make sure the JFrame is visible 
     guiFrame.setVisible(true); 
    } 

} 

對於未來我會建議你使用延伸的JFrame所以你只能寫這樣一個GUI沒有初始化一個JFrame:

public class CalculatorGUI extends JFrame { 
    public CalculatorGUI() { 
     setTitle("Calculator"); 
     setBounds(300, 300, 220, 200); 
    }} 

我建議你查看幾個關於如何創建Java Gui或者某些教程的教程。

0

非常感謝您對波蘭人姓名(已修復)的幫助和抱歉。 我能夠通過滾動添加文本區域。 看起來問題是在panel.add(this);放在按鈕和文本字段之前。 從我的理解是否panel.add(this)設置在panel.add(極點)之前;然後panel.add(this)被設置在前面並且杆被添加但是看不到。

下面我的實際工作代碼:

... 公共UI(){

sprites = new HashMap(); 
    // spriteCache = stage.getSpriteCache(); 
    JFrame okno = new JFrame ("VoLTE Script"); 
    setBounds(0,0,WIDTH,HEIGHT); 
    JPanel panel = (JPanel)okno.getContentPane(); 
    panel.setLayout (null); 
    okno.setBounds(0,0,800,600); 
    okno.setVisible(true); 
    JTextArea pole = new JTextArea(); 
    pole.setLayout(null); 
    pole.setLineWrap(true); 
    //pole.setBackground(Color.BLACK); 
    pole.setEditable(false); 
    JScrollPane scroll = new JScrollPane(pole); 
    scroll.setBounds(25,250,500,300); 

    scroll.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    panel.add(scroll); 
    panel.add(this); 

    okno.addWindowListener(new WindowAdapter(){ 
      public void windowClosing (WindowEvent e){ 
       System.exit(0); 
      } 

}); okno.setResizable(false);

createBufferStrategy(2); 
    strategia=getBufferStrategy(); 
    requestFocus(); 
    // addKeyListener(this); 
    addMouseListener(this); 

      } 
...