Witam próbuje napisać program, którym po zalogowaniu użytkownik będzie miał dwie opcje więcej do wyboru tzn. 2 buttony tj. jButton3 i 4 będą widoczne. Niestety troszkę sobie z tym nie radzę proszę o pomoc.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.*;
public class Konwerter
{
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new KonwerterFrame().setVisible(true);
}
});
}
}
class KonwerterFrame extends JFrame
{
public KonwerterFrame()
{
initComponents();
setTitle("Konwerter Jednostek");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setSize((screenWidth/3)-22, (screenHeight/2)-50);
setLocationByPlatform(true);
// Opcja Zamknij
jMenuItem2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
// Opcja Zaloguj
jMenuItem1.addActionListener(new ConnectAction());
// Pomoc
jMenuItem3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (dialog2 == null)
dialog2 = new PomocDialog(KonwerterFrame.this);
dialog2.setVisible(true);
}
});
// Autorzy
jMenuItem4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if (dialog3 == null)
dialog3 = new AutorzyDialog(KonwerterFrame.this);
dialog3.setVisible(true);
}
});
}
private PasswordChooser dialog = null;
private PomocDialog dialog2;
private AutorzyDialog dialog3;
private BladDialog dialog4;
private class ConnectAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
// jeśli to 1. raz tworzy okno dialogowe
if (dialog == null) dialog = new PasswordChooser();
// ustawienie wartosci domyslnych
dialog.setUser(new User("admin", null));
// wyswietlenie okna dialogowego
if (dialog.showDialog(KonwerterFrame.this, "Zaloguj_tytuł"))
{
// pobranie danych uzytkownika w przypaku zatwierdzenia
User u = dialog.getUser();
}
}
}
class PomocDialog extends JDialog
{
public PomocDialog(JFrame owner)
{
super(owner, "Pomoc", true);
JButton ok = new JButton("Ok");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
}
});
JPanel panelPomoc = new JPanel();
JPanel panelPomoc2 = new JPanel();
panelPomoc.add(ok, BorderLayout.SOUTH);
add(panelPomoc, BorderLayout.SOUTH);
add(panelPomoc2);
panelPomoc.setBackground(new Color(102, 153, 255));
panelPomoc2.setBackground(new Color(102, 153, 255));
setSize(250,150);
}
}
class AutorzyDialog extends JDialog
{
public AutorzyDialog(JFrame owner)
{
super(owner, "Autorzy", true);
JTextArea autorzy = new JTextArea();
JTextArea autorzy1 = new JTextArea();
autorzy1.setText("AUTORZY:");
autorzy1.setEditable(false);
autorzy1.setBackground(new Color(102, 153, 255));
autorzy1.setFont(new Font("Trebuchet MS", 1, 16));
autorzy.setText("Konrad Jasionowicz \nTomasz Karwat");
autorzy.setEditable(false);
autorzy.setBackground(new Color(102, 153, 255));
autorzy.setFont(new Font("Trebuchet MS", 1, 16));
JButton ok = new JButton("Ok");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
}
});
JPanel panelA = new JPanel();
JPanel panelA1 = new JPanel();
panelA1.add(autorzy1, BorderLayout.CENTER);
panelA1.add(autorzy, BorderLayout.CENTER);
add(panelA1, BorderLayout.CENTER);
panelA1.setBackground(new Color(102, 153, 255));
panelA.add(ok, BorderLayout.SOUTH);
add(panelA, BorderLayout.SOUTH);
panelA.setBackground(new Color(102, 153, 255));
setSize(250,150);
}
}
class BladDialog extends JDialog
{
public BladDialog(JFrame owner)
{
super(owner, "Blad", true);
JTextArea blad = new JTextArea();
blad.setText("WPISZ LICZBĘ POPRAWNIE!");
blad.setEditable(false);
blad.setBackground(new Color(102, 153, 255));
blad.setFont(new Font("Trebuchet MS", 1, 16));
JButton ok = new JButton("Ok");
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
}
});
JPanel panelB = new JPanel();
JPanel panelB1 = new JPanel();
panelB1.add(blad, BorderLayout.CENTER);
add(panelB1, BorderLayout.CENTER);
panelB1.setBackground(new Color(102, 153, 255));
panelB.add(ok, BorderLayout.SOUTH);
add(panelB, BorderLayout.SOUTH);
panelB.setBackground(new Color(102, 153, 255));
setSize(250,120);
}
}
private void initComponents()
{
jPanel1 = new JPanel();
jComboBox1 = new JComboBox();
jComboBox2 = new JComboBox();
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel4 = new JLabel();
jLabel3 = new JLabel();
jButton1 = new JButton();
jButton2 = new JButton();
jButton3 = new JButton();
jButton4 = new JButton();
jMenuBar1 = new JMenuBar();
jMenu1 = new JMenu();
jMenu2 = new JMenu();
jMenuItem1 = new JMenuItem();
jMenuItem2 = new JMenuItem();
jMenuItem3 = new JMenuItem();
jMenuItem4 = new JMenuItem();
jTextField1 = new JTextField();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new Color(102, 153, 255));
jButton1.setBackground(new Color(153, 204, 255));
jButton1.setLabel("PRZELICZ");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
przeliczanie(event);
}
});
jComboBox1.setBackground(new Color(153, 204, 255));
jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "cm", "m", "km", "in", "fd", "yd" }));
jComboBox2.setBackground(new Color(153, 204, 255));
jComboBox2.setModel(new DefaultComboBoxModel(new String[] { "cm", "m", "km", "in", "fd", "yd" }));
jLabel4.setFont(new Font("Trebuchet MS", 1, 16));
jLabel3.setFont(new Font("Trebuchet MS", 1, 16));
jLabel3.setText("WYNIK = ");
jLabel1.setFont(new Font("Trebuchet MS", 1, 16));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("ZAMIEŃ");
jLabel2.setFont(new Font("Trebuchet MS", 1, 16));
jLabel2.setText("NA");
jButton2.setBackground(new Color(153, 204, 255));
jButton2.setActionCommand("WYCZYŚĆ");
jButton2.setLabel("WYCZYŚĆ");
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
czyszczenie(event);
}
});
//if (pass == "admin")
//{
// jButton3.setVisible(true);
//}
//else if
//{
// jButton3.setVisible(false);
//}
jButton3.setBackground(new Color(153, 204, 255));
jButton3.setLabel("DODAJ");
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
dodawanie(event);
}
});
jButton4.setBackground(new Color(153, 204, 255));
jButton4.setLabel("USUŃ");
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
usuwanie(event);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(38, 38, 38)
.addComponent(jLabel2)
.addGap(40, 40, 40)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(jLabel3)))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap(39, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(44, 44, 44)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(84, 84, 84)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1)
.addComponent(jButton2)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4)))
.addGap(34, 34, 34))
);
jMenu1.setLabel("Opcje");
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem1.setLabel("Zaloguj");
jMenu1.add(jMenuItem1);
jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
jMenuItem2.setLabel("Zamknij");
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setLabel("Pomoc");
jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.ALT_MASK));
jMenuItem3.setLabel("Pomoc");
jMenu2.add(jMenuItem3);
jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.ALT_MASK));
jMenuItem4.setLabel("Autorzy");
jMenu2.add(jMenuItem4);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)
);
pack();
}
private void przeliczanie(ActionEvent event)
{
try
{
double wynik=0.0;
String spole=jTextField1.getText().trim();
double fpole=Double.parseDouble(spole);
int intComboBox1=jComboBox1.getSelectedIndex();
int intComboBox2=jComboBox2.getSelectedIndex();
if((intComboBox1==0 && intComboBox2==0)||(intComboBox1==1 && intComboBox2==1)||
(intComboBox1==2 && intComboBox2==2)||(intComboBox1==3 && intComboBox2==3)||
(intComboBox1==4 && intComboBox2==4)||(intComboBox1==5 && intComboBox2==5)||
(intComboBox1==6 && intComboBox2==6)){wynik=fpole;
}
//cm
else if(intComboBox1==0 && intComboBox2==1){wynik=fpole/100;}
else if(intComboBox1==0 && intComboBox2==2){wynik=fpole/100000;}
else if(intComboBox1==0 && intComboBox2==3){wynik=fpole/2.54;}
else if(intComboBox1==0 && intComboBox2==4){wynik=fpole/30.48;}
else if(intComboBox1==0 && intComboBox2==5){wynik=fpole/91.44;//}
//else if(intComboBox1==0 && intComboBox2==6){wynik=fpole/160934.4;
}
//m
else if(intComboBox1==1 && intComboBox2==0){wynik=fpole*100;}
else if(intComboBox1==1 && intComboBox2==2){wynik=fpole/1000;}
else if(intComboBox1==1 && intComboBox2==3){wynik=fpole*39.3700787;}
else if(intComboBox1==1 && intComboBox2==4){wynik=fpole*3.2808399;}
else if(intComboBox1==1 && intComboBox2==5){wynik=fpole*1.0936133;//}
//else if(intComboBox1==1 && intComboBox2==6){wynik=fpole/1609.344;
}
//km
else if(intComboBox1==2 && intComboBox2==0){wynik=fpole*100000;}
else if(intComboBox1==2 && intComboBox2==1){wynik=fpole*1000;}
else if(intComboBox1==2 && intComboBox2==3){wynik=fpole*39370.0787;}
else if(intComboBox1==2 && intComboBox2==4){wynik=fpole*3280.8399;}
else if(intComboBox1==2 && intComboBox2==5){wynik=fpole*1093.6133;//}
//else if(intComboBox1==2 && intComboBox2==6){wynik=fpole/1.609344;
}
//cal
else if(intComboBox1==3 && intComboBox2==0){wynik=fpole*2.54;}
else if(intComboBox1==3 && intComboBox2==1){wynik=fpole*0.0254;}
else if(intComboBox1==3 && intComboBox2==2){wynik=fpole*0.0000254;}
else if(intComboBox1==3 && intComboBox2==4){wynik=fpole*0.0833333333;}
else if(intComboBox1==3 && intComboBox2==5){wynik=fpole*0.0277777778;//}
//else if(intComboBox1==3 && intComboBox2==6){wynik=fpole/63360;
}
//stopa
else if(intComboBox1==4 && intComboBox2==0){wynik=fpole*30.48;}
else if(intComboBox1==4 && intComboBox2==1){wynik=fpole*0.3048;}
else if(intComboBox1==4 && intComboBox2==2){wynik=fpole*0.0003048;}
else if(intComboBox1==4 && intComboBox2==3){wynik=fpole*12;}
else if(intComboBox1==4 && intComboBox2==5){wynik=fpole*0.333333333;//}
//else if(intComboBox1==4 && intComboBox2==6){wynik=fpole/5280;
}
//jard
else if(intComboBox1==5 && intComboBox2==0){wynik=fpole*91.44;}
else if(intComboBox1==5 && intComboBox2==1){wynik=fpole*0.9144;}
else if(intComboBox1==5 && intComboBox2==2){wynik=fpole*0.0009144;}
else if(intComboBox1==5 && intComboBox2==3){wynik=fpole*36;}
else if(intComboBox1==5 && intComboBox2==4){wynik=fpole*3;//}
//else if(intComboBox1==5 && intComboBox2==6){wynik=fpole/1760;
}
NumberFormat nf=NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(9);
String swynik=nf.format(wynik);
jLabel4.setText(swynik+" "+jComboBox2.getSelectedItem());
}
catch (NumberFormatException e)
{
if (dialog4 == null)
dialog4 = new BladDialog(KonwerterFrame.this);
dialog4.setVisible(true);
jTextField1.setText("");
}
}
private void czyszczenie(ActionEvent event) {
jLabel4.setText("");
jTextField1.setText("");
}
private void dodawanie(ActionEvent event) {
jLabel4.setText("");
jTextField1.setText("");
}
private void usuwanie(ActionEvent event) {
jLabel4.setText("");
jTextField1.setText("");
}
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JButton jButton4;
private JComboBox jComboBox1;
private JComboBox jComboBox2;
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JMenu jMenu1;
private JMenu jMenu2;
private JMenuBar jMenuBar1;
private JMenuItem jMenuItem1;
private JMenuItem jMenuItem2;
private JMenuItem jMenuItem3;
private JMenuItem jMenuItem4;
private JPanel jPanel1;
private JTextField jTextField1;
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class PasswordChooser extends JPanel
{
public PasswordChooser()
{
setLayout(new BorderLayout());
// tworzenie panelu z nazwami użytkownika i hasła
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2,2));
panel.add(new JLabel("Nazwa użytkownika:"));
panel.add(username = new JTextField(""));
panel.add(new JLabel("Hasło:"));
panel.add(password = new JPasswordField(""));
add(panel, BorderLayout.CENTER);
// tworzenie przyciskow OK i Anuluj, ktore zamykaja okno dialogowe
JButton okButton = new JButton("Ok");
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
ok = true;
dialog.setVisible(false);
}
});
JButton cancelButton = new JButton("Anuluj");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
dialog.setVisible(false);
}
});
// dodoawanie przyciskow w poblizu poludniowej krawedzi
JPanel buttonPanel = new JPanel();
buttonPanel.add(okButton);
buttonPanel.add(cancelButton);
add(buttonPanel, BorderLayout.SOUTH);
}
// ustawia wartosci domyslne okna dialogowego
public void setUser(User u)
{
username.setText(u.getName());
}
// pobieran dane podane w oknie dialogowym
public User getUser()
{
return new User(username.getText(), password.getPassword());
}
// wyświetla panel z elementami przyjmujacymi dane od uzytkownika w oknie dialogowym
public boolean showDialog(Component parent, String title)
{
ok = false;
// lokalizacja ramki nadrzędnej
Frame owner = null;
if (parent instanceof Frame) owner = (Frame) parent;
else owner = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent);
// jeśli jest to pierwszy raz lub zmienił się użytkownik, utworzenie nowego okna dialogowego
if (dialog == null || dialog.getOwner() != owner)
{
dialog = new JDialog(owner, true);
dialog.add(this);
dialog.getRootPane().setDefaultButton(okButton);
dialog.pack();
}
// ustawienie tytułu i wyświetlanie okna dialogowego
dialog.setTitle(title);
dialog.setVisible(true);
return ok;
}
private JTextField username;
private JPasswordField password;
private JButton okButton;
private boolean ok;
private JDialog dialog;
}
public class User
{
public User(String aName, char[] aPassword)
{
name = aName;
password = aPassword;
}
public String getName()
{
return name;
}
public char[] getPassword()
{
return password;
}
public void setName(String aName)
{
name = aName;
}
public void setPassword(char[] aPassword)
{
password = aPassword;
}
private String name;
private char[] password;
}