2012-04-22 10 views
1

프로그램을 실행하려고 할 때마다 빈 창이 나타납니다. 이유를 알 수 없습니다. 이전에 메인 메쏘드 안에서 모든 것을 가지고 있었을 때 모든 것을 창에 나타낼 수있었습니다. 그러나 지금 다른 무언가를 시도 할 때, 그냥 나타나지 않고 닫아서 프로그램이 실행되는 것을 멈추지 않을 것입니다. 내 코드에서 그 점을 설명했다고 생각했습니다. 코드에는 다른 문제가있을 수 있지만 실제로 도움이 필요한 것은이 코드뿐입니다.내 JFrame의 아무 것도 나타나지 않습니다.

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

class AirplaneSeats extends JFrame implements ActionListener{ 

private static final int FRAME_WIDTH = 400; 
private static final int FRAME_HEIGHT = 300; 
private static final int FRAME_X_ORIGIN = 150; 
private static final int FRAME_Y_ORIGIN = 250; 

private JLabel reservetype = new JLabel("Select Reservation Type:"); 
private JLabel greenseat = new JLabel("Select availiable (green numbered) seat:"); 

private JButton firstclass; 
private JButton coach; 

private JButton one; 
private JButton two; 
private JButton three; 
private JButton four; 
private JButton five; 
private JButton six; 
private JButton seven; 
private JButton eight; 
private JButton nine; 
private JButton ten; 
private JButton eleven; 
private JButton twelve; 
private JButton nothing; 

public static void main(String [] args){ 
    JFrame frame = new JFrame("Island Airlines"); 
    frame.setVisible(true); 
    } 

public AirplaneSeats(){ 
    Container contentPane; 

    contentPane= getContentPane(); 
    contentPane.setLayout(null); 
    contentPane.setBackground(Color.white); 

    setTitle("JMenuFrame: Testing Swing Menus"); 
    setSize(FRAME_WIDTH, FRAME_HEIGHT); 
    setResizable(false); 
    setLocation(FRAME_X_ORIGIN,FRAME_Y_ORIGIN); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    reservetype.setBounds(10, 10, 200, 20); 
    greenseat.setBounds(10, 100, 250, 20); 

    contentPane.add(reservetype); 
    contentPane.add(greenseat); 

    firstclass = new JButton("first class"); 
    coach = new JButton("coach"); 

    contentPane.add(firstclass); 
    contentPane.add(coach); 

    firstclass.setBounds(60, 40, 80, 35); 
    coach.setBounds(150, 40, 80, 35); 

    firstclass.addActionListener(this); 
    coach.addActionListener(this); 

    one = new JButton("1"); 
    two = new JButton("2"); 
    three = new JButton("3"); 
    four = new JButton("4"); 
    five = new JButton("5"); 
    six = new JButton("6"); 
    seven = new JButton("7"); 
    eight = new JButton("8"); 
    nine = new JButton("9"); 
    ten = new JButton("10"); 
    eleven = new JButton("11"); 
    twelve = new JButton("12"); 
    nothing = new JButton(""); 

    one.setBounds(20, 120, 30, 30); 
    three.setBounds(60, 120, 30, 30); 
    five.setBounds(100, 120, 30, 30); 
    seven.setBounds(150, 120, 30, 30); 
    nine.setBounds(190, 120, 30, 30); 
    eleven.setBounds(230, 120, 30, 30); 
    two.setBounds(20, 160, 30, 30); 
    four.setBounds(60, 160, 30, 30); 
    six.setBounds(100, 160, 30, 30); 
    eight.setBounds(150, 160, 30, 30); 
    ten.setBounds(190, 160, 30, 30); 
    twelve.setBounds(230, 160, 30, 30); 

    contentPane.add(one); 
    contentPane.add(two); 
    contentPane.add(three); 
    contentPane.add(four); 
    contentPane.add(five); 
    contentPane.add(six); 
    contentPane.add(seven); 
    contentPane.add(eight); 
    contentPane.add(nine); 
    contentPane.add(ten); 
    contentPane.add(eleven); 
    contentPane.add(twelve); 
    contentPane.add(nothing); 

    one.addActionListener(this); 
    two.addActionListener(this); 
    three.addActionListener(this); 
    four.addActionListener(this); 
    five.addActionListener(this); 
    six.addActionListener(this); 
    seven.addActionListener(this); 
    eight.addActionListener(this); 
    nine.addActionListener(this); 
    ten.addActionListener(this); 
    eleven.addActionListener(this); 
    twelve.addActionListener(this); 


} 

@Override 
public void actionPerformed(ActionEvent event) { 
    // TODO Auto-generated method stub 

} 

} 
+1

상황의 99 %에서'null LayoutManager'를 사용하는 것은 끔찍한 생각입니다. – Jeffrey

+0

아멘 @ Jeffrey 님의 추천입니다. 처음부터 GUI를 만드는 것이 쉬운 것처럼 보일 수도 있지만 null 레이아웃은 업그레이드 및 유지 보수를 시도하는 절대적인 곰입니다. 레이아웃 관리자와 구성 요소의 기본 크기가 가장 자연스러운 레이아웃을 만드는 것이 좋습니다. –

+0

@ 제퍼 레 오하이오. Lol 저는 여전히 Guis를 배우며 LayoutManagers에 관해서는 혼합 된 메시지를 계속해서 보냅니다. –

답변

0
당신은 단지 빈 JFrame의를 생성 JFrame frame = new JFrame("Island Airlines");

public static void main(String [] args){ 
    AirplaneSeats airplaneSeats = new AirplaneSeats(); 
    airplaneSeats.setVisible(true); 
} 

에 주요 방법을 변경

.

1

기본 방법에서는 AirplaneSeats 객체가 아닌 일반 바닐라 JFrame 객체를 만들고 표시합니다. 대신 AirplaneSeats 객체를 만들고 표시하는 것이 좋습니다.

+0

감사합니다. 이제는 완벽하게 나타납니다! –

+0

@ 루카스 : 때로는 코드를보기 위해 신선한 눈이 필요합니다. –

관련 문제