The Internet

Log In or Register

 to print a copy of the current text

A button on the screen prints the current text.

Ask AI: Write a java program that has a button that you can push

Bookmark

0 Points

▲ good image

bad image ▼

Ask AI on The Internet

Question: Write a java program that has a button that you can push

Asked by brys

0 Points

Log In or Register to Vote

Sure, here is a simple example of a Java GUI program featuring a JButton using Swing. ```java import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JOptionPane; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ButtonExample { public static void main(String[] args) { // Creating instance of JFrame JFrame frame = new JFrame("Button Example"); // Creating panel JPanel panel = new JPanel(); // Creating the JFrame settings frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Creating button JButton button = new JButton("Click Me"); // Add action listener to button button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Button pressed!"); } }); // Add the button to the panel panel.add(button); // Add the panel to the frame frame.add(panel); // Setting the frame visibility to true frame.setVisible(true); } } ``` This program will create a frame with a button saying "Click Me". When the button is clicked, it will display a dialog box saying "Button pressed!".

Answered by gpt-4

Aug. 20, 2023, 12:38 p.m.

0 Points

Log In or Register to Vote

when you want to find the number of days since 1/1/2001 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Date; import javax.swing.*; public class DaysSince { public static void main(String[] args) { JFrame frame = new JFrame("Days Since 1/1/2001"); JButton btn = new JButton("Find Number of Days Since 1/1/2001"); btn.setBounds(150,100,200, 40); btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Date startDate = new Date(2001,1,1); Date endDate = new Date(); long startTime = startDate.getTime(); long endTime = endDate.getTime(); long diffTime = endTime - startTime; long diffDays = diffTime / (1000 * 60 * 60 * 24); JOptionPane.showMessageDialog(frame, "Number of Days Since 1/1/2001: " + diffDays); } }); frame.add(btn); frame.setSize(500,500); frame.setLayout(null); frame.setVisible(true); } }

Answered by text-davinci-003

Feb. 8, 2023, 10:08 a.m.

0 Points

Log In or Register to Vote

Think your answer is better than the AI's? Post it below.

Please log in or sign up to post your answer.

Question Tags
This article was generated in part by one of OpenAI’s large-scale language-generation model, answering user brys's question. Images were generated with OpenAI's AI model, DALL·E 2. The Internet takes ultimate responsibility for the content of this publication.
If you want your question answered by an AI, click here.

Published: Wednesday, February 8, 2023

Comment Section



USD