• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

How to make a java GUI

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Avg

Senior Member
Joined
Oct 30, 2003
So I have to write a GUI for this program and I am not allowed to use JOptionPane, the rest of swing is game though. I can pretty much make the gui I just don't know how to make the button work, for example I need to enter a number then choose a base(JRadioButton) and when I click on the ok button I need to save the number entered as a String and I need the base chosen.

Help this is due Friday.
 
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class methodname extends JFrame
{
private JButton button = new JButton("Button");
private JTextField number;
private Double random;

public methodname() throws NullPointerException
{
Container pane = getContentPane();
pane.setLayout(new GridLayout(1,2));
number = new JTextField(1);
buttonHandler btHandler = new buttonHandler();
button.addActionListener(btHandler);
pane.add(button);
pane.add(number);
setSize(200,50);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}


private class buttonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e) throws NullPointerException
{
random = Double.parseDouble(number.getText());
}
}
 public static void main(String[] args) throws NullPointerException
    {
     methodname whatever = new methodname();
    }
}


Not sure what you wanted with the radio button so I left it out, but here you go.

Michael
 
Here it is a little cleaned up along with some corrections:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class methodname extends JFrame
{
private JButton button = new JButton("Ok");
private JTextField number = new JTextField(1);
private JRadioButton radio = new JRadioButton();
private JLabel label = new JLabel("Base");
private String random;



public methodname() throws NullPointerException
{
setLayout(new GridLayout(2,2));


buttonHandler btHandler = new buttonHandler();
button.addActionListener(btHandler);

add(button);
add(number);
add(radio);
add(label);

setSize(200,75);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}


private class buttonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
random = number.getText();
System.out.println(random);
}
}
 public static void main(String[] args)
    {
     methodname whatever = new methodname();
    }
}
 
Last edited:
Back