2014-07-22 41 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); 

      } 
...