2014-07-22 2 views
-1

일부 단추와 텍스트 필드를 사용하는 작은 프로그램을 만들려고합니다. 내가 JPanel으로 창을 만들 수 있었다하지만 어떻게 버튼과 텍스트 필드를 추가하는 생각이없는- 텍스트 필드 및 단추를 컨테이너에 추가 할 수 없습니다.

내가 사용하는 코드는 다음과 같습니다

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 또는 뭔가), oracle 페이지에서 자습서를 확인하십시오.

P. 필드 이름 짓기 등을 생각해보십시오. "przycisk"라는 이름을 지어 주면 코드를 더 이상 읽을 수없는 이유가 생깁니다.

+0

추신 가치가 없다. 일부 사용자는 모국어가 편합니다. 존중 해줘. 고맙습니다. – 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 또는 sth를 만드는 방법에 대한 몇 가지 자습서를 살펴 보는 것이 좋습니다.

0

폴란드어 이름에 대해 도움을 받아서 죄송합니다 (이미 수정 됨). 스크롤이있는 텍스트 영역을 추가 할 수있었습니다. 문제가 panel.add에있는 것 같습니다 (현재); 단추 및 텍스트 필드 전에 putted. 내 이해에서 panel.add (this)가 panel.add (pole) 앞에 설정되는지 여부; 그러면 panel.add (this)가 앞에 설정되고 pole이 추가되지만 보이지 않습니다. 내 실제 작업 코드 아래

:

... 공공 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); 

      } 
... 
관련 문제