2010-05-29 3 views
3

왜 JFrame '프레임'이 빈 창을 표시하는지, 아래에 3 개의 메뉴 단추와 그려진 JComponent가 있어야합니까? 내가 여기서 무엇을 놓치고 있니?프레임에 구성 요소가있는 패널을 표시하는 방법

import java.awt.*; 
import javax.swing.*; 

public class Eyes extends JFrame { 

    public static void main(String[] args) { 
     final JFrame frame = new JFrame("Eyes"); 
     frame.setPreferredSize(new Dimension(450, 300)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel players = new JPanel(new GridLayout(1, 3)); 
       players.add(new JButton("Eyes color")); 
       players.add(new JButton("Eye pupil")); 
       players.add(new JButton("Background color")); 

     JPanel eyes = new JPanel(); 
     eyes.add(new MyComponent()); 

     JPanel content = new JPanel(); 
     content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); 
     content.add(players); 
     content.add(eyes); 

     frame.getContentPane(); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

class MyComponent extends JComponent { 

    public MyComponent(){ 

    } 

    @Override 
    public void paint(Graphics g) { 
     int height = 120; 
     int width = 120; 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 
     BasicStroke bs = new BasicStroke(3.0f); 
     g2d.setStroke(bs); 
     g2d.setColor(Color.yellow); 
     g2d.fillOval(200, 200, height, width); 
     g2d.setColor(Color.black); 
     g2d.drawOval(60, 60, height, width); 
    } 
} 
+1

사용자 지정 그리기는 paint() 메서드가 아니라 paintComponent() 메서드를 재정 의하여 수행해야합니다. 그리고 메소드의 첫 번째 문으로 super.paintComponent()를 호출해야합니다. – camickr

답변

2

귀하의 라인 :

frame.getContentPane(); 

나던 프레임의 내용 창에 액세스하지만 아무것도.

frame.setContentPane(content); 

편집 : @trashgod가 지적으로

대안, 기본에 액세스 할 수 getContentPane 방법을 사용할 수 있습니다 대신 컨텐츠 구획을 받고, 당신은 이 같은 콘텐츠 창을 설정해야 컨텐츠 구획과 그에 content 구성 요소를 추가 :

frame.getContentPane().add(content); 
+0

+1 포워딩을 통해'frame.add (content) '. – trashgod

0

난 당신이 JPanels를 중첩 사용하려고 생각합니다. 이것은 확실히 구성 요소를 구성하는 방법이지만, 경우에 따라 관리하기가 어려워지는 단점이 있습니다. 아래에서이 코드 스 니펫을 시도해 볼 수 있습니다. 프로그램에서 당신은 발견 할 것이다 :

1) 나는에 Container를 사용 JLabel

2) 프로그램의 끝에서 중첩 된 JTextField

3) JPanels

의 배열의 배열 이 객체의 최종 제품을 내 그래픽 창에 추가하십시오.

내가 생각할 수있는 가장 효율적인 방법은 프로그램의 맨 위에서 이러한 구성 요소를 정의하여 나중에 필요할 때 다시 사용할 수 있도록하는 것입니다.

는이 코드 조각을 시도 할 수 있습니다이를 달성하기

import javax.swing.*; //Required to use Swing Components 

public class TestGUI extends JFrame 
{ 
    JLabel[] label;   //Define this with an array 
    JTextField[] textField; //Define this with an array as well 

    private int nLabels;  //Number of labels preferred 
    private int nTextFields; //Number of text fields preferred 

    public testGUI(int amt) 
    { 
     //Assuming that you want equal amounts of each, 
     //set these two variables to the "ant" input parameter 
     nLabels  = amt; 
     nTextFields = amt; 

     //Set component attributes 
     label  = new JLabel[2];  //Label compared text fields 
     textField = new JTextField[2]; //Use two of these for comparison 

     textField[0].setEnabled(false); //Disabled editing 
     //Do nothing with the second text field 

     JPanel labels = new JPanel(); //Place JLabels here 

     //Use this to align the labels vertically 
     labels.setLayout(new GridLayout(2, 1)); 

     //Use this for loop to add the labels to this JPanel 
     for(int i = 0; i < nLabels; i++) 
     { 
      labels.add(label[i]); 
      //You can also define and apply additional properties 
      //to labels inside this loop. TIP: You can do this in 
      //any loop 
     } 

     JPanel txtFields = new JPanel(); //Place JTextFields here 

     //Use this to align the text fields vertically 
     txtFields.setLayout(new GridLayout(2, 1)); 

     //Use this for loop to add the labels to this JPanel 
     for(int i = 0; i < nTextFields; i++) 
     { 
      textFields.add(textField[i]); 
      //You can also define and apply additional properties 
      //to text fields inside this loop. TIP: You can do 
      //this in any loop 
     } 

     //Now we have the two components, you asked for help with, set up 
     //Next, we will need another JPanel to add these to panels to. 
     //This JPanel will be added to the JFrame Container 
     //You probably know how to run this via the "main" method 

     JPanel window = new JPanel(); 
     //Place the JPanel for the labels and text fields 

     //This requires a horizontal grid 
     window.setLayout(new GridLayout(1, 2)); 

     //Add the the two JPanels: "labels" and "txtFields" 
     window.add(labels); 
     window.add(txtFields); 

     //Define the Container object to set up the GUI 
     Container container = getContentPane(); 

     //Apply the "window" JPanel object to the container 
     container.add(window, BorderLayout.CENTER); 
     //Center this in the Graphics Window when displayed 
    } 
    //Any other methods and/or functions can be added as well 
    //If they are, they must placed in the constructor method above 
} 

이 만들고 내가 쓰는 내 그래픽 윈도우를 조작에서 이동하려고 할 때 내가 사용하는 것이 방법입니다. 때로는 애플릿을 작성하지만, 일반 그래픽 창에서 모든 기능이 제대로 작동하는지 확인한 후에해야합니다.

이 정보가 도움이되기를 바랍니다.

다른 질문이 있으면 알려주세요. 최선을 다해 답변 해 드리겠습니다. 감사합니다.

관련 문제