I have probably a quick question but here it goes. I am making a game (new to programming) and I want a text field in the middle of my game that I can easily add to kind of like a console. You will be able to view the beginning from late into the game.
Example:
Welcome to the game.
Added when you click a button
More text is added.
You click a different button
More text is added.
I am currently using a JLabel for this but it's a .setText which is inconvenient when I want to have tons of text throughout my game. As well, how could I add a scrollbar to this?
I'd guess you are looking for JTextArea .
Related
I'm currently getting started on a project where I use a grid for a game (Dot game, but that's not very important). Basically, it will be a grid of dots, where a player can click on the lines between the dots (like this).
I currently am using a JFrame, and was thinking about ways I could populate it with JPanel components to create clickable regions. Currently, I was thinking about trying to create for loops to correctly populate it, but I don't know how I could efficiently do this.
Any recommendations for how to do this? I'm not asking people to write code for me, just point me in the right direction. Is there any layout (GridLayout?) that would work well for this?
Welp, I ended up figuring it out on my own, but using a soulution not quite ideal. I have a for loop making every odd line have a black panel, then clickable jbutton, then a black panel, etc. Every even line has a clickable j button, then a white middle pannel, then a clickable jbutton, etc.
Looks like this:
https://gyazo.com/79b80dfe13b017a234e6fad2aa224d58
I am a beginner and I am trying to make a text editor and I want to create a pop up window for text format when I press a menu button where I can put all things like font face, font size , font style etc. Can you tell me how I can make this new window? Thanks for your patience!
For example Notepad:
I think what you're after is a dialog of some kind.
Take a look at How to Make Dialogs for more details.
What I would do is design the basic UI onto a JPanel. I would then add this JPanel to an instance of a JDialog (possibly even using a JOptionPane) and show this dialog, making sure to make it modal, so you can easily retrieve the values set by the user.
This means that you can decide how best to show the user interface or even show it in a number of different ways as it's not constrained to a single top level container
You can simply create a brand spanking new JFrame and it will still be counted as the same application.
Tip: Use Eclipse Window Builder
Is there any way to make a JButton look like it has the input focus when it doesn't?
I am creating a search window that has a text area for the input and a 'Find Next' button to start the search. I want the 'Find Next' button to appear to have the focus even though keyboard input is going to the text area. The idea is notify the user that pressing <Enter> will start the search, similar to applications like Microsoft Word.
I know that I can paint the button myself, but I'm looking for an easier way.
This is not the same as input focus; it's just that the button should be the default button of the dialog.
Get the JRootPane using getRootPane() on your dialog/frame/window and use setDefaultButton on that instead.
L&Fs might draw the default button even differently than one having input focus, so a user might be quite confused if there are two apparent controls having focus.
I am working on a homework so i am not asking for code, i am trying to make this by myself. by the way, i am stuck again with the GUI part and have little problems with code part. first things is about button size and image size. i didnt use methods for size of buttons just set the image as an icon for the button. but as you see below, buttons dont fit the image.
second thing is how can i first disable the icon and when user presses to button it will reveal the icon ?. and how can i embed 8 pictures in a loop? can i create an array for images... i appreciate if you can help me. and thanks anyway : )
I'd start with How to Use Buttons. JToggleButton works well for this, as you can change the Icon in an ItemListener based on the button's selected state. Examples may be found here and here.
The title is a bit confusing, but I will be using Java and Jframe. Basically, I want to be able to click anywhere on the form and have a "text area/box" show up (maybe use a JTextField or JTextArea ?). I want the user to be able to edit, delete and move this string around as well.
I am thinking I need an actionlistener to listen for clicks on the form. Each click will call for a new text"box" to be created. I am not sure how to make this "box" editable, deleteable, or moveable by the user though.
I need a way to store the string and co-ordinate data too. Would it be a good idea to simply extend the JTextField or JTextArea to add co-ordinate information to them? I see that swing is event based, so I need some kind of trigger to "save" the text (was thinking the enter key, but I realize I'd like the user to be able to enter multi-line strings).
Any thoughts would be appreciated. I am familiar with Java but only have a bit of experience with the UI portion.
Instead of an ActionListener you will need a MouseListener to track clicks.
Sounds like you need an undecorated JInternalFrame with a text box in it on JDesktopPane. However, I don't think you can create an undecorated JInternalFrame, maybe start with a normal JInternalFrame with a TextBox in it and create new frames on mouse clicks on the Desktop Pane. Then see if you can make the JInternalFrame more like a Window.
Another route is a custom component that does everything you need. This is possible, just a lot more custom code.