Java Tab窗格插入另一个类

时间:2014-03-30 07:20:32

标签: java swing user-interface

我是Java GUI的新手,我只是想问一下如何使用eclipse将另一个类放在选项卡窗格中。

这是我的代码:

package tabexperiment;
import java.awt.*;
import java.awt.event.*;
import javax.swing.BorderFactory; 
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel; 
import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.BoxLayout;


public class Gamemulator extends JPanel {
    public Gamemulator() {

        super(new GridLayout(1,0));


        Border blackline, raisedetched, loweredetched,
               raisedbevel, loweredbevel, empty;

        Border paneEdge = BorderFactory.createEmptyBorder(20,100,100,100);

        blackline = BorderFactory.createLineBorder(Color.black);


        JPanel base = new JPanel();
        base.setBorder(paneEdge);
        base.setLayout(new BoxLayout(base,
                                              BoxLayout.LINE_AXIS));

        addCompForBorder(blackline, "Game Emulator",
                         base);




        //Second pane: TicTacToe
        JPanel tictactoe = new JPanel();

        tictactoe.setBorder(paneEdge);
        tictactoe.setLayout(new BoxLayout(tictactoe,
                                              BoxLayout.Y_AXIS));


        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Main", null, base, null);
        tabbedPane.addTab("TicTac", null, tictactoe, null);
        tabbedPane.setSelectedIndex(0);     
        add(tabbedPane);


    }



    void addCompForTitledBorder(TitledBorder border,
                                String description,
                                int justification,
                                int position,
                                Container container) {
        border.setTitleJustification(justification);
        border.setTitlePosition(position);
        addCompForBorder(border, description,
                         container);
    }

    void addCompForBorder(Border border,
                          String description,
                          Container container) {
        JPanel comp = new JPanel(new GridLayout(1, 1), false);
        JLabel label = new JLabel(description, JLabel.CENTER);
        comp.add(label);
        comp.setBorder(border);

        container.add(Box.createRigidArea(new Dimension(0, 10)));
        container.add(comp);
    }



    private static void createAndShowGUI() {
        //Create and set up the window.

        JFrame frame = new JFrame("Gamemulator version 1.0");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        Gamemulator newContentPane = new Gamemulator();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                createAndShowGUI();
            }


        });
    }
}

我想将我的TicTacToe类放在第二个选项卡窗格中。我怎样才能做到这一点? 另外,如何在第一个标签后的第一个边框文字后添加文字?

这是tictactoe代码:

package tabexperiment;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class TicTac extends JApplet {
  private char whoseTurn = 'X';
  private Cell[][] cells =  new Cell[3][3];

  private JLabel jlblStatus = new JLabel("X's turn to play");

  public TicTac() {
    JPanel p = new JPanel(new GridLayout(3, 3, 0, 0));
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        p.add(cells[i][j] = new Cell());

    p.setBorder(new LineBorder(Color.red, 1));
    jlblStatus.setBorder(new LineBorder(Color.yellow, 1));

    add(p, BorderLayout.CENTER);
    add(jlblStatus, BorderLayout.SOUTH);
  }

  public boolean isFull() {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
        if (cells[i][j].getToken() == ' ')
          return false;

    return true;
  }

  public boolean isWon(char token) {
    for (int i = 0; i < 3; i++)
      if ((cells[i][0].getToken() == token)
          && (cells[i][1].getToken() == token)
          && (cells[i][2].getToken() == token)) {
        return true;
      }

    for (int j = 0; j < 3; j++)
      if ((cells[0][j].getToken() ==  token)
          && (cells[1][j].getToken() == token)
          && (cells[2][j].getToken() == token)) {
        return true;
      }

    if ((cells[0][0].getToken() == token)
        && (cells[1][1].getToken() == token)
        && (cells[2][2].getToken() == token)) {
      return true;
    }

    if ((cells[0][2].getToken() == token)
        && (cells[1][1].getToken() == token)
        && (cells[2][0].getToken() == token)) {
      return true;
    }

    return false;
  }

  public class Cell extends JPanel {
    private char token = ' ';

    public Cell() {
      setBorder(new LineBorder(Color.black, 1)); 
      addMouseListener(new MouseListener()); 
    }

    /** Return token */
    public char getToken() {
      return token;
    }

    /** Set a new token */
    public void setToken(char c) {
      token = c;
      repaint();
    }

    /** Paint the cell */
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);

      if (token == 'X') {
        g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
        g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
      }
      else if (token == 'O') {
        g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
      }
    }

    private class MouseListener extends MouseAdapter {
      /** Handle mouse click on a cell */
      public void mouseClicked(MouseEvent e) {
        if (token == ' ' && whoseTurn != ' ') {
          setToken(whoseTurn); 

          if (isWon(whoseTurn)) {
            jlblStatus.setText(whoseTurn + " won! The game is over");
            whoseTurn = ' '; 
          }
          else if (isFull()) {
            jlblStatus.setText("Draw! The game is over");
            whoseTurn = ' ';
          }
          else {
            whoseTurn = (whoseTurn == 'X') ? 'O': 'X'; 
            jlblStatus.setText(whoseTurn + "'s turn"); 
          }
        }
      }
    }
  }

  /** This main method enables the applet to run as an application */
  public static void main(String[] args) {
    JFrame frame = new JFrame("TicTacToe");

    TicTac applet = new TicTac();

    frame.add(applet, BorderLayout.CENTER);

    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

感谢任何帮助。提前谢谢。

0 个答案:

没有答案