我试图在Java中创建一个GUI窗口,在顶部的“家”,“预订”,“来宾”,“房间”中有4个选项卡。但是,问题是我不确定如何在特定选项卡中实现按钮。

更具体地说,我创建了一个名为GuestTab的类,并创建了1个按钮和1个文本字段,但是我不知道如何将这些信息传达给Guest选项卡。

因此,如果我不太清楚,请单击“访客”选项卡时,我要具有在GuestTab类中创建的按钮和文本字段。

我将放置“ SEP”类和“ GuestTab”类的代码,在其中添加我的GUI主设计,并在其中添加“ Guest”选项卡的内容。

SEP.java:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SEP extends JFrame
{

  private GuestTab GuestTab;
  private JTabbedPane tPane;
  private MyButtonListener buttonListener;
  private MyTabListener tabListener;
  private JMenuBar menuBar;
  private JMenu fileMenu;
  private JMenuItem exitMenuItem;

  public SEP()
  {
    super("Deer Alley Hotel");

    buttonListener = new MyButtonListener();
    tabListener = new MyTabListener();

    exitMenuItem = new JMenuItem("Exit");
    exitMenuItem.addActionListener(buttonListener);

    fileMenu = new JMenu("File");

    setJMenuBar(menuBar);

    tPane = new JTabbedPane();

    tPane.addTab("                Home             ", new JPanel(
         new FlowLayout()));
    tPane.addTab("               Booking           ", new JPanel(
        new FlowLayout()));
    tPane.addTab("              Guest           ", GuestTab);
    tPane.addTab("              Room        ", new JPanel(new FlowLayout()));
    tPane.addChangeListener(tabListener);

    add(tPane);

    setSize(575, 452);
    setVisible(true);
    setResizable(false);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

  }

  private class MyButtonListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == exitMenuItem)
      {
        int choice = JOptionPane.showConfirmDialog(null,
              "Do you really want to exit the program?", "Exit",
              JOptionPane.YES_NO_OPTION);

        if (choice == JOptionPane.YES_OPTION)
        {
          System.exit(0);
        }
      }
    }
  }

  public class MyTabListener implements ChangeListener
  {
    public void stateChanged(ChangeEvent e)
    {

    }
  }
}


GuestTab.java:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class GuestTab extends JPanel
{
  private JButton button1;
  private JButton button2;
  private JButton button3;
  private JButton button4;

  private JPanel panel1;
  private JPanel panel2;

  private JTextField text;

  public GuestTab()
  {
    panel1 = new JPanel();

    button1 = new JButton("Edit Note");
    button2 = new JButton("Check out");
    button3 = new JButton("Edit Form");
    button4 = new JButton("Search");

    text = new JTextField(15);

    add(panel1);
    panel1.setPreferredSize(new Dimension(280, 300));
    panel1.add(button4);
    panel1.add(text);
    setVisible(true);
  }

 }

最佳答案

您没有正确定义GuestTab。

在这一行:

tPane.addTab("Guest", guestTab);


更改为:

tPane.addTab("Guest", new GuestTab());


或者,您可以初始化JPanel。你从来没有真正做到这一点,只是说
有一个来宾标签,但您永远不会对其进行任何操作,因此您也可以执行以下操作:

private GuestTab guestTab;


然后再:

guestTab = new GuestTab();


旁注,不要对变量定义和类调用使用相同的大小写。确保您使用的骆驼包合适。

10-07 19:42