2012-12-17 7 views
-5

이있는 JPanel의JPanel에 클래스를 추가하려면 어떻게해야합니까?

public class DisplayBoard { 

    public static void main (String[] args) { 

    JFrame frame = new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //The main panel 
    JPanel main = new JPanel(); 
    main.setPreferredSize(new Dimension(600,800)); 
    main.setLayout(new BorderLayout()); 

    //The title panel 
    JPanel title = new JPanel(); 
    title.setPreferredSize(new Dimension(400, 120)); 
    title.setBackground(Color.YELLOW); 
    JLabel test1 = new JLabel("Title goes here"); 
    title.add(test1); 

    //The side bar panel 
    JPanel sidebar = new JPanel(); 
    sidebar.setPreferredSize(new Dimension(200, 800)); 
    sidebar.add(AddSubtract); 
    sidebar.setBackground(Color.GREEN); 
    JLabel test2 = new JLabel("Sidebar goes here"); 
    sidebar.add(test2); 

    //The panel that displays all the cards 
    JPanel cardBoard = new JPanel(); 
    cardBoard.setPreferredSize(new Dimension(400,640)); 

    //adding panels to the main panel 
    main.add(cardBoard, BorderLayout.CENTER); 
    main.add(title, BorderLayout.NORTH); 
    main.add(sidebar, BorderLayout.WEST); 

    frame.setContentPane(main); 
    frame.pack(); 
    frame.setVisible(true); 

    } 
} 

내가

public class AddSubtract { 

int Number = 0; 
private JFrame Frame = new JFrame("Math"); 
private JPanel ContentPane = new JPanel(); 
private JButton Button1 = new JButton("Add"); 
private JButton Button2 = new JButton("Subtract"); 
private JLabel Num = new JLabel ("Number: " + Integer.toString (Number)); 

public AddSubtract() { 

    Frame.setContentPane(ContentPane); 
    ContentPane.add(Button1); 
    ContentPane.add(Button2); 
    ContentPane.add(Num); 
    Button1.addActionListener(new Adding()); 
    Button2.addActionListener(new Subtracting()); 
} 

public class Adding implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     Number++; 
     Num.setText ("Number: " + Integer.toString (Number)); 
    } 
} 

public class Subtracting implements ActionListener { 
    public void actionPerformed(ActionEvent e) { 
     Number--; 
     Num.setText ("Number: " + Integer.toString (Number)); 
    } 
} 

public void launchFrame(){ 
    Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    Frame.pack(); 
    Frame.setVisible(true); 
} 

public static void main(String args[]){ 
    AddSubtract Test = new AddSubtract(); 
    Test.launchFrame(); 
    } 
} 

누군가가 내가이 작업을 수행 할 수있는 방법을 나에게 설명 할 수 사이드 패널에이 클래스를 추가 할? 이것이 작동하지 않는다는 느낌이 들지만, 실제로 그것을 수행하는 방법을 배우고 싶습니다.

+0

"수업 추가"란 무엇을 의미합니까? – GGrec

+0

클래스 "AddSubtract"를 원합니다 사이드 바 패널에 표시하려면 다음을 수행하십시오. – Oxtis

+0

클래스'AddSubtract'는'JPanel'을 확장해야합니다. 그런 다음 주 클래스 –

답변

2

이것은 확실히 작동하지 않을 것입니다. 우선, 두 개의 main() 메소드가 있습니다. 둘째, 프레임에 클래스를 추가하려면 JComponent까지 확장해야합니다. 기본적으로 코드는 다음과 같아야합니다.

public class MainFrame extends JFrame { 
    public MainFrame() { 
     this.add(new MainPanel()) 
     //insert all settings here. 
    } 
} 

public class MainPanel extends JPanel { 
    public MainPanel() { 
     this.add(new AddSubtract()); 
     this.add(/*more panels*/) 
    } 
} 

public class AddSubtract extends JPanel { 
    public AddSubtract() { 
     //add buttons and stuff here 
    } 
} 

및 변수는 대문자로 시작하지 않아야합니다.

편집 :

public static void main(String[] args) { 
    new MainFrame(); 
} 

단지 생성자의 설정과 JFrame의 구성을 설정 : 당신은 어떤 JFrame의있을 때, 그것은 단지 하나의 선이있는 main() 방법을하는 것이 가장 좋습니다.

+2

또는 [Runnable'] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)에 'JPanel'을 추가합니다. – trashgod

관련 문제