Witam tak jak w temacie jak powinna wyglądać jakaś prosta aplikacja okienkowa np włączam aplikacje są tam przyciski kalkulator, przeliczanie stopni itp i po kliknięciu otwiera mi sie osobne okno z tym co nacisnąłem? I gdzie wpisywać kod do nowych okienek? Szukałem tego ale nie potrafię nic znaleźć
jak stworzysz tak będzie :) ale sądząc po umiejętności opisania własnego problemu to nic z tego nie będzie. Zajrzyj do pierwszego lepszego tutoriala do Swinga lub JavaFx i próbuj. Nie omieszkaj zapoznać się ze wzorcem MVC
Chciałbym po prostu przykład zobaczyć jakiś, który będzie robił to co pisze... a z resztą sobie już poradzę o to się nie martw, a takie posty to sobie daruj i czytaj co piszę w pierwszej wiadomości. A takie nabijanie postów jest tutaj niepotrzebne..
Bardzo prosto wystarczy że oprogramujesz jedno kienko
a później tylko powielać.
Okno już mam i przyciski też tylko jak te przyciski zaprogramować żeby otwierały inny plik np kalkulator.java w nowym oknie?
Nie chciałem Cię obrazić, ale nie ma też co się obrażać za prawdę...
Tu możesz podejrzeć componenty i jak je łączyć
http://docs.oracle.com/javase/tutorial/uiswing/components/
A tu masz przykład z Twoim przyciskiem "na zgodę":P
JButton button = new JButton();
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new JFrame().setVisible(true);
}
});
A jak tym otworzę nową klase? Mam drugi plik w tym projekcie kalkulator i chce po nacisnieciu wlasnie tego przycisku żeby otwierał się kalkulator w nowym oknie tzn po prostu otwarcie kalkulatora. Wystarczy żeby w actionlisenerze dodać nowy obiekt kalkulator ?
jeżeli dziedziczy on po JFrame to odpowiedź brzmi tak.
new Kalkulator().setVisible(true);//pamiętaj, że tu za każdym razem tworzysz nowy obiekt, ale chyba w przypadku kalkulatora to nie powinno mieć znaczenia
Jeszcze jedno pytanie jak by to wyglądało dla dwóch przycisków? jeden kalkulator a drugi powiedzmy edytor tekstu ?
Ty tak na poważnie?
JButton button2 = new JButton();
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new EdytorTekstu().setVisible(true);
}
});
aa już kapuje myślałem, że do każdego przyciusku się w jednej lini dodaje, dzięki.
Mogę się pochwalić na razie tym co robię i proszę w razie czegoś jakieś uwagi piszcie :) taki nizbędnik w nim kalkulator zmiana stopni temp edytor tekstu i dodawanie kont. Na razie mam głwone okno i zamiana stopni temp.
Aplikacja
package aplikacja;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
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
{
JMenuBar menuBar;
JMenu menuPlik, menuPomoc;
JMenuItem mWyjscie, mOProgramie;
public Aplikacja()
{
setSize(600,400);
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);
setLayout(new GridLayout(2, 4));
JButton bkalk = new JButton("Kalkulator");
JButton btemp = new JButton("Zamiana stopni temp.");
btemp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Temperatura().setVisible(true);
}
});
JButton bedytor = new JButton("Edytor tekstu");
JButton bkonto = new JButton("Dodawanie kont");
JButton bwyjscie = new JButton("Wyjście");
bwyjscie.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
add(bkonto);
add(bkalk);
add(bwyjscie);
add(btemp);
add(bedytor);
}
@Override
public void actionPerformed(ActionEvent e)
{
Object z = e.getSource();
if (z==mWyjscie)
dispose();
if (z==mOProgramie)
JOptionPane.showMessageDialog(this, "Niezbędnik 1.0\nProgram na zaliczenie.");
}
public static void main(String[] args)
{
Aplikacja aplikacja = new Aplikacja();
aplikacja.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aplikacja.setVisible(true);
}
}
Temperatura
package aplikacja;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Temperatura extends JFrame implements ChangeListener, ActionListener
{
private JLabel lCelsius, lFahrenheit;
private JSlider sCelsius, sFahrenheit;
private int tempCelsius, tempFahrenheit;
JMenuBar menuBar;
JMenu menuPlik;
JMenuItem mWyjscie;
public Temperatura()
{
setSize(500,300);
setTitle("Przeliczanie stopni Celsiusza na Fahrenheita");
setLayout(null);
menuBar = new JMenuBar();
menuPlik = new JMenu("Plik");
mWyjscie = new JMenuItem("Wyjście");
menuPlik.add(mWyjscie);
mWyjscie.addActionListener (this);
mWyjscie.setAccelerator(KeyStroke.getKeyStroke("ctrl X"));
setJMenuBar(menuBar);
menuBar.add(menuPlik);
sCelsius = new JSlider(0, 100, 0);
sCelsius.setBounds(50,50,300,50);
sCelsius.setMajorTickSpacing(20);
sCelsius.setMinorTickSpacing(5);
sCelsius.setPaintTicks(true);
sCelsius.setPaintLabels(true);
sCelsius.addChangeListener(this);
add(sCelsius);
sFahrenheit = new JSlider(30, 212, 30);
sFahrenheit.setBounds(50,150,300,50);
sFahrenheit.setMajorTickSpacing(20);
sFahrenheit.setMinorTickSpacing(5);
sFahrenheit.setPaintTicks(true);
sFahrenheit.setPaintLabels(true);
sFahrenheit.setEnabled(false);
add(sFahrenheit);
lCelsius = new JLabel("Celsius: ");
lCelsius.setBounds(380,50,300,50);
add(lCelsius);
lFahrenheit = new JLabel("Fahrenheit: ");
lFahrenheit.setBounds(380,150,300,50);
add(lFahrenheit);
}
public static void main(String[] args)
{
Temperatura aplikacja = new Temperatura();
aplikacja.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aplikacja.setVisible(true);
}
public void stateChanged(ChangeEvent e)
{
Object z = e.getSource();
if (z==mWyjscie)
dispose();
tempCelsius = sCelsius.getValue();
lCelsius.setText("Celsius: " + tempCelsius);
tempFahrenheit = (int)Math.round(32 + (9.0/5.0)*tempCelsius);
lFahrenheit.setText("Fahrenheit: " + tempFahrenheit);
sFahrenheit.setValue(tempFahrenheit);
}
public void actionPerformed(ActionEvent e)
{
Object z = e.getSource();
if (z==mWyjscie)
dispose();
}
}
Co o tym sądzicie?
dispose();
nie kończy programu, zamyka (i zwalnia zasoby) bieżącego okna. Wykonywanie programu zakończy się jeżeli dispose wywołane zostało na ostatnim otwartym oknie, a Ty tworzysz program wielookienkowy.
Ja bym wolał tworzyć wszystkie okna podrzędne od razu, a wyświetlaniem sterować setVisible(...), wtedy zachowywany jest stan tych okien (wprowadzone wartości, wygląd, położenie).
De gustibus non est disputandum, ale dla mnie wygląd głównego okna jest przerażający.
Czyli jak ty byś to zrobił ja jestem w tym zielony i jak wyjście jest źle to jak ma być ?
I mam jeszcze jeden problem z kalkulatorem po zamknięciu kalkulatora zamyka się cała aplikacja co jest nie tak ? Oto kod:
/*
* 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 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;
/**
*
* @author Hadol
*/
public class Kalkulator extends javax.swing.JFrame {
/**
* Creates new form Kalkulator
*/
public Kalkulator() {
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.
*/
@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();
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.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);
}
});
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)
.addComponent(jTextField1)
.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(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(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(bC, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bWynik, 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)))
.addGap(0, 20, Short.MAX_VALUE)))
.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)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGap(118, 118, 118)
.addComponent(b7, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.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(bWynik, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bDziel, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, 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;
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
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(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Kalkulator.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Kalkulator.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() {
Kalkulator kalk = new Kalkulator();
kalk.setDefaultCloseOperation(JFrame.EXIT_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 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
}
To jest źle
kalk.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
A jeśli chodzi o kończenie programu, to albo
System.exit(0);
, albo w pętli po wszystkich otwartych oknach dispose();
Powyższy kod wyrzuć przez okno albo spal, a najlepiej to jedno i drugie.
A jak powinno być to zamknięcie kalkulatora ?
Przeczytaj dokumentacje https://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29 i wybierz. Ja nie wiem co chcesz uzyskać.
Chce po prostu zamknąć kalkulator a głowne menu ma być otwarte, a po zamknieciu kalkulatora zamyka się wszystko
A zajrzałeś do dokumentacji? Zamknie okno HIDE_ON_CLOSE i DISPOSE_ON_CLOSE. Ale ich działanie się różni, musisz zdecydować.
Tak patrzyłem wszystkie i za każdym razem zamyka wszystko...
Jest w stanie mi ktoś pomóc ?
W tym wierszu
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
zmień.
Dalej zamyka się wszystko ;/
Zrobiłem ten kalkulator przez JForm Form i nie mogę edytować tej linijki jak to zmienić?
Prawym na ramkę, wejdź do properties i zmień pierwszą linijkę