我需要插入什么才能显示整个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");}}
}
}
答案 0 :(得分:3)
从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所涵盖的内容。阅读。你不应该在没有经过每一小步的测试之后编写如此庞大的代码,特别是如果你是一个新手。上面的这些行应该是应用程序的第一行。