2015-12-14 48 views
1

我想我的原始窗口關閉,當有人輸入密碼和新的彈出,或者如果你有更好的建議,請告訴我。這裏是我的代碼,Java,使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,但是我要麼不正確地使用它們,要麼有一些更好的方法來做到這一點,因爲它不起作用。任何建議表示感謝球員感謝所有幫助。

+0

所有的第一次,不要使用空佈局。而是使用適當的[佈局管理器](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)(或它們的組合)。有關這方面的更多信息,請查看[空佈局是邪惡的](http://www.fredosaurus.com/notes-java/GUI/layouts/nulllayout.html)和[爲什麼在Swing中使用空佈局會皺起眉頭? ](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Frakcool

+2

使用CardLayout來切換視圖或一些對話框kind顯示登錄視圖 – MadProgrammer

+0

避免使用空佈局,他們更麻煩,那麼他們是值得的 – MadProgrammer

回答

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; 
    } 
} 
+0

嘿謝謝一堆。我實際上開始研究它,並做了另一個記事本項目,但我一定會看看這個。非常感謝您的幫助。 –

+0

不客氣。祝你的項目好運! –