每次我尝试运行程序时,只会出现一个空窗口,我无法弄清楚原因。当我在main方法中拥有相当多的东西时,我能够将所有内容都显示在窗口中,但是现在当我尝试不同的东西时,它就不会出现并且关闭它不会阻止程序运行,即使我以为我在我的代码中占了这个。代码可能还有其他问题,但我唯一需要帮助的就是这个问题。
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
}
}
答案 0 :(得分:1)
在main方法中,您只需创建并显示普通的vanilla JFrame对象,而不是AirplaneSeats对象。也许您想要创建并显示您的AirplaneSeats对象?
答案 1 :(得分:0)
将主要方法更改为
public static void main(String [] args){
AirplaneSeats airplaneSeats = new AirplaneSeats();
airplaneSeats.setVisible(true);
}
你有JFrame frame = new JFrame("Island Airlines");
只创建一个空白的jframe。