This question already has answers here:
Need to read input of two JTextfields after a button is clicked
(2 answers)
Closed 8 years ago.
So I made a fully functional credit card validator which uses Luhn's Algorithm and all that jazz to validate the card type and number. It currently only uses Scanner and the console to print out stuff, but I wanted to take my program to the next level.
I wanted to make an application with Java graphics that can take in a credit card number entered into my applet/japplet/whatever you suggest and can essentially do the same process as the previously mentioned program, but I want to give it the aesthetic appeal of graphics.
So I'm honestly a little overwhelmed with the graphics in Java (not sure if that's weird), but here's what I want advice on.
How should I approach my graphics project? Should I use JApplet, Applet, JFrame, or something else?
I want to make a text field that the user enters his or her credit card into, what is the method of doing that? I looked up JTextFields but I'm at a loss on how to use it. I looked at the API but it doesn't do a very good job of explaining things in my opinion.
My main problem is the textfield, can someone give me an example of a textfield that can take in data that the user types? Sort of like Scanner in the console but in my graphics application.
Sorry for my word wall, you guys have been very helpful to me in the past :)
Tips, tricks, and anything else you think would help me out would be greatly appreciated.
Here is an example of a text field using swing:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUI extends JFrame { // The JFrame is the window
JTextField textField; // The textField
public GUI() {
textField = new JTextField(10); // The user can enter 10 characters into the textField
textField.addActionListener(new ActionListener() { // This will listen for actions to be performed on the textField (enter button pressed)
#Override
public void actionPerformed(ActionEvent e) { // Called when the enter button is pressed
// TODO Auto-generated method stub
String inputText = textField.getText(); // Get the textField's text
textField.setText(""); // Clear the textField
System.out.println(inputText); // Print out the text (or you can do something else with it)
}
});
JPanel panel = new JPanel(); // Make a panel to be displayed
panel.add(textField); // Add the textField to the panel
this.add(panel); // Add the panel to the JFrame (we extend JFrame)
this.setVisible(true); // Visible
this.setSize(500, 500); // Size
this.setDefaultCloseOperation(EXIT_ON_CLOSE); // Exit when the "x" button is pressed
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}
Related
I'm new to Java programming and I'd like to know how to mess around with buttons, I have created a "Home Screen" and some buttons as well as text, however it's not as advanced as I'd like it to be, I want to know how to create things like image effects, so let's say I hover over a button, I want it to display a glowing animation behind it, or since I don't have that animation, if there's no easy way to create it, just displaying an image behind it is alright, also I don't have anything happening when pressing the button bcs IDK how to do that yet so if you could help with that it'd be awesome!
Here's the code I currently have:
package Menu;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class BackgroundPanel extends JFrame {
private BufferedImage image;
public BackgroundPanel() {
ImageIcon icon = new ImageIcon("image_path.png");
JButton btn = new JButton(icon);
btn.setOpaque(false);
btn.setContentAreaFilled(false);
btn.setBorderPainted(false);
btn.setFocusPainted(false);
try {
image = ImageIO.read(new File("image_path.jpg"));
// Set your Image Here.
this.setContentPane(new JLabel(new ImageIcon(image)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JPanel panel = new JPanel();
panel.add(new JLabel("Username:"));
panel.add(new JTextField("",20));
panel.add(new JLabel("Password:"));
panel.add(new JTextField("",20));
panel.add(btn);
//Adding components to Panel and JFrame
this.add(panel);
// JFrame Properties
this.setSize(1280,720);
this.setLayout(new FlowLayout());
this.setResizable(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Panel");
this.setVisible(true);
}
public static void main(String[] args) {
new BackgroundPanel();
}
}
[...] also I don't have anything happening when pressing the button bcs IDK how to do that yet so if you could help with that it'd be awesome [...]
You need to add an ActionListener to your button. There are various other ways to detect if the button was pressed, but this one is the (in my opinion) easiest. This is how you do it:
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
// code you write in here will be executed if the button was pressed
}
});
[...] let's say I hover over a button, I want it to display a glowing animation behind it, or since I don't have that animation, if there's no easy way to create it, just displaying an image behind it is alright [...]
For this, you'll have to deal with JLayeredPanes and MouseListeners. Here is an example that I created "on the run"; the layouting is very dirty and has to be improved. Anyhow, you'll notice that once you hover over the button, a black-bordered box containing LA- -LL! will appear behind the button. That's a JLabel and you can use it to display images and such by using the JLabel.setIcon method.
let's say I hover over a button, I want it to display a glowing animation behind it, or since I don't have that animation, if there's no easy way to create it, just displaying an image behind it is alright
This is not that easy, it requires jumping through a whole bunch of hoops, especially when the button isn't rectangle.
A while I ago a did a prototype of a "validation highlighter" which highlighted invalid fields, it makes use of JXLayer library, but which should be convertible to use the included JLayer library on the core libraries
See How can I change the highlight color of a focused JComboBox for more details
The other possibility we'd be to create a custom JPanel and override its paintComponent method and paint your effect there. You would place your button it and with a combination of a a layout manager and borders you should be able to get the button positioned where you need it.
The problem with this, is it will effect the over layout of your form, as the effect will be considered while the primary form is laid out
I really don't have anything happening when pressing the button bcs IDK how to do that yet so if you could help with that it'd be awesome
I suggest you have a look at How to use buttons and How to write ActionListeners
What I'm trying to do is create a very basic button click game much like "AdVenture Capitalist" and several other casual games like it. Nothing quite as fancy. I'm using FlashDevelop (AS3) and I'm trying to figure out how to create a button then every time that button is pressed it adds +1 to my main value represented on screen.
I'd also like to figure out how to make a button that would automate the clicking process.
If anyone could show me how, or direct me to a specific page that would help me out I would be very thankful.
I'm also not sure if Actionscript is the best language to write this in. If you have a recommendation that I can use FlashDevelop with I would appreciate the tip!
Thank you!
I have idea about the how this operation will be performed in java. For example creating buttons and labels. As long as you need to combine java and flash this link is useful for starters. If you need only java the following snippet will be useful. Also learn java swing from JavaDocs for GUI purpose
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonClick
{
int counter = 0;
public ButtonClick()
{
JFrame frame = new JFrame("Title");
JButton btn = new JButton("Click me");
JLabel clicks = new JLabel("");
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
counter++;
clicks.setText("Number of clicks till now :"+Integer.toString(counter));
frame.repaint();
}
});
frame.add(btn);
frame.add(clicks);
frame.setVisible(true);
}
public static void main(String []args)
{
ButtonClick c = new ButtonClick();
}
}
This question already has answers here:
JFrame: How to disable window resizing?
(8 answers)
Closed 9 years ago.
how to get non resizable frame in gui i confused about this because i am using setLayout(new FlowLayout()); so if i drag the size of the frame the location of my button is going to disarrange . here is my code so far
import java.awt.*;
import javax.swing.*;
public class aw extends JFrame
{
private JTextField aw1;
private JLabel aww;
private JButton aw2;
public aw()
{
setLayout(new FlowLayout());
aww = new JLabel("Enter Your Password");
add(aww);
aw1 = new JTextField(15);
add(aw1);
aw2 = new JButton("Enter");
add(aw2);
}
public static void main(String args [])
{
aw v = new aw();
v.setSize(200,200);
v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
v.setVisible(true);
}
}
I think setResizable(false) is what you're looking for
SIDE NOTES
Also, instead of setSize(). You should just pack() the frame. You can use EmptyBorders if you want empty space.
If you wanted the frame to be re-sizable and you want all the components centered when resizing, You could always wrap them all in a JPanel, then add the JPanel to the frame.
Use Java naming convention. Class names start with capital letters.
Run Swing apps form the Event Dispatch Thread, see Initial Threads
Put all the content into a JPanel, that would let you configure the pack() element, please ensure that you use an Empty Border. In the Object we have an accesor by setResizable set it to false.
Keep a note of rest and then use a Singleton thread model to run the Event-Dispatch Thread.
Thanks to AndrewThompson for his extra-ordinary knowledge that I was able to make the necessary updates
I'm trying to simply create four jtextfields and a jbutton. Once the button is pushed, i want the text inputted into the jtextfields to be passed as parameters (p, var, s, f) to another window to which displays a mathematical function using the parameters given.
I don't want this second window to show up and display a mathematical function until the initial button was pushed.
How can I do this? I'm sorry if this is a newbie question but I'm learning..
So far, I have graphing part done, and so all I need to do now is create the first window with the textboxes and buttons which link to the graphing window.
Here is the beginning of the code that I think is worth showing so you know which variables I'm talking about:
public class Cartesian {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
CartesianFrame frame = new CartesianFrame();
frame.showUI();
}
});
}
}
class CartesianFrame extends JFrame {
CartesianPanel panel;
public CartesianFrame() {
panel = new CartesianPanel();
add(panel);
}
public void showUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Polynomial Grapher");
setSize(700, 700);
setVisible(true);
}
}
class CartesianPanel extends JPanel {
//These are the variables I want to be assigned to textfields(I'm assuming using "gettext" from another window.
String p="something from textbox one";//Variable 1
String var="something from textbox two";//Variable 2
double s=-2;//ANY double value from textbox 3
double f=2;//ANY double value from textbox 4
...
...
...
The rest of the code used after this is just a paint component, etc. which is used to display the cartesian plane and the mathematical function.
I've looked on the web for some other examples, but they haven't applied to what I'm doing.. I'm interested in any feedback! Thank you!
Don't create a second JFrame. If you absolutely must show a second window, show a dialog such as a JDialog or JOptionPane. As to how to do this, simply create a JPanel that displays the information that you'd like to show the user, perhaps in a JLabel, and then show it in a JOptionPane using its showMessage(...) method. It's pretty easy, actually.
If this doesn't help, then you'd better tell us more about exactly where you're stuck.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to print the user input on screen from a TextField using Java Swing
Please see the code below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Swingtest extends JFrame implements ActionListener{
JTextField txtdata;
JButton calbtn = new JButton("Calculate");
public Swingtest()
{
JPanel myPanel = new JPanel();
add(myPanel);
myPanel.setLayout(new GridLayout(3, 2));
myPanel.add(calbtn);
calbtn.addActionListener(this);
txtdata = new JTextField();
myPanel.add(txtdata);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == calbtn) {
String data = txtdata.getText(); //perform your operation
System.out.println(data);
}
}
public static void main(String args[])
{
Swingtest g = new Swingtest();
g.setLocation(10, 10);
g.setSize(300, 300);
g.setVisible(true);
}
}
What I want to do is display the text entered in the field by the user in the same window. Kinda like with paint(Graphics g) and repaint() when the text is changed. Please help. Thanks.
Add a JLabel, JTextField, JTextArea or whatever component able to display text. Get the document of the JTextField where the user enters text, and add a DocumentChangeListener to this document. Each time a DocumentEvent is received, get the text from the JTextField and update the text in the JLabel, JTextField, JTextArea or whatever component you chose.
This can be done through redirecting the I/O/E streams:
I would have given you an elaborated answer, but the answerer of the answer selected for this question explains it better.
See this question: How could I read Java Console Output into a String buffer
After reading it into a StringBuffer, you can print it onto a JTextField or JTextArea.. etc