java - 让我的 GUI 显示出来?

标签 java swing user-interface

我需要插入什么才能显示整个 GUI?
我相信我的 AirlineReservation.java 类中的所有内容都已正确格式化。我只是确定如何实际进行并打印我创建的 GUI 来测试它。

 package airline;

    import javax.swing.JOptionPane;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;

    import javax.swing.BorderFactory;
    import javax.swing.ButtonGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JCheckBoxMenuItem;
    import javax.swing.JRadioButtonMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JTabbedPane;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.SwingConstants;

    import javax.swing.event.ChangeListener;
    import javax.swing.event.ChangeEvent;
    import userInterface.AirlineReservation;



    //Begin AirportMain class
    public class AirportMain {

    //Main Method
    public static void main(String[] args) {

        //New instance of Airport
     Airport airport = new Airport(
     "Orlando International Airport", "Orlando", "Florida","MCO");  


        //Create an instance of Airline and Flight for Delta  
        Flight flight = new Flight(1);
        Airline delta = new Airline();
        delta.setAirlineName("Delta Airlines");
        delta.setAirlineHubLocation("ATL");
        delta.setFlight(flight);
        flight.setFlightNumber(98);
        flight.setDestinationLocation("Buffalo, NY");

        //Create an instance of Airline and Flight for United
        Flight flights = new Flight(2);
        Airline united = new Airline();
        united.setAirlineName("United Airlines");
        united.setAirlineHubLocation("MCO");
        united.setFlight(flights);
        flights.setFlightNumber(234);
        flights.setDestinationLocation("Los Angeles, CA");

        //Displays info from Airport/Delta/United in a dialog box
        //JOptionPane.showMessageDialog(null, airport.toString() + 
        delta.toString() + "\n" +  united.toString());




    }
       AirlineReservation airlineReservation = new AirlineReservation();


     }

这是我的 GUI 的代码

package userInterface;

    import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.*;

public class AirlineReservation {

    private JFrame frame;
    private JLabel label;
    private JMenu fileMenu;
    private JMenu formatMenu;
    private JMenu reserveMenu;
    private JMenuBar menuBar;
    private JMenuItem menuItem1;
    private JMenuItem menuItem2;
    private JMenuItem menuItem3;
    private JMenuItem menuItem4;
    private JMenuItem menuItem5;
    private JRadioButtonMenuItem radioMenuItem1;
    private JRadioButtonMenuItem radioMenuItem2;
    private JCheckBoxMenuItem checkMenuItem1;
    private JCheckBoxMenuItem checkMenuItem2;
    private JSlider slider;
    private JTabbedPane tabPane;


   public AirlineReservation()
    {
        initComponents();
    }

    public void initComponents()
    {
        frame = new JFrame("Airline Reservation");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(300, 300);

        frame.setVisible(true);

        // Now lets create a JMenuBar to add to our JFrame
        // Initializing the JMenuBar
        menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);


        // Initializing menu items
        menuItem1 = new JMenuItem("Add Airport");
        menuItem2 = new JMenuItem("Add Airline");
        menuItem3 = new JMenuItem("Add Flight");

        // Initializing the JMenu and adding JMenuItems
        fileMenu = new JMenu("File");
        fileMenu.setMnemonic('F');
        fileMenu.add(menuItem1);
        fileMenu.add(menuItem2);
        fileMenu.add(menuItem3);


        // Initializing menu items
        menuItem4 = new JMenuItem("Search Flights");
        menuItem5 = new JMenuItem("Make Reservation");

        // Initializing the JMenu and adding JMenuItems
        reserveMenu = new JMenu("Reservation");
        reserveMenu.setMnemonic('R');
        reserveMenu.add(menuItem4);
        reserveMenu.add(menuItem5);



        // Initializing the menu items
        radioMenuItem1 = new JRadioButtonMenuItem("Times New Roman");
        radioMenuItem2 = new JRadioButtonMenuItem("Comic Sans");
        ButtonGroup group = new ButtonGroup();
        group.add(radioMenuItem1);
        group.add(radioMenuItem2);

        checkMenuItem1 = new JCheckBoxMenuItem("Bold");
        checkMenuItem2 = new JCheckBoxMenuItem("Italics");

        // Initializing the JMenu and adding JMenuItems
        formatMenu = new JMenu("Format");
        formatMenu.setMnemonic('M');
        formatMenu.add(radioMenuItem1);
        formatMenu.add(radioMenuItem2);
        formatMenu.addSeparator();
        formatMenu.add(checkMenuItem1);
        formatMenu.add(checkMenuItem2);



        // Add the menus to the JMenuBar
        menuBar.add(fileMenu);
        menuBar.add(reserveMenu);
        menuBar.add(formatMenu);



        // Add the menubar to the JFrame
        frame.setJMenuBar(menuBar);

        slider = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
        // sets the value for each major tick spacing
        slider.setMajorTickSpacing(50);
        // makes the tick labels with values visible
        slider.setPaintLabels(true);
        // makes the individual tick marks visible
        //        slider.setPaintTicks(true);
        // This is an event handler
        slider.addChangeListener(
                // This is an anonymous inner class
                new ChangeListener()
                {
                    // This handles the updated value of the slider
                    public void stateChanged(ChangeEvent e)
                    {
                        // sets the text of the label to the actual value on the slide
                        // explicit type casting because setText expects a String
                        // but getValue returns and int
                        label.setText(String.valueOf(slider.getValue()));
                    }
                });


        // initializing the JTabbedPane
        JLabel label1 = new JLabel("panel 1");
        JPanel panel1 = new JPanel();

        // adding a label to display the value of the slider
        label = new JLabel();
        label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panel1.add(label);
        panel1.add(slider);
        panel1.add(label1);

        JLabel label2 = new JLabel("panel 2");
        JPanel panel2 = new JPanel();
        panel2.add(label2);

        tabPane = new JTabbedPane();
        tabPane.addTab("Tab One", null, panel1, "First Panel");
        tabPane.addTab("Tab Two", null, panel2, "Second Panel");
        frame.add(tabPane);

    }
 public class FileMenuAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand() == "Add Airport") {
                System.out.println("Add Airport Clicked");
            } else if (e.getActionCommand() == "Add Airline") {
                System.out.println("Add Airline Clicked");

            } else if (e.getActionCommand() == "Add Flight") {
                System.out.println("Add Flight Clicked");
            }
        }
    }

    public class BookMenuAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand() == "sjsjsjs") {
                System.out.println("fjfhfhf");
            }
        }
    }

    public class AboutMenuAction implements ActionListener {

        public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "About Info")
        { System.out.println("About Application clicked");}}
    }
}

最佳答案

从 GUI 构造函数中删除这一行:

frame.setVisible(true);

在 GUI 构造函数的末尾添加此行:

frame.pack();

将此方法添加到您的 GUI 类中:

public void showFrame() {
    frame.setVisible(true);
}

从主类中删除此行:

AirlineReservation airlineReservation = new AirlineReservation();

并将以下内容添加到您的主要方法中:

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        AirlineReservation airlineReservation = new AirlineReservation();
        airlineReservation.showFrame();
    }
});

这基本上是 "Getting started" part of the Swing tutorial 所涵盖的内容。阅读。您永远不应该编写这么大的代码而不在每个小步骤之后进行测试,特别是如果您是新手。上面这些行应该是应用程序的第一行。

关于java - 让我的 GUI 显示出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21961516/

相关文章:

java - 动态添加多个 MouseListener JPanel

WPF 字体缩放

java - for 循环(i) 在循环外使用变量或可能的解决方案?

python - 在 GTK 应用程序中嵌入 python 程序(C 语言)

java - Java中随机选择函数

java - 将按钮和文本放在新行上并将它们移动到 java 中的屏幕底部

java - 重新绘制传送我的图形而不是平滑地移动它

异步完成创建的 Java 对象 - 对象在构造函数完成后的某个时间准备就绪

java - Android 错误 : type mismatch cannot convert from android. app.actionbar 到 android.support.v7.app.actionbar

Java测试(初级)