Problems Getting Values
Hi to all, i'm new :)
I want to create a JFrame that has 2 JLabel, 2 JTextField and 1 JButton.
In this JFrame i want to get the values that will be insert in the Jtexfield, when the 2 values are write i want to press a button (lest say a "OK" button) that led to an actionperformed that will save the 2 values in 2 int (lest say int a; int b;). I already have that done, buy my problem is that i want to use that those 2 values to draw a grid or a rectangule. I have to create a paint funcion to begin use the drawline command. But i don't know how to use the 2 values that i already have (int a,b) to draw something. I mean, i don't know how to get those values in my paint funcion. Here's what i have done.
import java.awt.Graphics;
import java.awt.Color;
public class Ventana3 extends javax.swing.JFrame {
public int a;
public int b;
public Ventana3() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
org.jdesktop.layout.GroupLayout jFrame1Layout = new org.jdesktop.layout.GroupLayout(jFrame1.getContent Pane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(org.jdesktop.lay out.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(org.jdesktop.lay out.GroupLayout.LEADING)
.add(0, 300, Short.MAX_VALUE)
);
getContentPane().setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jLabel1.setText("Ancho");
getContentPane().add(jLabel1);
jLabel1.setBounds(20, 20, 34, 14);
jLabel2.setText("Alto");
getContentPane().add(jLabel2);
jLabel2.setBounds(20, 50, 34, 14);
jButton1.setText("OK");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(150, 80, 47, 23);
getContentPane().add(jTextField1);
jTextField1.setBounds(80, 20, 64, 19);
getContentPane().add(jTextField2);
jTextField2.setBounds(80, 50, 64, 19);
jMenu1.setText("Menu");
jMenuBar1.add(jMenu1);
setJMenuBar(jMenuBar1);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//int x, y;
a=Integer.parseInt(jTextField1.getText());
b=Integer.parseInt(jTextField2.getText());
//paint(a,b);
/*Here's the actionperformed button that i was talking about, my problem is that i want to use the x,y values in the funcion below*/
}
public void paint(Graphics g)
{
int x, y;
//int w = this.getWidth();
//int h = this.getHeight();
y = 10;
while (y <= a)
{
g.drawLine(10, y, a, y);
y = y + 25;
}
x = 10;
while (x <= b)
{
g.drawLine(x, 10, x, b);
x = x + 25;
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ventana3().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
private javax.swing.JFrame jFrame1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
}
Thanks for the help.
|