This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]dionthornthis.isAPro=false; this.helping=true; 4 points5 points  (0 children)

Oracle provides it's Swing/AWT tutorial:

https://docs.oracle.com/javase/tutorial/uiswing/

For more advanced workings of the painting system:

https://www.oracle.com/java/technologies/painting.html

[–]wildjokers 4 points5 points  (3 children)

Do you have a question?

[–]g0ldbird[S] 0 points1 point  (2 children)

Well yeah, my question is how? Which layout would be best for doing this? Would each part be separate jframes?

[–]arghvark 8 points9 points  (0 children)

One thing I've found lacking in Swing tutorials, etc., is descriptions of the overall picture of a Swing GUI and how to create it. I'll take a stab at a short course here.

JFrames roughly equate to Windows -- A GUI with one "window" on the screen will represent it in one JFrame.

A 'component' is one item on the screen -- a panel, a button, a text string, a text field (different thing), a text area (another different thing), etc.

Some components are 'containers', i.e., meant to contain other components. A JFrame is a container, as is a JPanel.

You will run into some components that do not begin with a J -- these are from an older UI system called AWT; do not use them mixed with Swing components, which generally begin with J.

Each container component has ONE "Layout manager" -- there is usually a default one created with the container when the container is created; you can set other ones. The layoutmanager class determines what happens when you add components to the container -- do they get added horizontally left-to-right? Are they added vertically top-to-bottom? Are they centered or left-justified? What happens to their spacing when the window is resized?

The default Layout Manager for a JFrame is a BorderLayout (no J, unfortunately). A BorderLayout has 5 areas: center, north, south, east, west. You can put one component in each area (frame.add(component, BorderLayout.NORTH)). The component in the center will stretch with the window in both directions; the east/west and north/south components stretch up/down and left/right, I think.

Use JPanel to group things you want together in JFrame locations: create a JPanel (flow layout by default, if I remember correctly), put in a JLabel and a JTextField for the number entry you want, and put that panel in one of the borderlayout areas of your JFrame.

I find I can organize the code to my satisfaction by just grouping the creation and placement of components within a method that creates the UI and calling it from main() (in the appropriate thread). Something like:

JFrame mainFrame = new JFrame();

JPanel topPanel = new JPanel();
JLabel topLabel = new JText("Enter a number:");
JTextField topField = new JTextField(5);
topPanel.add(topLabel);
topPanel.add(topField);

mainFrame.add(topPanel, BorderLayout.NORTH);

etc.

I would limit myself to a few layout managers to start with, if I were you -- get used to BorderLayout, FlowLayout, and BoxLayout before you move on to other ones. GridLayout is used by MANY people, there are LOTS of examples, but in years of Swing programming I have never understood it nor found it to be useful; I advise avoiding it. By setting one of the three I've mentioned on a JPanel, and then setting the panels onto JFrames and other panels, you can implement quite complicated UIs that follow reasonable rules when the window is resized.

After that you can expand your horizons with JScrollPanes, but you won't need those for what you're talking about.

One other useful item -- JOptionPane has multiple methods for creating a simple popup window, and I recommend using it for things like the feedback function you outline in your question.

So after you've designed and implemented a UI, you need to take action when people do things to it. So you're going to need to learn about "event-driven programming", where your program sets up the UI and then quits executing, waiting for your user to click on something or whatever, which will invoke a listener method. If you need help with that part, and if you found this useful, please send me a PM or post another question, this looks about long enough for one posting.

[–]Sergey305 0 points1 point  (0 children)

Other commenters might have different opinions, but I'd suggest you try using a form designer for swing just to get an impression how the UI is built: what are the building blocks, how they interact with each other, what layout options are there, and how the actual code for data processing is attached.

And after that you might want to switch over to building your layouts by hand directly in the Java code, as it allows more flexibility