Tak jak w temacie stworzyłem taki mały niezbędnik na zaliczenie i jest w nim kalkulator i po otwarciu go i zamknięciu zamyka się również okno z tym niezbędnikiem jak to zrobić żeby zamykało się tylko okno kalkulatora oto kody:
Aplikacja.java
package aplikacja;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
public class Aplikacja extends JFrame implements ActionListener
{
protected JButton bkonto, bkalk, bedytor, btemp, bwyjscie;
JMenuBar menuBar;
JMenu menuPlik, menuPomoc;
JMenuItem mWyjscie, mOProgramie;
public Aplikacja()
{
setSize(790,560);
setTitle("Niezbędnik");
menuBar = new JMenuBar();
menuPlik = new JMenu("Plik");
mWyjscie = new JMenuItem("Wyjście");
menuPlik.add(mWyjscie);
mWyjscie.addActionListener(this);
mWyjscie.setAccelerator(KeyStroke.getKeyStroke("ctrl X"));
menuPomoc = new JMenu("Pomoc");
mOProgramie = new JMenuItem("O Programie");
menuPomoc.add(mOProgramie);
mOProgramie.addActionListener(this);
setJMenuBar(menuBar);
menuBar.add(menuPlik);
menuBar.add(Box.createHorizontalGlue());
menuBar.add(menuPomoc);
ImageIcon konto = new ImageIcon("konto.png");
ImageIcon kalk = new ImageIcon("kalkulator.png");
ImageIcon wyjscie = new ImageIcon("wyjscie.png");
ImageIcon temp = new ImageIcon("temp.png");
ImageIcon edytor = new ImageIcon("edytor.png");
setLayout(new GridLayout(2, 4));
bkalk = new JButton(kalk);
bkalk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Kalkulator().setVisible(true);
}
});
btemp = new JButton(temp);
btemp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Temperatura().setVisible(true);
}
});
bedytor = new JButton(edytor);
bedytor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Edytor().setVisible(true);
}
});
bkonto = new JButton(konto);
bkonto.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Konto().setVisible(true);
}
});
bwyjscie = new JButton(wyjscie);
bwyjscie.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
bkonto.setToolTipText("Tworzenie konta");
bkalk.setToolTipText("Kalkulator");
bwyjscie.setToolTipText("Wyjście");
btemp.setToolTipText("Przeliczanie stopni Celsiusza na Fahrenheita");
bedytor.setToolTipText("Edytor tekstu");
add(bkonto);
add(bkalk);
add(bwyjscie);
add(btemp);
add(bedytor);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object z = e.getSource();
if (z==mWyjscie)
{
int odp = JOptionPane.showConfirmDialog(null, "Czy na pewno chcesz wyjść?", "Wyjście", JOptionPane.YES_NO_OPTION);
if (odp==JOptionPane.YES_OPTION)
dispose();
}
if (z==mOProgramie)
JOptionPane.showMessageDialog(null, "Niezbędnik\nProgram na zaliczenie.\nWersja: 1.0", "Niezbędnik 1.0", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args)
{
Aplikacja aplikacja = new Aplikacja();
aplikacja.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aplikacja.setVisible(true);
}
}
Kalkulator.java
package aplikacja;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JFrame;
public class Kalkulator extends JFrame {
public Kalkulator() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
b4 = new javax.swing.JButton();
b7 = new javax.swing.JButton();
b1 = new javax.swing.JButton();
b5 = new javax.swing.JButton();
b8 = new javax.swing.JButton();
b3 = new javax.swing.JButton();
b6 = new javax.swing.JButton();
b9 = new javax.swing.JButton();
b0 = new javax.swing.JButton();
bC = new javax.swing.JButton();
bWynik = new javax.swing.JButton();
bPlus = new javax.swing.JButton();
bMinus = new javax.swing.JButton();
b2 = new javax.swing.JButton();
bDziel = new javax.swing.JButton();
bMnoz = new javax.swing.JButton();
bKropka = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jTextField1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jTextField1KeyPressed(evt);
}
});
b4.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b4.setText("4");
b4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b4ActionPerformed(evt);
}
});
b7.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b7.setText("7");
b7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b7ActionPerformed(evt);
}
});
b1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b1.setText("1");
b1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b1ActionPerformed(evt);
}
});
b5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b5.setText("5");
b5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b5ActionPerformed(evt);
}
});
b8.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b8.setText("8");
b8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b8ActionPerformed(evt);
}
});
b3.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b3.setText("3");
b3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b3ActionPerformed(evt);
}
});
b6.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b6.setText("6");
b6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b6ActionPerformed(evt);
}
});
b9.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b9.setText("9");
b9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b9ActionPerformed(evt);
}
});
b0.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b0.setText("0");
b0.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b0ActionPerformed(evt);
}
});
bC.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
bC.setText("C");
bC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCActionPerformed(evt);
}
});
bWynik.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
bWynik.setText("=");
bWynik.setAutoscrolls(true);
bWynik.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bWynikActionPerformed(evt);
}
});
bPlus.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
bPlus.setText("+");
bPlus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bPlusActionPerformed(evt);
}
});
bMinus.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
bMinus.setText("-");
bMinus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bMinusActionPerformed(evt);
}
});
b2.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
b2.setText("2");
b2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b2ActionPerformed(evt);
}
});
bDziel.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
bDziel.setText("/");
bDziel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bDzielActionPerformed(evt);
}
});
bMnoz.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
bMnoz.setText("*");
bMnoz.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bMnozActionPerformed(evt);
}
});
bKropka.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
bKropka.setText(".");
bKropka.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bKropkaActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(b4, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b7, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b0, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(b5, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b8, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(b6, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(b3, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(b9, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bMnoz, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addComponent(bC, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addComponent(bKropka, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bDziel, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bWynik, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(24, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField1)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(41, 41, 41)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(b7, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b3, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bPlus, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b5, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b6, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b4, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bMinus, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b8, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(b9, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bMnoz, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b0, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bC, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bDziel, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bKropka, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(bWynik, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(25, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '4');
}
private void b7ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '7');
}
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '1');
}
private void b5ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '5');
}
private void b8ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '8');
}
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '3');
}
private void b6ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '6');
}
private void b9ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '9');
}
private void b0ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '0');
}
private void bCActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
}
private void bWynikActionPerformed(java.awt.event.ActionEvent evt) {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = jTextField1.getText();
try {
jTextField1.setText(String.valueOf(engine.eval(foo)));
} catch (ScriptException ex) {
Logger.getLogger(Kalkulator.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void bPlusActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '+');
}
private void bMinusActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '-');
}
private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '2');
}
private void bDzielActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '/');
}
private void bMnozActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '*');
}
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
switch (evt.getKeyChar())
{
case '\n':
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String foo = jTextField1.getText();
try {
jTextField1.setText(String.valueOf(engine.eval(foo)));
} catch (ScriptException ex) {
Logger.getLogger(Kalkulator.class.getName()).log(Level.SEVERE, null, ex);
}
break;
}
}
private void bKropkaActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText(jTextField1.getText() + '.');
}
public static void main(String args[]) {
Kalkulator kalk = new Kalkulator();
kalk.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
kalk.setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JButton b0;
private javax.swing.JButton b1;
private javax.swing.JButton b2;
private javax.swing.JButton b3;
private javax.swing.JButton b4;
private javax.swing.JButton b5;
private javax.swing.JButton b6;
private javax.swing.JButton b7;
private javax.swing.JButton b8;
private javax.swing.JButton b9;
private javax.swing.JButton bC;
private javax.swing.JButton bDziel;
private javax.swing.JButton bKropka;
private javax.swing.JButton bMinus;
private javax.swing.JButton bMnoz;
private javax.swing.JButton bPlus;
private javax.swing.JButton bWynik;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}