我正在开发一个Java GUI程序,它只是一个练习/示例程序,用于检查酒店中哪些房间是空的,并为一组人预留。我的问题是我需要在#34; Check Room Availability"之后显示JButton。按下,如果RoomsArray中有一个值为0的索引,我可以在按下该按钮后显示一个JTextPane但是我的"是"如果它出现在If语句中,JButton就不会出现,如果它在If语句之外,它可以正常工作,但是我需要它只出现在" Check Room Availability"按下按钮。任何帮助表示赞赏!
package text;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import javax.swing.JTextPane;
import java.awt.Color;
import javax.swing.UIManager;
import java.awt.Font;
import java.awt.Choice;
import javax.swing.JButton;
import org.apache.commons.lang3.ArrayUtils;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
@SuppressWarnings("serial")
public class HotelReservationGUI extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HotelReservationGUI frame = new HotelReservationGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @return
*/
public HotelReservationGUI() {
contentPane = new JPanel();
final int[] RoomsArray = new int[10];
RoomsArray[0] = (int) ((10 * Math.random()) - 1);
RoomsArray[1] = (int) ((10 * Math.random()) - 1);
RoomsArray[2] = (int) ((10 * Math.random()) - 1);
RoomsArray[3] = (int) ((10 * Math.random()) - 1);
RoomsArray[4] = (int) ((10 * Math.random()) - 1);
RoomsArray[5] = (int) ((10 * Math.random()) - 1);
RoomsArray[6] = (int) ((10 * Math.random()) - 1);
RoomsArray[7] = (int) ((10 * Math.random()) - 1);
RoomsArray[8] = (int) ((10 * Math.random()) - 1);
RoomsArray[9] = (int) ((10 * Math.random()) - 1);
setResizable(false);
setIconImage(Toolkit.getDefaultToolkit().getImage(HotelReservationGUI.class.getResource("/text/Hotel Icon.png")));
setTitle("Hotel Reservation");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 500, 500);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu MenuFile = new JMenu("File");
menuBar.add(MenuFile);
JMenuItem MenuItemExit = new JMenuItem("Exit");
MenuFile.add(MenuItemExit);
MenuItemExit.setToolTipText("Exit application");
contentPane.setBackground(UIManager.getColor("Button.light"));
contentPane.setBorder(new EmptyBorder(0, 0, 0, 0));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextPane WelcomeToHotel = new JTextPane();
WelcomeToHotel.setBackground(UIManager.getColor("Button.light"));
WelcomeToHotel.setFont(new Font("Tahoma", Font.BOLD, 13));
WelcomeToHotel.setText("Welcome to Hotel Reservation!");
WelcomeToHotel.setBounds(10, 10, 208, 20);
contentPane.add(WelcomeToHotel);
MenuItemExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
JTextPane EnterYourNumber = new JTextPane();
EnterYourNumber.setFont(new Font("Tahoma", Font.BOLD, 13));
EnterYourNumber.setBounds(10, 40, 198, 44);
EnterYourNumber.setBackground(UIManager.getColor("Button.light"));
EnterYourNumber.setForeground(Color.BLACK);
EnterYourNumber.setEditable(false);
EnterYourNumber.setText("Enter your number of guests:");
contentPane.add(EnterYourNumber);
Choice Guests = new Choice();
Guests.setBounds(214, 40, 64, 20);
contentPane.add(Guests);
Guests.add("1");
Guests.add("2");
Guests.add("3");
Guests.add("4");
Guests.add("5");
Guests.add("6");
Guests.add("7");
Guests.add("8");
Object GuestsNumber = ((Choice) Guests).getSelectedItem();
JButton CheckRoomAvailability = new JButton("Check Room Availability");
CheckRoomAvailability.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
if (ArrayUtils.contains(RoomsArray, 0))
{
//This button won't appear while in the If Statement.
JButton ReserveRoomYes = new JButton("Yes");
ReserveRoomYes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
}
});
ReserveRoomYes.setBounds(288, 300, 55, 20);
contentPane.add(ReserveRoomYes);
JTextPane RoomAvailableTXT = new JTextPane();
RoomAvailableTXT.setFont(new Font("Tahoma", Font.BOLD, 13));
RoomAvailableTXT.setText("A room is available! \nWould you like to reserve this room?");
RoomAvailableTXT.setBounds(10, 70, 300, 200);
contentPane.add(RoomAvailableTXT);
}
else
{
JTextPane RoomNotAvailableTXT = new JTextPane();
RoomNotAvailableTXT.setFont(new Font("Tahoma", Font.BOLD, 13));
RoomNotAvailableTXT.setText("No rooms are available, please try again later.");
RoomNotAvailableTXT.setBounds(10, 70, 300, 200);
contentPane.add(RoomNotAvailableTXT);
}
}
});
CheckRoomAvailability.setBounds(288, 40, 170, 20);
contentPane.add(CheckRoomAvailability);
}
}
答案 0 :(得分:1)
另一种方法是首先在页面上创建按钮,然后立即创建
jButton.setVisible(false);
您的GUI将显示为按钮不存在。每当按钮显示时,您只需调用
jButton.setVisible(true);
完成按钮后,您可以再次隐藏它。
答案 1 :(得分:0)
您可以通过添加
来解决此问题contentPane.repaint();
在所述按钮的代码之后。