我想我的原始窗口關閉,當有人輸入密碼和新的彈出,或者如果你有更好的建議,請告訴我。這裏是我的代碼,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,但是我要麼不正確地使用它們,要麼有一些更好的方法來做到這一點,因爲它不起作用。任何建議表示感謝球員感謝所有幫助。
所有的第一次,不要使用空佈局。而是使用適當的[佈局管理器](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
使用CardLayout來切換視圖或一些對話框kind顯示登錄視圖 – MadProgrammer
避免使用空佈局,他們更麻煩,那麼他們是值得的 – MadProgrammer