How do I put button that convert one by one according to the input data in radio button?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm doing converter of distance for my project but how to put JButton
in this code? I don't want JRadioButton
to be listener.
I wanted to add 3 button that act as trigger to convert inches, miles and feet when user choose at the radio button,
I've tried so many times but its always come up with an error.
Here's the code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MetricConverterWindow extends JFrame
{
private JPanel panel; // A holding panel
private JLabel messageLabel; // A message to the user
private JTextField kiloTextField; // To hold user input
private JRadioButton milesButton; // To convert to miles
private JRadioButton feetButton; // To convert to feet
private JRadioButton inchesButton; // To convert to inches
private ButtonGroup radioButtonGroup; // To group radio buttons
private final int WINDOW_WIDTH = 400; // Window width
private final int WINDOW_HEIGHT = 100; // Window height
/**
Constructor
*/
public MetricConverterWindow()
{
setTitle("Metric Converter");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel()
{
messageLabel = new JLabel("Enter a distance in kilometers");
kiloTextField = new JTextField(10);
milesButton = new JRadioButton("Convert to miles");
feetButton = new JRadioButton("Convert to feet");
inchesButton = new JRadioButton("Convert to inches");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(milesButton);
radioButtonGroup.add(feetButton);
radioButtonGroup.add(inchesButton);
// Add action listeners to the radio buttons.
milesButton.addActionListener(new RadioButtonListener());
feetButton.addActionListener(new RadioButtonListener());
inchesButton.addActionListener(new RadioButtonListener());
// Create a panel and add the components to it.
panel = new JPanel();
panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(milesButton);
panel.add(feetButton);
panel.add(inchesButton);
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input; // To hold the user's input
String convertTo = ""; // The units we're converting to
double result = 0.0; // To hold the conversion
// Get the kilometers entered.
input = kiloTextField.getText();
// Determine which radio button was clicked.
if (e.getSource() == milesButton)
{
// Convert to miles.
convertTo = " miles.";
result = Double.parseDouble(input) * 0.6214;
}
else if (e.getSource() == feetButton)
{
// Convert to feet.
convertTo = " feet.";
result = Double.parseDouble(input) * 3281.0;
}
else if (e.getSource() == inchesButton)
{
// Convert to inches.
convertTo = " inches.";
result = Double.parseDouble(input) * 39370.0;
}
// Display the conversion.
JOptionPane.showMessageDialog(null, input +
" kilometers is " + result + convertTo);
}
}
public static void main(String args)
{
new MetricConverterWindow();
}
}
java swing jbutton
add a comment |
I'm doing converter of distance for my project but how to put JButton
in this code? I don't want JRadioButton
to be listener.
I wanted to add 3 button that act as trigger to convert inches, miles and feet when user choose at the radio button,
I've tried so many times but its always come up with an error.
Here's the code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MetricConverterWindow extends JFrame
{
private JPanel panel; // A holding panel
private JLabel messageLabel; // A message to the user
private JTextField kiloTextField; // To hold user input
private JRadioButton milesButton; // To convert to miles
private JRadioButton feetButton; // To convert to feet
private JRadioButton inchesButton; // To convert to inches
private ButtonGroup radioButtonGroup; // To group radio buttons
private final int WINDOW_WIDTH = 400; // Window width
private final int WINDOW_HEIGHT = 100; // Window height
/**
Constructor
*/
public MetricConverterWindow()
{
setTitle("Metric Converter");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel()
{
messageLabel = new JLabel("Enter a distance in kilometers");
kiloTextField = new JTextField(10);
milesButton = new JRadioButton("Convert to miles");
feetButton = new JRadioButton("Convert to feet");
inchesButton = new JRadioButton("Convert to inches");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(milesButton);
radioButtonGroup.add(feetButton);
radioButtonGroup.add(inchesButton);
// Add action listeners to the radio buttons.
milesButton.addActionListener(new RadioButtonListener());
feetButton.addActionListener(new RadioButtonListener());
inchesButton.addActionListener(new RadioButtonListener());
// Create a panel and add the components to it.
panel = new JPanel();
panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(milesButton);
panel.add(feetButton);
panel.add(inchesButton);
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input; // To hold the user's input
String convertTo = ""; // The units we're converting to
double result = 0.0; // To hold the conversion
// Get the kilometers entered.
input = kiloTextField.getText();
// Determine which radio button was clicked.
if (e.getSource() == milesButton)
{
// Convert to miles.
convertTo = " miles.";
result = Double.parseDouble(input) * 0.6214;
}
else if (e.getSource() == feetButton)
{
// Convert to feet.
convertTo = " feet.";
result = Double.parseDouble(input) * 3281.0;
}
else if (e.getSource() == inchesButton)
{
// Convert to inches.
convertTo = " inches.";
result = Double.parseDouble(input) * 39370.0;
}
// Display the conversion.
JOptionPane.showMessageDialog(null, input +
" kilometers is " + result + convertTo);
}
}
public static void main(String args)
{
new MetricConverterWindow();
}
}
java swing jbutton
1
What is the error?
– Robert
Nov 16 '18 at 12:40
add a comment |
I'm doing converter of distance for my project but how to put JButton
in this code? I don't want JRadioButton
to be listener.
I wanted to add 3 button that act as trigger to convert inches, miles and feet when user choose at the radio button,
I've tried so many times but its always come up with an error.
Here's the code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MetricConverterWindow extends JFrame
{
private JPanel panel; // A holding panel
private JLabel messageLabel; // A message to the user
private JTextField kiloTextField; // To hold user input
private JRadioButton milesButton; // To convert to miles
private JRadioButton feetButton; // To convert to feet
private JRadioButton inchesButton; // To convert to inches
private ButtonGroup radioButtonGroup; // To group radio buttons
private final int WINDOW_WIDTH = 400; // Window width
private final int WINDOW_HEIGHT = 100; // Window height
/**
Constructor
*/
public MetricConverterWindow()
{
setTitle("Metric Converter");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel()
{
messageLabel = new JLabel("Enter a distance in kilometers");
kiloTextField = new JTextField(10);
milesButton = new JRadioButton("Convert to miles");
feetButton = new JRadioButton("Convert to feet");
inchesButton = new JRadioButton("Convert to inches");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(milesButton);
radioButtonGroup.add(feetButton);
radioButtonGroup.add(inchesButton);
// Add action listeners to the radio buttons.
milesButton.addActionListener(new RadioButtonListener());
feetButton.addActionListener(new RadioButtonListener());
inchesButton.addActionListener(new RadioButtonListener());
// Create a panel and add the components to it.
panel = new JPanel();
panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(milesButton);
panel.add(feetButton);
panel.add(inchesButton);
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input; // To hold the user's input
String convertTo = ""; // The units we're converting to
double result = 0.0; // To hold the conversion
// Get the kilometers entered.
input = kiloTextField.getText();
// Determine which radio button was clicked.
if (e.getSource() == milesButton)
{
// Convert to miles.
convertTo = " miles.";
result = Double.parseDouble(input) * 0.6214;
}
else if (e.getSource() == feetButton)
{
// Convert to feet.
convertTo = " feet.";
result = Double.parseDouble(input) * 3281.0;
}
else if (e.getSource() == inchesButton)
{
// Convert to inches.
convertTo = " inches.";
result = Double.parseDouble(input) * 39370.0;
}
// Display the conversion.
JOptionPane.showMessageDialog(null, input +
" kilometers is " + result + convertTo);
}
}
public static void main(String args)
{
new MetricConverterWindow();
}
}
java swing jbutton
I'm doing converter of distance for my project but how to put JButton
in this code? I don't want JRadioButton
to be listener.
I wanted to add 3 button that act as trigger to convert inches, miles and feet when user choose at the radio button,
I've tried so many times but its always come up with an error.
Here's the code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MetricConverterWindow extends JFrame
{
private JPanel panel; // A holding panel
private JLabel messageLabel; // A message to the user
private JTextField kiloTextField; // To hold user input
private JRadioButton milesButton; // To convert to miles
private JRadioButton feetButton; // To convert to feet
private JRadioButton inchesButton; // To convert to inches
private ButtonGroup radioButtonGroup; // To group radio buttons
private final int WINDOW_WIDTH = 400; // Window width
private final int WINDOW_HEIGHT = 100; // Window height
/**
Constructor
*/
public MetricConverterWindow()
{
setTitle("Metric Converter");
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildPanel();
add(panel);
setVisible(true);
}
private void buildPanel()
{
messageLabel = new JLabel("Enter a distance in kilometers");
kiloTextField = new JTextField(10);
milesButton = new JRadioButton("Convert to miles");
feetButton = new JRadioButton("Convert to feet");
inchesButton = new JRadioButton("Convert to inches");
// Group the radio buttons.
radioButtonGroup = new ButtonGroup();
radioButtonGroup.add(milesButton);
radioButtonGroup.add(feetButton);
radioButtonGroup.add(inchesButton);
// Add action listeners to the radio buttons.
milesButton.addActionListener(new RadioButtonListener());
feetButton.addActionListener(new RadioButtonListener());
inchesButton.addActionListener(new RadioButtonListener());
// Create a panel and add the components to it.
panel = new JPanel();
panel.add(messageLabel);
panel.add(kiloTextField);
panel.add(milesButton);
panel.add(feetButton);
panel.add(inchesButton);
}
private class RadioButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String input; // To hold the user's input
String convertTo = ""; // The units we're converting to
double result = 0.0; // To hold the conversion
// Get the kilometers entered.
input = kiloTextField.getText();
// Determine which radio button was clicked.
if (e.getSource() == milesButton)
{
// Convert to miles.
convertTo = " miles.";
result = Double.parseDouble(input) * 0.6214;
}
else if (e.getSource() == feetButton)
{
// Convert to feet.
convertTo = " feet.";
result = Double.parseDouble(input) * 3281.0;
}
else if (e.getSource() == inchesButton)
{
// Convert to inches.
convertTo = " inches.";
result = Double.parseDouble(input) * 39370.0;
}
// Display the conversion.
JOptionPane.showMessageDialog(null, input +
" kilometers is " + result + convertTo);
}
}
public static void main(String args)
{
new MetricConverterWindow();
}
}
java swing jbutton
java swing jbutton
edited Nov 16 '18 at 14:02
Andrew Thompson
154k29165349
154k29165349
asked Nov 16 '18 at 12:34
Royale BlissRoyale Bliss
62
62
1
What is the error?
– Robert
Nov 16 '18 at 12:40
add a comment |
1
What is the error?
– Robert
Nov 16 '18 at 12:40
1
1
What is the error?
– Robert
Nov 16 '18 at 12:40
What is the error?
– Robert
Nov 16 '18 at 12:40
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338012%2fhow-do-i-put-button-that-convert-one-by-one-according-to-the-input-data-in-radio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53338012%2fhow-do-i-put-button-that-convert-one-by-one-according-to-the-input-data-in-radio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What is the error?
– Robert
Nov 16 '18 at 12:40