Im using blue j for a project which is a java framework (not through my choice) I cant get it to print out what seat they have chosen because it always selects the 1st value A1.
我使用蓝色j作为一个java框架的项目(不是通过我的选择)我不能打印出他们选择的座位,因为它总是选择第一个值A1。
Its a seating plan that displays and image and a combo box in which you can select your seat for exmaple A1 and upon the button click save it prints out the seat you have chosen in a dialog box. Help!
它是一个显示和图像的座位计划和一个组合框,您可以在其中选择您的座位作为例子A1,点击按钮保存它打印出您在对话框中选择的座位。帮帮我!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.lang.String;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class SeatingChart extends JFrame
{
private JLabel infoLabel;
// Get the list of field names, used for ordering.
String[] ordering =
{"A1","A2","A3","A4",
"B1","B2","B3","B4",
"C1","C2","C3","C4","D1","D2","D3","D4"};
String chosenSeat;
String ticket= "1" ;
int ticketValue = Integer.parseInt(ticket);
/**
* Main method for starting the system from a command line.
*/
public static void main(String[] args)
{
SeatingChart gui = new SeatingChart();
}
/**
* Create a visual aspect and display its GUI on screen.
*/
public SeatingChart()
{
super("Choose Your seats");
makeFrame();
}
/**
* Quit function: quit the application.
*/
private void quit()
{
System.exit(0);
}
private void play()
{
JFrame frames = new JFrame("Sample frame");
frames.setSize(400, 400);
frames.setVisible(false);
frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(frames, "You have chosen the seat " +chosenSeat);
}
/**
* Create the complete application GUI.
*/
public void makeFrame ()
{
// the following makes sure that our application exits when
// the user closes its window
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel contentPane = (JPanel) getContentPane();
contentPane.setBorder(new EmptyBorder(6, 10, 10, 10));
makeMenuBar(); //calls the menu bar method
// Create the center with image
JPanel centerPane = new JPanel();
{
centerPane.setLayout(new BorderLayout(8, 8));
JLabel image = new JLabel(new ImageIcon("SeatingChart.jpg"));
image.setSize (20,20);//dont work
centerPane.add(image, BorderLayout.NORTH);
centerPane.setBackground(Color.WHITE);
infoLabel = new JLabel("Please use the chart to select where you would like to sit and save it. Do this for how many tickets you have");
infoLabel.setHorizontalAlignment(SwingConstants.CENTER);
centerPane.add(infoLabel, BorderLayout.CENTER);
}
contentPane.add(centerPane, BorderLayout.EAST);
JPanel leftPane = new JPanel();
{
leftPane.setLayout(new BorderLayout(8, 8));
// Set up components for ordering the list
JPanel orderingPanel = new JPanel();
orderingPanel.setLayout(new BorderLayout());
orderingPanel.add(new JLabel("Choose your seat:"), BorderLayout.NORTH);
// Create the combo box.
JComboBox formatList = new JComboBox(ordering);
orderingPanel.add(formatList, BorderLayout.CENTER);
leftPane.add(orderingPanel, BorderLayout.NORTH);
formatList.setSelectedIndex(0);
chosenSeat = formatList.getSelectedItem().toString();
//formatList.removeItemAt(formatList.getSelectedIndex());
}
contentPane.add(leftPane, BorderLayout.CENTER);
JPanel toolbar = new JPanel();
{
toolbar.setLayout(new GridLayout(1, 0));
JButton button = new JButton("Save");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
play();
}
});
toolbar.add(button);
button = new JButton("Next");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// This button would allow the use to go to the next step
}
});
toolbar.add(button);
}
contentPane.add(toolbar, BorderLayout.SOUTH);
// building is done - arrange the components
pack();
// place this frame at the center of the screen and show
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2);
setVisible(true);
}
/**
* Create the main frame's menu bar.
*/
private void makeMenuBar()
{
final int SHORTCUT_MASK =
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JMenuBar menubar = new JMenuBar();
setJMenuBar(menubar);
JMenu menu;
JMenuItem item;
// create the File menu
menu = new JMenu("File");
menubar.add(menu);
item = new JMenuItem("Quit");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
quit();
}
});
menu.add(item);
}
}
}
1 个解决方案
#1
1
You set the chosenSeat
field immediately after creating the formatList
combo box. At that moment, the user has not even had a chance to pick a seat and seat A1 is selected by default. If you set chosenSeat
in the play
method, it should reflect the seat picked by your user.
在创建formatList组合框后立即设置selectedSeat字段。此时,用户甚至没有机会选择座位,默认情况下选择座位A1。如果您在播放方法中设置selectedSeat,它应该反映您的用户选择的座位。
I think the new JFrame
in the play
method - as already mentioned by mKorbel - is not needed, since you can use the application frame (this
) as the parent component for the call to the JOptionPane.showMessageDialog
method:
我认为不需要play方法中的新JFrame - 正如mKorbel已经提到的那样 - 因为你可以使用应用程序框架(this)作为调用JOptionPane.showMessageDialog方法的父组件:
private void play() {
// todo: frames is not needed.
//JFrame frames = new JFrame("Sample frame");
//frames.setSize(400, 400);
//frames.setVisible(false);
//frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chosenSeat = formatList.getSelectedItem().toString();
// todo: use the application frame as the parent.
//JOptionPane.showMessageDialog(frames, "You have chosen the seat " + chosenSeat);
JOptionPane.showMessageDialog(this, "You have chosen the seat " + chosenSeat);
}
#1
1
You set the chosenSeat
field immediately after creating the formatList
combo box. At that moment, the user has not even had a chance to pick a seat and seat A1 is selected by default. If you set chosenSeat
in the play
method, it should reflect the seat picked by your user.
在创建formatList组合框后立即设置selectedSeat字段。此时,用户甚至没有机会选择座位,默认情况下选择座位A1。如果您在播放方法中设置selectedSeat,它应该反映您的用户选择的座位。
I think the new JFrame
in the play
method - as already mentioned by mKorbel - is not needed, since you can use the application frame (this
) as the parent component for the call to the JOptionPane.showMessageDialog
method:
我认为不需要play方法中的新JFrame - 正如mKorbel已经提到的那样 - 因为你可以使用应用程序框架(this)作为调用JOptionPane.showMessageDialog方法的父组件:
private void play() {
// todo: frames is not needed.
//JFrame frames = new JFrame("Sample frame");
//frames.setSize(400, 400);
//frames.setVisible(false);
//frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chosenSeat = formatList.getSelectedItem().toString();
// todo: use the application frame as the parent.
//JOptionPane.showMessageDialog(frames, "You have chosen the seat " + chosenSeat);
JOptionPane.showMessageDialog(this, "You have chosen the seat " + chosenSeat);
}