Witam!
Słuchajcie borykam się z małym problemem.
Tworzę sobie GUI w netBeansowym GUI builderze i napotkałem na mały problem. główna ramka posiada kilka funkcji w tym button wywołujący JDialog z opcją logowania.
JDialog stworzyłem w nowym pliku, a nie tworzyłem nowej klasy wewnątrz kodu ramki. Teraz gdy za pomocą przycisku zaloguj próbuję wywołać JDialog konsola nie rzyga żadnym błędem, ale okienko też się nie ukazuje.
Wspomóżcie!
Ramka główna:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package menutestowe;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import static java.time.Clock.system;
import java.util.Arrays;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author jarek
*/
public class NewJFrame extends javax.swing.JFrame {
String wybranyTekst;
Object panelHasla = null;
public NewJFrame() {
initComponents();
setResizable(false);
setLocation(500, 200);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPopupMenu1 = new javax.swing.JPopupMenu();
mKopiuj = new javax.swing.JMenuItem();
mWklej = new javax.swing.JMenuItem();
mDolacz = new javax.swing.JMenuItem();
jScrollPane1 = new javax.swing.JScrollPane();
notatnik = new javax.swing.JTextArea();
bSzukaj = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
tSzukany = new javax.swing.JTextArea();
colorCombo = new javax.swing.JComboBox();
mKolor = new javax.swing.JButton();
pHaslo = new javax.swing.JPasswordField();
bZaloguj = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
menuPlik = new javax.swing.JMenu();
mOtworz = new javax.swing.JMenuItem();
mZapisz = new javax.swing.JMenuItem();
mWyjscie = new javax.swing.JMenuItem();
menuOpcje = new javax.swing.JMenu();
mNarz1 = new javax.swing.JMenuItem();
mNarz2 = new javax.swing.JMenuItem();
chOpcja1 = new javax.swing.JCheckBoxMenuItem();
menuAbout = new javax.swing.JMenu();
mAbout = new javax.swing.JMenuItem();
jPopupMenu1.setComponentPopupMenu(jPopupMenu1);
jPopupMenu1.setNextFocusableComponent(jScrollPane1);
jPopupMenu1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jPopupMenu1MousePressed(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
jPopupMenu1MouseReleased(evt);
}
});
notatnik.add(jPopupMenu1);
notatnik.setComponentPopupMenu(jPopupMenu1);
mKopiuj.setText("Kopiuj");
mKopiuj.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mKopiujActionPerformed(evt);
}
});
jPopupMenu1.add(mKopiuj);
mWklej.setText("Wklej");
mWklej.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mWklejActionPerformed(evt);
}
});
jPopupMenu1.add(mWklej);
mDolacz.setText("Dołącz");
mDolacz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mDolaczActionPerformed(evt);
}
});
jPopupMenu1.add(mDolacz);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent evt) {
formKeyTyped(evt);
}
});
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setEnabled(false);
notatnik.setColumns(20);
notatnik.setRows(5);
jScrollPane1.setViewportView(notatnik);
bSzukaj.setText("Szukaj");
bSzukaj.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bSzukajActionPerformed(evt);
}
});
jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
tSzukany.setColumns(20);
tSzukany.setRows(5);
jScrollPane2.setViewportView(tSzukany);
colorCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Czarny", "Czerwony", "Zielony" }));
colorCombo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
colorComboActionPerformed(evt);
}
});
mKolor.setText("Zmień Kolor");
mKolor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mKolorActionPerformed(evt);
}
});
pHaslo.setText("jPasswordField1");
pHaslo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pHasloActionPerformed(evt);
}
});
bZaloguj.setText("Zaloguj się!");
bZaloguj.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bZalogujActionPerformed(evt);
}
});
menuPlik.setText("Plik");
mOtworz.setText("Otwórz");
mOtworz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mOtworzActionPerformed(evt);
}
});
menuPlik.add(mOtworz);
mZapisz.setText("Zapisz");
mZapisz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mZapiszActionPerformed(evt);
}
});
menuPlik.add(mZapisz);
menuPlik.addSeparator();
mWyjscie.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
mWyjscie.setText("Wyjdź");
mWyjscie.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mWyjscieActionPerformed(evt);
}
});
menuPlik.add(mWyjscie);
jMenuBar1.add(menuPlik);
menuOpcje.setText("Opcje");
mNarz1.setText("Narzędzie 1");
menuOpcje.add(mNarz1);
mNarz2.setText("Narzędzie 2");
menuOpcje.add(mNarz2);
chOpcja1.setSelected(true);
chOpcja1.setText("Opcja1");
chOpcja1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chOpcja1ActionPerformed(evt);
}
});
menuOpcje.add(chOpcja1);
jMenuBar1.add(menuOpcje);
jMenuBar1.add(Box.createHorizontalGlue());
menuAbout.setText("O Programie");
mAbout.setText("O Programie");
mAbout.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mAboutActionPerformed(evt);
}
});
menuAbout.add(mAbout);
jMenuBar1.add(menuAbout);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(149, 149, 149)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(bSzukaj)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(mKolor))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(colorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(76, 76, 76)
.addComponent(pHaslo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(131, 131, 131)
.addComponent(bZaloguj)))
.addContainerGap(107, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(108, 108, 108)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(colorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(bSzukaj, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addComponent(mKolor)))
.addGroup(layout.createSequentialGroup()
.addGap(93, 93, 93)
.addComponent(pHaslo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(146, 146, 146)
.addComponent(bZaloguj)))
.addContainerGap(128, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void mAboutActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, "Program testowy \n Wersja 1.0");
}
private void mOtworzActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File plik = fc.getSelectedFile();
try
{
//JOptionPane.showMessageDialog(null, "Zapisany plik to: " + plik);
Scanner skaner = new Scanner(plik);
while (skaner.hasNextLine())
{
notatnik.append(skaner.nextLine() + "\n");
}
} catch (FileNotFoundException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void mZapiszActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
{
File plik = fc.getSelectedFile();
try
{
PrintWriter pw = new PrintWriter(plik);
Scanner skaner = new Scanner(notatnik.getText());
while (skaner.hasNextLine())
{
pw.println(skaner.nextLine() + "\n");
}
pw.close();
}
catch (FileNotFoundException ex)
{
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void mWyjscieActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
private void chOpcja1ActionPerformed(java.awt.event.ActionEvent evt) {
if (chOpcja1.isSelected())
{
mNarz1.setEnabled(true);
}
else
{
mNarz1.setEnabled(false);
}
}
private void bSzukajActionPerformed(java.awt.event.ActionEvent evt) {
String tekst = notatnik.getText();
String szukane = tSzukany.getText();
String wystapienia = "";
int i = 0;
int index;
int startIndex = 0;
while((index = tekst.indexOf(szukane, startIndex)) != -1 )
{
startIndex = index + szukane.length();
i++;
wystapienia = wystapienia + " " + index;
}
JOptionPane.showMessageDialog(null, szukane + " wystąpiło " + i + " razy " + wystapienia);
}
private void mKopiujActionPerformed(java.awt.event.ActionEvent evt) {
wybranyTekst = notatnik.getSelectedText();
}
private void jPopupMenu1MousePressed(java.awt.event.MouseEvent evt) {
if (evt.isPopupTrigger())
{
jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
private void jPopupMenu1MouseReleased(java.awt.event.MouseEvent evt) {
if (evt.isPopupTrigger())
{
jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
}
}
private void formKeyTyped(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void mWklejActionPerformed(java.awt.event.ActionEvent evt) {
notatnik.insert(wybranyTekst, notatnik.getCaretPosition());
}
private void mDolaczActionPerformed(java.awt.event.ActionEvent evt) {
notatnik.append("\n" + wybranyTekst);
}
private void colorComboActionPerformed(java.awt.event.ActionEvent evt) {
String color = (String) colorCombo.getSelectedItem();
if (color.equals("Czarny"))
{
notatnik.setForeground(Color.BLACK);
}
else if (color.equals("Zielony"))
{
notatnik.setForeground(Color.GREEN);
}
else if (color.equals("Czerwony"))
{
notatnik.setForeground(Color.RED);
}
}
private void mKolorActionPerformed(java.awt.event.ActionEvent evt) {
Color wybranyKolor = JColorChooser.showDialog(null, "Wybierz kolor", Color.black);
notatnik.setForeground(wybranyKolor);
}
private void pHasloActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(null, new String(pHaslo.getPassword()));
}
private void bZalogujActionPerformed(java.awt.event.ActionEvent evt) {
if(panelHasla == null)
panelHasla = new PanelHasla(this);
setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bSzukaj;
private javax.swing.JButton bZaloguj;
private javax.swing.JCheckBoxMenuItem chOpcja1;
private javax.swing.JComboBox colorCombo;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JPopupMenu jPopupMenu1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JMenuItem mAbout;
private javax.swing.JMenuItem mDolacz;
private javax.swing.JButton mKolor;
private javax.swing.JMenuItem mKopiuj;
private javax.swing.JMenuItem mNarz1;
private javax.swing.JMenuItem mNarz2;
private javax.swing.JMenuItem mOtworz;
private javax.swing.JMenuItem mWklej;
private javax.swing.JMenuItem mWyjscie;
private javax.swing.JMenuItem mZapisz;
private javax.swing.JMenu menuAbout;
private javax.swing.JMenu menuOpcje;
private javax.swing.JMenu menuPlik;
private javax.swing.JTextArea notatnik;
private javax.swing.JPasswordField pHaslo;
private javax.swing.JTextArea tSzukany;
// End of variables declaration
}
JDialog logowanie:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package menutestowe;
import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.JFrame;
/**
*
* @author jarek
*/
public class PanelHasla extends JDialog {
/**
* Creates new form PanelHasla
*/
public PanelHasla(NewJFrame owner) {
super(owner, "Wprowadzanie hasła", true );
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
bOk = new javax.swing.JButton();
bCancel = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel2");
jTextField1.setText("jTextField1");
jTextField2.setText("jTextField2");
bOk.setText("OK");
bCancel.setText("Cancel");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(73, 73, 73)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(bOk, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(bCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(93, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jLabel1))
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(11, 11, 11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jLabel2))
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bOk)
.addComponent(bCancel))
.addContainerGap(92, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* Closes the dialog
*/
private void closeDialog(java.awt.event.WindowEvent evt) {
setVisible(false);
dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
PanelHasla dialog = new PanelHasla(new NewJFrame());
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bCancel;
private javax.swing.JButton bOk;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}