Hey guys. I need help. Im java newbie and i write small program. I want post my integer variables in the next class. Here a code(I wanna change answer color):
Main class(pagrindine.java):
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; //Mokinio rinkinukas su skaiciofke kur gali keisti atsakymo spalva, pabandyti sukurti uzrasu knygute. public class pagrindine extends JFrame { JLabel pasisveikinimas; JButton calc, uzrasai; JMenuBar menubar; JMenu menu; JMenuItem item; public pagrindine() { setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); pasisveikinimas = new JLabel("Sveiki. Ka naudosite?"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 3; c.gridy = 3; add(pasisveikinimas, c); calc = new JButton("Skaiciuotuvas"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 3; c.gridy = 4; add(calc, c); uzrasai = new JButton("Uzrasai"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 4; c.gridy = 4; add(uzrasai, c); calcs e = new calcs (); calc.addActionListener(e); uzras ee = new uzras (); uzrasai.addActionListener(ee); } public class calcs implements ActionListener { public void actionPerformed(ActionEvent e) { pagrindines gui = new pagrindines(pagrindine.this); gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); gui.setSize(350,200); gui.setLocation(300,300); gui.setVisible(true); } } public class uzras implements ActionListener { public void actionPerformed(ActionEvent ee) { } } public static void main(String[]args) { pagrindine gui = new pagrindine(); gui.setSize(350, 250); gui.setVisible(true); gui.setTitle("Mokinio Rinkinukas"); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
Next Class(pagrindiness.java):
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class pagrindines extends JDialog { JButton atimtis, pridetis, dalyba, daugyba; JLabel atsakymas, nu1, nu2; JTextField n1, n2; JMenuBar menubar; JMenu menu; JMenuItem item; double nn1, nn2, ats; String op; public pagrindines(JFrame frame) { super(frame, "Skaiciuotuvas", true); setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); menubar = new JMenuBar(); setJMenuBar(menubar); menu = new JMenu("Nustatymai"); menubar.add(menu); item = new JMenuItem("Keisti atsakymo spalva"); menu.add(item); nu1 = new JLabel("1st: "); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; add(nu1, c); n1 = new JTextField(10); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 0; c.gridwidth = 3; add(n1, c); nu2= new JLabel("2nd: "); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 1; c.gridwidth = 1; add(nu2, c); n2 = new JTextField(10); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 1; c.gridwidth = 3; add(n2, c); pridetis = new JButton("+"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 2; c.gridwidth = 1; add(pridetis, c); atimtis = new JButton("-"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 2; add(atimtis, c); daugyba = new JButton("*"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 2; c.gridy = 2; add(daugyba, c); dalyba = new JButton("/"); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 3; c.gridy = 2; add(dalyba, c); atsakymas = new JLabel(""); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 4; c.gridwidth = 4; add(atsakymas, c); event e = new event(); atimtis.addActionListener(e); pridetis.addActionListener(e); daugyba.addActionListener(e); dalyba.addActionListener(e); keist k = new keist(); item.addActionListener(k); } public class keist implements ActionListener { public void actionPerformed(ActionEvent k) { slideris gui = new slideris(pagrindines.this); gui.setSize(400,200); gui.setVisible(true); gui.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); } } public class event implements ActionListener { public void actionPerformed(ActionEvent e) { try { nn1 = Double.parseDouble(n1.getText()); } catch(NumberFormatException e1) { atsakymas.setText("Klaida!Turi buti skaicius pirmame langelyje!"); return; } try { nn2 = Double.parseDouble(n2.getText()); }catch (NumberFormatException e2) { atsakymas.setText("Klaida!Turi buti skaicius antrame langelyje!"); return; } op = e.getActionCommand(); if (op.equals("+")) { ats = nn1 + nn2; atsakymas.setText("Atsakymas yra "+ats); atsakymas.setForeground(new Color(r,g,b)); } else { if (op.equals("-")) { ats = nn1 - nn2; atsakymas.setText("Atsakymas yra "+ats); atsakymas.setForeground(new Color(r,g,b)); } else { if (op.equals("*")) { ats = nn1 * nn2; atsakymas.setText("Atsakymas yra "+ats); atsakymas.setForeground(new Color(r,g,b)); } else { if (op.equals("/")) { if (nn1 == 0 || nn2 == 0) { atsakymas.setText("Dalyboje turi negali buti nuliu!!!"); atsakymas.setForeground(new Color(r,g,b)); } else { ats = nn1 / nn2; atsakymas.setText("Atsakymas yra "+ats); atsakymas.setForeground(new Color(r,g,b)); } } } } } } } }
Next Class(slideris.java):
import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; public class slideris extends JDialog { JSlider redSlider,greenSlider,blueSlider; JLabel redLabel,greenLabel,blueLabel; JPanel colorPanel, sliders, labels; int r,g,b; public slideris(pagrindines pagrindines) { super(pagrindines, "Spalvu keitiklis", true); setLayout(new FlowLayout()); redSlider = new JSlider(JSlider.HORIZONTAL,0,255,0); greenSlider = new JSlider(JSlider.HORIZONTAL,0,255,0); blueSlider = new JSlider(JSlider.HORIZONTAL,0,255,0); redLabel = new JLabel("Red = 0"); greenLabel = new JLabel("Green = 0"); blueLabel = new JLabel("Blue = 0"); colorPanel = new JPanel(); colorPanel.setBackground(Color.BLACK); event e = new event(); redSlider.addChangeListener(e); blueSlider.addChangeListener(e); greenSlider.addChangeListener(e); Container pane = this.getContentPane(); pane.setLayout(new GridLayout(1,3,3,3)); sliders = new JPanel(); labels = new JPanel(); pane.add(sliders); pane.add(labels); pane.add(colorPanel); sliders.setLayout(new GridLayout(3,1,2,2)); sliders.add(redSlider); sliders.add(blueSlider); sliders.add(greenSlider); labels.setLayout(new GridLayout(3,1,2,2)); labels.add(blueLabel); labels.add(greenLabel); labels.add(redLabel); } public class event implements ChangeListener { public void stateChanged(ChangeEvent e) { r = redSlider.getValue(); g = greenSlider.getValue(); b = blueSlider.getValue(); colorPanel.setBackground(new Color(r,g,b)); } } }
So, guys, i want post variables r,g,b from class slideris.java in the pagrindiness.java. So you can help me?
Sorry for bad english language ;D Im from Lithuanian, so....