Java,弹出JTabbedPane

时间:2015-12-14 19:56:03

标签: java swing jtabbedpane

当有人输入密码并弹出一个新密码时,我希望我的原始窗口关闭,或者如果您有更好的建议,请告诉我。这是我的代码,

主要班级,

package notebook;

import java.awt.EventQueue;

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;


public class mainPage extends JDialog  {
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainPage frame = new mainPage();
                    frame.setVisible(true);
                    frame.setResizable(false);
                    Image icon = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB_PRE);
                    frame.setIconImage(icon);
                    frame.setTitle("Notebook");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws IOException 
     */
    public mainPage() throws IOException {
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 560, 390);      
        JLabel contentPane = new JLabel(
                new ImageIcon(
                        ImageIO.read(new File(
                                "C:\\Users\\Gianmarco\\workspace\\notebook\\src\\notebook\\cool_cat.jpg"))));
        contentPane.setBorder(new CompoundBorder());
        setContentPane(contentPane);
        contentPane.setLayout(null);


        JLabel lblEnterPassword = new JLabel(" Enter Password");
        lblEnterPassword.setForeground(Color.LIGHT_GRAY);
        lblEnterPassword.setBackground(Color.DARK_GRAY);
        lblEnterPassword.setOpaque(true);
        lblEnterPassword.setBounds(230, 60, 100, 15);
        contentPane.add(lblEnterPassword);

        security sInfo = new security();


        textField = new JPasswordField(10);
        nbTab notebook = new nbTab();

        Action action = new AbstractAction()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String textFieldValue = textField.getText();
                if (sInfo.checkPassword(textFieldValue)){ 
                    System.out.println("working");
                    notebook.setVisible(true);
                    //dispose();
                }
            }
        };

        JPanel panel = new JPanel(); 
        textField.setBounds(230, 85, 100, 15);
        contentPane.add(textField);
        contentPane.add(panel);
        textField.setColumns(10);
        textField.addActionListener(action);


    }
}

密码类

package notebook;

public class security {

    private String password = "kitten";

    protected boolean checkPassword(String x){
        if(x.length()<15 && x.equals(password)) return true;
        return false;
    }

}

JTabbedPane类,

package notebook;

import javax.swing.JTabbedPane;
import javax.swing.JEditorPane;
import javax.swing.JList;
import javax.swing.JButton;

public class nbTab<E> extends JTabbedPane {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * Create the panel.
     */

    public nbTab() {

        JEditorPane editorPane = new JEditorPane();
        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(480, 345, 40, 30);
        editorPane.add(btnNewButton);
        editorPane.setBounds(80, 45, 400, 300);

        addTab("New tab", null, editorPane, null);


        JList<? extends E> list = new JList();
        addTab("New tab", null, list, null);



    }

}

在我的主要课程中,在第76-82行(动作事件列表器所在的位置)我希望关闭当前窗口并打开一个新的笔记本窗口。我使用dispose()来关闭密码窗口。然后我尝试用setVisible(),setSelectedComponent和setSelectedIndex打开JTabbedPane但是我要么错误地使用它们,要么必须有更好的方法来执行此操作,因为它不起作用。任何建议都表示感谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

正如MadProgrammerFrakcool所建议的那样,CardLayout布局管理器在您的案例中是一个有趣的选项。有关Swing的几个布局管理器的详细介绍,请访问:A Visual Guide to Layout Managers

您可以使用以下代码了解它的工作原理。我对您的主要应用程序类进行了一些修改:

  • 主要课程现在从JFrame(而不是JDialog)延伸。
  • 使用了更多面板和布局管理器。
  • 在Java类名称中,通常以大写字母开头,因此我将您的类重命名为MainPageNotebookTabSecurity

以下是修改后的MainPage类:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class MainPage extends JFrame {
    private static final String LOGIN_PANEL_ID = "Login panel";
    private static final String NOTEBOOK_ID = "Notebook tabbed pane";

    private JPanel mainPanel;
    private CardLayout cardLayout;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainPage frame = new MainPage();
                    frame.setResizable(false);
                    Image icon = new BufferedImage(1, 1,
                                                   BufferedImage.TYPE_INT_ARGB_PRE);
                    frame.setIconImage(icon);
                    frame.setTitle("Notebook");
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     * @throws IOException
     */
    public MainPage() throws IOException {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 560, 390);

        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);

        mainPanel.add(createLoginPanel(), LOGIN_PANEL_ID);
        mainPanel.add(new NotebookTab(), NOTEBOOK_ID);

        getContentPane().add(mainPanel);
    }

    private JPanel createLoginPanel() throws IOException {
        JPanel loginPanel = new JPanel(new BorderLayout());

        JPanel passwordPanel = new JPanel();
        passwordPanel.setLayout(new BoxLayout(passwordPanel, BoxLayout.PAGE_AXIS));

        JLabel lblEnterPassword = new JLabel("Enter Password");
        lblEnterPassword.setForeground(Color.LIGHT_GRAY);
        lblEnterPassword.setBackground(Color.DARK_GRAY);
        lblEnterPassword.setOpaque(true);
        lblEnterPassword.setHorizontalAlignment(SwingConstants.CENTER);
        lblEnterPassword.setMaximumSize(new Dimension(100, 16));
        lblEnterPassword.setAlignmentX(Component.CENTER_ALIGNMENT);

        JTextField textField = new JPasswordField(10);
        textField.setMaximumSize(new Dimension(100, 16));
        textField.setAlignmentX(Component.CENTER_ALIGNMENT);

        passwordPanel.add(Box.createRigidArea(new Dimension(0, 42)));
        passwordPanel.add(lblEnterPassword);
        passwordPanel.add(Box.createRigidArea(new Dimension(0, 10)));
        passwordPanel.add(textField);

        loginPanel.add(passwordPanel, BorderLayout.NORTH);

        Action loginAction = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (new Security().checkPassword(textField.getText())) {
                    System.out.println("working");
                    cardLayout.show(mainPanel, NOTEBOOK_ID);
                }
            }
        };

        textField.addActionListener(loginAction);

        String imagePath = "C:\\Users\\Gianmarco\\workspace\\" +
                           "notebook\\src\\notebook\\cool_cat.jpg";
        BufferedImage bufferedImage = ImageIO.read(new File(imagePath));
        JLabel imageLabel = new JLabel(new ImageIcon(bufferedImage));

        loginPanel.add(imageLabel, BorderLayout.CENTER);

        return loginPanel;
    }
}